greenlearning.utils package¶
Submodules¶
greenlearning.utils.backend module¶
Source: https://github.com/lululxvi/deepxde/blob/master/deepxde/backend.py
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.
https://www.tensorflow.org/probability/api_docs/python/tfp/optimizer/lbfgs_minimize
https://stackoverflow.com/questions/58591562/how-can-we-use-lbfgs-minimize-in-tensorflow-2-0
https://gist.github.com/piyueh/712ec7d4540489aad2dcfb80f9a54993
https://github.com/pierremtb/PINNs-TF2.0/blob/master/utils/neuralnetwork.py
-
class
greenlearning.utils.external_optimizer.ExternalOptimizerInterface(loss, var_list=None, equalities=None, inequalities=None, var_to_bounds=None, **optimizer_kwargs)[source]¶ Bases:
objectBase 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:
greenlearning.utils.external_optimizer.ExternalOptimizerInterfaceWrapper 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.plotting module¶
greenlearning.utils.print_weights module¶
greenlearning.utils.real module¶
Source: https://github.com/lululxvi/deepxde/blob/master/deepxde/real.py
greenlearning.utils.save_results module¶
greenlearning.utils.tf_session module¶
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.