greenlearning.utils package

Submodules

greenlearning.utils.backend module

Source: https://github.com/lululxvi/deepxde/blob/master/deepxde/backend.py

greenlearning.utils.backend.backend()[source]

Returns the name and version of the current backend, e.g., (“tensorflow”, 1.14.0).

Returns:

A tuple of the name and version of the backend GreenLearning is currently using.

Return type:

tuple

Example:

gl.utils.backend.backend()
>>> ("tensorflow", 1.15.0)
greenlearning.utils.backend.is_tf_1()[source]

Check the version of Tensorflow.

greenlearning.utils.config module

greenlearning.utils.external_optimizer module

TensorFlow interface for third-party optimizers.

Code below is taken from https://github.com/tensorflow/tensorflow/blob/v1.15.2/tensorflow/contrib/opt/python/training/external_optimizer.py, because the tf.contrib module is not included in TensorFlow 2.

Another solution is using TensorFlow Probability, see the following references. But the following solution requires setting the weights before building the network and loss, which is not consistent with other optimizers in graph mode. A possible solution Could be adding a TFPOptimizerInterface similar to ScipyOptimizerInterface.

class greenlearning.utils.external_optimizer.ExternalOptimizerInterface(loss, var_list=None, equalities=None, inequalities=None, var_to_bounds=None, **optimizer_kwargs)[source]

Bases: object

Base class for interfaces with external optimization algorithms. Subclass this and implement _minimize in order to wrap a new optimization algorithm. ExternalOptimizerInterface should not be instantiated directly; instead use e.g. ScipyOptimizerInterface. @@__init__ @@minimize

minimize(session=None, feed_dict=None, fetches=None, step_callback=None, loss_callback=None, **run_kwargs)[source]

Minimize a scalar Tensor. Variables subject to optimization are updated in-place at the end of optimization. Note that this method does not just return a minimization Op, unlike Optimizer.minimize(); instead it actually performs minimization by executing commands to control a Session.

Parameters:
  • session – A Session instance.

  • feed_dict – A feed dict to be passed to calls to session.run.

  • fetches – A list of Tensor`s to fetch and supply to `loss_callback as positional arguments.

  • step_callback – A function to be called at each optimization step; arguments are the current values of all optimization variables flattened into a single vector.

  • loss_callback – A function to be called every time the loss and gradients are computed, with evaluated fetches supplied as positional arguments.

  • **run_kwargs – kwargs to pass to session.run.

class greenlearning.utils.external_optimizer.ScipyOptimizerInterface(loss, var_list=None, equalities=None, inequalities=None, var_to_bounds=None, **optimizer_kwargs)[source]

Bases: ExternalOptimizerInterface

Wrapper allowing scipy.optimize.minimize to operate a tf.compat.v1.Session.

Example:

vector = tf.Variable([7., 7.], 'vector')
# Make vector norm as small as possible.
loss = tf.reduce_sum(tf.square(vector))
optimizer = ScipyOptimizerInterface(loss, options={'maxiter': 100})
with tf.compat.v1.Session() as session:
    optimizer.minimize(session)
# The value of vector should now be [0., 0.].

Example with simple bound constraints:

vector = tf.Variable([7., 7.], 'vector')
# Make vector norm as small as possible.
loss = tf.reduce_sum(tf.square(vector))
optimizer = ScipyOptimizerInterface(loss, var_to_bounds={vector: ([1, 2], np.infty)})
with tf.compat.v1.Session() as session:
    optimizer.minimize(session)
# The value of vector should now be [1., 2.].

Example with more complicated constraints:

vector = tf.Variable([7., 7.], 'vector')
# Make vector norm as small as possible.
loss = tf.reduce_sum(tf.square(vector))
# Ensure the vector's y component is = 1.
equalities = [vector[1] - 1.]
# Ensure the vector's x component is >= 1.
inequalities = [vector[0] - 1.]
# Our default SciPy optimization algorithm, L-BFGS-B, does not support
# general constraints. Thus we use SLSQP instead.
optimizer = ScipyOptimizerInterface(loss, equalities=equalities, inequalities=inequalities, method='SLSQP')
with tf.compat.v1.Session() as session:
    optimizer.minimize(session)
# The value of vector should now be [1., 1.].

greenlearning.utils.load_data module

greenlearning.utils.load_data.load_data(model, example_path, example_name)[source]

Load the training dataset.

greenlearning.utils.plotting module

class greenlearning.utils.plotting.MathTextSciFormatter(fmt='%1.2e')[source]

Bases: Formatter

Format the axis of the figure.

greenlearning.utils.plotting.figsize(scale, nplots=1)[source]

Define the figure size.

greenlearning.utils.plotting.newfig(width, nplots=1)[source]

Create a new figure.

greenlearning.utils.plotting.savefig(filename, crop=True)[source]

Save the figure as a pdf file.

greenlearning.utils.print_weights module

greenlearning.utils.print_weights.print_weights(model)[source]

Print all the trainable weights.

greenlearning.utils.real module

Source: https://github.com/lululxvi/deepxde/blob/master/deepxde/real.py

class greenlearning.utils.real.Real(precision)[source]

Bases: object

Set the float precision in numpy and Tensorflow.

set_float32()[source]
set_float64()[source]
set_precision(precision)[source]

greenlearning.utils.save_results module

greenlearning.utils.save_results.save_results(model, Green_slice=1)[source]

Save the Green’s function evaluated at a grid in a csv file. If the spatial dimension is equal to 2, Green_slice indicates the slice to save the Green’s function’

greenlearning.utils.tf_session module

greenlearning.utils.tf_session.open_tf_session()[source]

Open a Tensorflow session.

greenlearning.utils.visualization module

greenlearning.utils.visualization.input_data_slice(model, Green_slice=1)[source]

Return evaluation points for the selected slice of the Green’s function. Green_slice=1 : G(:,:,y1,y2), where y1, y2 are points in the middle of the domain. Green_slice=2 : G(:,x2,:,y2). Green_slice=3 : G(x1,x2,:,:), where x1, x2 are points in the middle of the domain. Green_slice=4 : G(x1,:,y1,:).

greenlearning.utils.visualization.plot_1d_results(model)[source]

Plot the learned Green’s function and homogeneous solution in 1D.

greenlearning.utils.visualization.plot_1d_systems(model)[source]

Plot the learned Green’s functions and homogeneous solutions for a system of PDEs in 1D.

greenlearning.utils.visualization.plot_2d_results(model)[source]

Plot the learned Green’s function and homogeneous solution in 2D.

greenlearning.utils.visualization.plot_2d_systems(model, Green_slice=1)[source]

Plot the learned Green’s functions and homogeneous solutions of a system of PDEs in 2D.

greenlearning.utils.visualization.plot_results(model)[source]

Plot the learned Green’s function and homogeneous solution in a pdf.