[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running L-BFGS-B optimizer in TF2 #48167

Open
JHvdM1959 opened this issue Mar 29, 2021 · 31 comments
Open

Running L-BFGS-B optimizer in TF2 #48167

JHvdM1959 opened this issue Mar 29, 2021 · 31 comments
Assignees
Labels
comp:keras Keras related issues stat:contribution welcome Status - Contributions welcome TF 2.4 for issues related to TF 2.4 type:feature Feature requests

Comments

@JHvdM1959
Copy link

System information

  • This concerns a customized script applying PINN
  • Runs both (quite well) on Jupyter Notebooks, and Colab
  • TF2 (and T1 in other environment) installed using Anaconda, and Colab
  • TF 2.4.1
  • Python 3.8.2 (3.7 in Colab)
  • No CUDA used on local host (yet), automatically assigned on Colab
  • NVIDIA 1070 (local host, not yet used), automatically assigned on Colab

Issue at hand:


Originally the optimizer based on L-BFGS-B only runs on TF1 via

self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss,
method = 'L-BFGS-B',
options = {'maxiter': 50000,
'maxfun': 50000,
'maxcor': 50,
'maxls': 50,
'ftol' : 1.0 * np.finfo(float).eps})

The '.contrib' module has been left out of TF2, and so far no straightforward solution found anywhere that works well.

Reason for request:
PINN is a significant and growing development for science / engineering applications. Hence not having
this functionality implemented in a usable and accessible way in TF2 is an issue.

Hence in short, this is a feature request:

Please enable straightforward to use implementation of the Broyden - Fletcher - Goldfarb - Shanno optimization into TF2.
Ideally, accessible through the Keras framework (be it functional API or not).

Thanks and best regards,

Jan van de Mortel

@rmothukuru rmothukuru self-assigned this Mar 30, 2021
@rmothukuru rmothukuru added type:feature Feature requests comp:apis Highlevel API related issues TF 2.4 for issues related to TF 2.4 stat:awaiting tensorflower Status - Awaiting response from tensorflower comp:keras Keras related issues and removed comp:apis Highlevel API related issues labels Mar 30, 2021
@nikitamaia
Copy link
Member

Adding the contributions welcome label to this issue for further investigation by the community. If you are interested in working on this issue, please leave a comment and I will assign it to you. Thanks!

@nikitamaia nikitamaia added stat:contribution welcome Status - Contributions welcome and removed stat:awaiting tensorflower Status - Awaiting response from tensorflower labels Apr 6, 2021
@bhack
Copy link
Contributor
bhack commented Apr 14, 2021

@nikitamaia Probably we could route this in the ecosystem (TF probability). See tensorflow/probability#565

@vulkomilev
Copy link

Hi, can I contribute to this issue ?

@vulkomilev
Copy link

It looks like that this issue don't have any repo.I am creating one

@bhack
Copy link
Contributor
bhack commented Jun 14, 2021

Any news here? @vulkomilev Are you still active on this?

@vulkomilev
Copy link
vulkomilev commented Jun 24, 2021 via email

@vulkomilev
Copy link

@bhack can you help with me with the organization of the project?

@lululxvi
Copy link

Any update on this? I am also looking forward to L-BFGS in TF2.

@vulkomilev
Copy link
vulkomilev commented Aug 16, 2021 via email

@ms140429
Copy link

Hi @vulkomilev , how about the progress of LBFGS on TF2.x? Look forward to your contriubtion.

@lululxvi
Copy link
lululxvi commented Oct 12, 2021

For those who needs L-BFGS in TF 2.x, I implemented a TensorFlow interface for tfp.optimizer.lbfgs_minimize:
https://github.com/lululxvi/deepxde/blob/9f0d86dea2230478d8735615e2ad518c62efe6e2/deepxde/optimizers/tensorflow/tfp_optimizer.py#L103

@ms140429
Copy link

@lululxvi , thanks a lot Lu, great job!

@lwkobe
Copy link
lwkobe commented Nov 10, 2021

@lululxvi How to use this interface for tfp.optimizer.lbfgs_minimize in my own code with TF 2.x?
Could you give me some examples?

@lululxvi
Copy link
lululxvi commented Nov 11, 2021

@lwkobe

import deepxde as dde

net = ...  # your tf.keras.Model
trainable_variables = net.trainable_variables  # the network weights and biases

def build_loss():  # no arguments
    loss = ... # compute the loss for the network
    return loss

dde.optimizers.tfp_optimizer.lbfgs_minimize(trainable_variables, build_loss)

@sushreebarsa sushreebarsa self-assigned this Mar 30, 2022
@sushreebarsa
Copy link
Contributor

@JHvdM1959 Is this still an issue?
Could you please refer this link and let us know if it helps?
Thanks!

@sushreebarsa sushreebarsa added the stat:awaiting response Status - Awaiting response from author label Mar 30, 2022
@lululxvi
Copy link

@sushreebarsa Yes, it is still an issue. L-BFGS in tfp helps, but it is not convenient to use, and we have to add an interface, as I discussed above #48167 (comment)

Ideally, a direct support would be better, something like L-BFGS in PyTorch https://pytorch.org/docs/stable/generated/torch.optim.LBFGS.html

@sushreebarsa sushreebarsa removed the stat:awaiting response Status - Awaiting response from author label Mar 31, 2022
@sushreebarsa sushreebarsa removed their assignment Mar 31, 2022
@bhack
Copy link
Contributor
bhack commented Mar 31, 2022

Probably we could a TF counterpart like:

https://pytorch.org/docs/stable/_modules/torch/optim/lbfgs.html#LBFGS

But I think it Is better to open a new ticket in Keras directly.

You could ask to reopen this or open a new one:

keras-team/keras#5085

@rohitvuppala
Copy link

@lululxvi I tried to use the interface from DeepXDE but encounter an error. While I understand the error, I'm not sure how to resolve it. Any help in this regards is appreciated.

I'm using:

dde.optimizers.config.set_LBFGS_options(maxcor=50,ftol=1*np.finfo(float).eps,maxiter=100000,maxfun=100000,maxls=50)
self.optimizer = dde.optimizers.tfp_optimizer.lbfgs_minimize(self.trainable_var,self.loss)

The error:
"Cannot convert a symbolic Tensor ({}) to numpy array"

It seems like the function only accepts numpy arrays but I have a TF symbolic tensor. Any way to resolve this?

@lululxvi
Copy link
lululxvi commented Jun 22, 2022

@rohitvuppala
Copy link

@lululxvi Thank you for the quick response!. It seems to be an issue with numpy as mentioned here - #56527 (comment).

@mbhynes
Copy link
mbhynes commented Aug 22, 2022

Hi folks,

I was also interested in using L-BFGS (and other batch solver) from scipy.optimize, and have made a package for this in my own work:

I set up the package to be as drag & drop info TF2 as possible, since I was myself alternating between solvers like ADAM and L-BFGS depending on the problem size.
There shouldn't be anything for the caller to do other than the standard (keras) model training steps: .compile() and .fit(), so this also plays nice with KerasTuner and allows you to swap between stochastic and full-batch sovlers.

Extended Footnote on the .fit() interface

FWIW, creating a full batch optimizer that fits well into tensorflow's model training procedure is rather difficult IMO, because of the order of operations currently used to (1) iterate over data and (2) then call an optimizer within the scope of the mini-batch of data, after which the optimizer is effectively paused and expected to pick up again on a new mini-batch. This isn't the order in which ready-made implementations of solvers from other libraries (e.g. scipy, pymanopt, cvxopt) are designed for---they assume a function exists to minimize and they can't be stepped in and out of (although many provide the caller with the option of specifying a callback callable). So any batch solver has to be ported or really massaged in weird ways to fit into the training procedure.

This is a bit of a digressive footnote of course, but personally I think the issue is that the fit() and Optimizer interface is missing a function abstraction that could coalesce the typical stochastic and deterministic optimization procedures into 1 interface.
The key thing would be to remove [or, more accurately, push down] the outer epoch loop over the dataset in the training procedure entirely, and have an Optimizer class that has a narrow scope of optimizing a Function, which may be a DeterministicFunction or StochasticFunction.
Both types of Function implement their __call__ method by sampling from the underlying training dataset. The only difference would be how the __call__ methods iterate and return function values: stochastic functions return estimates based on subsets of dataset iterators [or generators], and DeterministicFunctions return "exact" values based on the full set).
Communication of training progress from the optimizer back to the outer "epoch-level" caller could be handled instead with callbacks made by the optimizer, which may be during the mini-batch loops or in each iteration of the optimization algorithm (where iteration may constitute multiple epochs in general).

@itskobold
Copy link

@mbhynes this is excellent, thank you so much for sharing! Exactly what I've been looking for over the past few months.

@Zhengwu-Miao
Copy link

@lwkobe

import deepxde as dde

net = ...  # your tf.keras.Model
trainable_variables = net.trainable_variables  # the network weights and biases

def build_loss():  # no arguments
    loss = ... # compute the loss for the network
    return loss

dde.optimizers.tfp_optimizer.lbfgs_minimize(trainable_variables, build_loss)

Can this interface return the value of the target function during iteration? If possible, how to achieve them?

@lululxvi
Copy link
lululxvi commented Jun 30, 2023

Can this interface return the value of the target function during iteration? If possible, how to achieve them?

No. But you can simply add one line in the code to return the loss.

https://github.com/lululxvi/deepxde/blob/master/deepxde/optimizers/tensorflow/tfp_optimizer.py

@jesusgl86
Copy link

@lululxvi I tried your solution however the last code did not run correctly.
However using this command helped

dde.optimizers.tensorflow_compat_v1.scipy_optimizer.ScipyOptimizerInterface(self.loss,
method = 'L-BFGS-B',
options = {'maxiter': 50000,
'maxfun': 50000,
'maxcor': 50,
'maxls': 50,
'ftol' : 1.0 * np.finfo(float).eps})

@RoboticAutonomy
Copy link
RoboticAutonomy commented Nov 6, 2023

Hence in short, this is a feature request:

Please enable straightforward to use implementation of the Broyden - Fletcher - Goldfarb - Shanno optimization into TF2. Ideally, accessible through the Keras framework (be it functional API or not).

Thanks and best regards,

@lululxvi I tried your solution however the last code did not run correctly. However using this command helped

dde.optimizers.tensorflow_compat_v1.scipy_optimizer.ScipyOptimizerInterface(self.loss, method = 'L-BFGS-B', options = {'maxiter': 50000, 'maxfun': 50000, 'maxcor': 50, 'maxls': 50, 'ftol' : 1.0 * np.finfo(float).eps})

@jesusgl86 Hi, I am trying to use L-BFGS-B with the PINNs code from there github using the same method with their class that they used. I tried @lululxvi lib but always gives me errors while using inside or outside the class, is there any way to make it work? as i need to use two optimizers? @lululxvi @jesusgl86
here is my code:

import sys
sys.path.insert(0, '../../Utilities/')

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
from scipy.interpolate import griddata
from pyDOE import lhs
from plotting import newfig, savefig
from mpl_toolkits.mplot3d import Axes3D
import time
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable

np.random.seed(1234)
tf.set_random_seed(1234)

class PhysicsInformedNN:
# Initialize the class
def init(self, X_u, u, X_f, layers, lb, ub, nu):

    self.lb = lb
    self.ub = ub

    self.x_u = X_u[:,0:1]
    self.t_u = X_u[:,1:2]
    
    self.x_f = X_f[:,0:1]
    self.t_f = X_f[:,1:2]
    
    self.u = u
    
    self.layers = layers
    self.nu = nu
    
    # Initialize NNs
    self.weights, self.biases = self.initialize_NN(layers)
    
    # tf placeholders and graph
    self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                                 log_device_placement=True))
    
    self.x_u_tf = tf.placeholder(tf.float32, shape=[None, self.x_u.shape[1]])
    self.t_u_tf = tf.placeholder(tf.float32, shape=[None, self.t_u.shape[1]])        
    self.u_tf = tf.placeholder(tf.float32, shape=[None, self.u.shape[1]])
    
    self.x_f_tf = tf.placeholder(tf.float32, shape=[None, self.x_f.shape[1]])
    self.t_f_tf = tf.placeholder(tf.float32, shape=[None, self.t_f.shape[1]])        
            
    self.u_pred = self.net_u(self.x_u_tf, self.t_u_tf) 
    self.f_pred = self.net_f(self.x_f_tf, self.t_f_tf)         
    
    self.loss = tf.reduce_mean(tf.square(self.u_tf - self.u_pred)) + \
                tf.reduce_mean(tf.square(self.f_pred))
           
            
    self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss, 
                                                            method = 'L-BFGS-B', 
                                                            options = {'maxiter': 50000,
                                                                       'maxfun': 50000,
                                                                       'maxcor': 50,
                                                                       'maxls': 50,
                                                                       'ftol' : 1.0 * np.finfo(float).eps})
    
    init = tf.global_variables_initializer()
    self.sess.run(init)

            
def initialize_NN(self, layers):        
    weights = []
    biases = []
    num_layers = len(layers) 
    for l in range(0,num_layers-1):
        W = self.xavier_init(size=[layers[l], layers[l+1]])
        b = tf.Variable(tf.zeros([1,layers[l+1]], dtype=tf.float32), dtype=tf.float32)
        weights.append(W)
        biases.append(b)        
    return weights, biases
    
def xavier_init(self, size):
    in_dim = size[0]
    out_dim = size[1]        
    xavier_stddev = np.sqrt(2/(in_dim + out_dim))
    return tf.Variable(tf.truncated_normal([in_dim, out_dim], stddev=xavier_stddev), dtype=tf.float32)

def neural_net(self, X, weights, biases):
    num_layers = len(weights) + 1
    
    H = 2.0*(X - self.lb)/(self.ub - self.lb) - 1.0
    for l in range(0,num_layers-2):
        W = weights[l]
        b = biases[l]
        H = tf.tanh(tf.add(tf.matmul(H, W), b))
    W = weights[-1]
    b = biases[-1]
    Y = tf.add(tf.matmul(H, W), b)
    return Y
        
def net_u(self, x, t):
    u = self.neural_net(tf.concat([x,t],1), self.weights, self.biases)
    return u

def net_f(self, x,t):
    u = self.net_u(x,t)
    u_t = tf.gradients(u, t)[0]
    u_x = tf.gradients(u, x)[0]
    u_xx = tf.gradients(u_x, x)[0]
    f = u_t + u*u_x - self.nu*u_xx
    
    return f

def callback(self, loss):
    print('Loss:', loss)
    
def train(self):
    
    tf_dict = {self.x_u_tf: self.x_u, self.t_u_tf: self.t_u, self.u_tf: self.u,
               self.x_f_tf: self.x_f, self.t_f_tf: self.t_f}
                                                                                                                      
    self.optimizer.minimize(self.sess, 
                            feed_dict = tf_dict,         
                            fetches = [self.loss], 
                            loss_callback = self.callback)        
                                

def predict(self, X_star):
            
    u_star = self.sess.run(self.u_pred, {self.x_u_tf: X_star[:,0:1], self.t_u_tf: X_star[:,1:2]})  
    f_star = self.sess.run(self.f_pred, {self.x_f_tf: X_star[:,0:1], self.t_f_tf: X_star[:,1:2]})
           
    return u_star, f_star

if name == "main":

nu = 0.01/np.pi
noise = 0.0        

N_u = 100
N_f = 10000
layers = [2, 20, 20, 20, 20, 20, 20, 20, 20, 1]

data = scipy.io.loadmat('../Data/burgers_shock.mat')

t = data['t'].flatten()[:,None]
x = data['x'].flatten()[:,None]
Exact = np.real(data['usol']).T

X, T = np.meshgrid(x,t)

X_star = np.hstack((X.flatten()[:,None], T.flatten()[:,None]))
u_star = Exact.flatten()[:,None]              

# Doman bounds
lb = X_star.min(0)
ub = X_star.max(0)    
    
xx1 = np.hstack((X[0:1,:].T, T[0:1,:].T))
uu1 = Exact[0:1,:].T
xx2 = np.hstack((X[:,0:1], T[:,0:1]))
uu2 = Exact[:,0:1]
xx3 = np.hstack((X[:,-1:], T[:,-1:]))
uu3 = Exact[:,-1:]

X_u_train = np.vstack([xx1, xx2, xx3])
X_f_train = lb + (ub-lb)*lhs(2, N_f)
X_f_train = np.vstack((X_f_train, X_u_train))
u_train = np.vstack([uu1, uu2, uu3])

idx = np.random.choice(X_u_train.shape[0], N_u, replace=False)
X_u_train = X_u_train[idx, :]
u_train = u_train[idx,:]
    
model = PhysicsInformedNN(X_u_train, u_train, X_f_train, layers, lb, ub, nu)

start_time = time.time()                
model.train()
elapsed = time.time() - start_time                
print('Training time: %.4f' % (elapsed))

u_pred, f_pred = model.predict(X_star)
        
error_u = np.linalg.norm(u_star-u_pred,2)/np.linalg.norm(u_star,2)
print('Error u: %e' % (error_u))                     


U_pred = griddata(X_star, u_pred.flatten(), (X, T), method='cubic')
Error = np.abs(Exact - U_pred)

@jesusgl86
Copy link
jesusgl86 commented Nov 6, 2023

@RoboticAutonomy
This is what I did. Change first the files to tensorflow V2.

Example
In the command window

pip install tf-nightly
!tf_upgrade_v2 --infile AC.py --outfile AC.py

It will give an error that one command was not changed which is tf.contrib.opt.ScipyOptimizerInterface

To fix this then do the following:

Have this libraries imported just in case

import tensorflow as tf
import tensorflow_probability as tfp
import deepxde as dde
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
from scipy.interpolate import griddata
from pyDOE import lhs
from plotting import newfig, savefig
from mpl_toolkits.mplot3d import Axes3D
import time
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable

Then replace:

self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss, 
                                                        method = 'L-BFGS-B', 
                                                        options = {'maxiter': 50000,
                                                                   'maxfun': 50000,
                                                                   'maxcor': 50,
                                                                   'maxls': 50,
                                                                   'ftol' : 1.0 * np.finfo(float).eps})

With:

self.optimizer = dde.optimizers.tensorflow_compat_v1.scipy_optimizer.ScipyOptimizerInterface(self.loss,
method = 'L-BFGS-B',
options = {'maxiter': 50000,
'maxfun': 50000,
'maxcor': 50,
'maxls': 50,
'ftol' : 1.0 * np.finfo(float).eps})

Hope this helps.

@RoboticAutonomy
Copy link
RoboticAutonomy commented Nov 6, 2023

update: @jesusgl86 Thank you so much for help. It works now well, but unfortunately the results of the training are completely wrong, and the training finished in 0.5 sec.
I havn't installed tensorflow-probability
111

@jesusgl86
Copy link
jesusgl86 commented Nov 9, 2023

@RoboticAutonomy Let mi post the AC example setup

import sys
sys.path.insert(0, '../../Utilities/')

import tensorflow as tf
import tensorflow_probability as tfp
import deepxde as dde
import numpy as np
import matplotlib.pyplot as plt
import time
import scipy.io
from plotting import newfig, savefig
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable

np.random.seed(1234)
tf.compat.v1.set_random_seed(1234)

class PhysicsInformedNN:
# Initialize the class
def init(self, x0, u0, x1, layers, dt, lb, ub, q):

    self.lb = lb
    self.ub = ub
    
    self.x0 = x0
    self.x1 = x1
    
    self.u0 = u0
    
    self.layers = layers
    self.dt = dt
    self.q = max(q,1)

    # Initialize NN
    self.weights, self.biases = self.initialize_NN(layers)
    
    # Load IRK weights
    tmp = np.float32(np.loadtxt('../../Utilities/IRK_weights/Butcher_IRK%d.txt' % (q), ndmin = 2))
    self.IRK_weights = np.reshape(tmp[0:q**2+q], (q+1,q))
    self.IRK_times = tmp[q**2+q:]
    
    # tf placeholders and graph
    self.sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True,
                                                 log_device_placement=True))
    
    self.x0_tf = tf.compat.v1.placeholder(tf.float32, shape=(None, self.x0.shape[1]))
    self.x1_tf = tf.compat.v1.placeholder(tf.float32, shape=(None, self.x1.shape[1]))
    self.u0_tf = tf.compat.v1.placeholder(tf.float32, shape=(None, self.u0.shape[1]))
    self.dummy_x0_tf = tf.compat.v1.placeholder(tf.float32, shape=(None, self.q)) # dummy variable for fwd_gradients
    self.dummy_x1_tf = tf.compat.v1.placeholder(tf.float32, shape=(None, self.q+1)) # dummy variable for fwd_gradients
    
    self.U0_pred = self.net_U0(self.x0_tf) # N x (q+1)
    self.U1_pred, self.U1_x_pred= self.net_U1(self.x1_tf) # N1 x (q+1)
    
    self.loss = tf.reduce_sum(tf.square(self.u0_tf - self.U0_pred)) + \
                tf.reduce_sum(tf.square(self.U1_pred[0,:] - self.U1_pred[1,:])) + \
                tf.reduce_sum(tf.square(self.U1_x_pred[0,:] - self.U1_x_pred[1,:]))                     
    
    self.optimizer = dde.optimizers.tensorflow_compat_v1.scipy_optimizer.ScipyOptimizerInterface(self.loss, 
                                                            method = 'L-BFGS-B', 
                                                            options = {'maxiter': 50000,
                                                                       'maxfun': 50000,
                                                                       'maxcor': 50,
                                                                       'maxls': 50,
                                                                       'ftol' : 1.0 * np.finfo(float).eps})
    
    self.optimizer_Adam = tf.compat.v1.train.AdamOptimizer()
    self.train_op_Adam = self.optimizer_Adam.minimize(self.loss)
    
    init = tf.compat.v1.global_variables_initializer()
    self.sess.run(init)
    
def initialize_NN(self, layers):        
    weights = []
    biases = []
    num_layers = len(layers) 
    for l in range(0,num_layers-1):
        W = self.xavier_init(size=[layers[l], layers[l+1]])
        b = tf.Variable(tf.zeros([1,layers[l+1]], dtype=tf.float32), dtype=tf.float32)
        weights.append(W)
        biases.append(b)        
    return weights, biases
    
def xavier_init(self, size):
    in_dim = size[0]
    out_dim = size[1]        
    xavier_stddev = np.sqrt(2/(in_dim + out_dim))
    return tf.Variable(tf.random.truncated_normal([in_dim, out_dim], stddev=xavier_stddev), dtype=tf.float32)

def neural_net(self, X, weights, biases):
    num_layers = len(weights) + 1
    
    H = 2.0*(X - self.lb)/(self.ub - self.lb) - 1.0
    for l in range(0,num_layers-2):
        W = weights[l]
        b = biases[l]
        H = tf.tanh(tf.add(tf.matmul(H, W), b))
    W = weights[-1]
    b = biases[-1]
    Y = tf.add(tf.matmul(H, W), b)
    return Y

def fwd_gradients_0(self, U, x):        
    g = tf.gradients(U, x, grad_ys=self.dummy_x0_tf)[0]
    return tf.gradients(g, self.dummy_x0_tf)[0]

def fwd_gradients_1(self, U, x):        
    g = tf.gradients(U, x, grad_ys=self.dummy_x1_tf)[0]
    return tf.gradients(g, self.dummy_x1_tf)[0]

def net_U0(self, x):
    U1 = self.neural_net(x, self.weights, self.biases)
    U = U1[:,:-1]
    U_x = self.fwd_gradients_0(U, x)
    U_xx = self.fwd_gradients_0(U_x, x)
    F = 5.0*U - 5.0*U**3 + 0.0001*U_xx
    U0 = U1 - self.dt*tf.matmul(F, self.IRK_weights.T)
    return U0

def net_U1(self, x):
    U1 = self.neural_net(x, self.weights, self.biases)
    U1_x = self.fwd_gradients_1(U1, x)
    return U1, U1_x # N x (q+1)

def callback(self, loss):
    print('Loss:', loss)

def train(self, nIter):
    tf_dict = {self.x0_tf: self.x0, self.u0_tf: self.u0, self.x1_tf: self.x1,
               self.dummy_x0_tf: np.ones((self.x0.shape[0], self.q)),
               self.dummy_x1_tf: np.ones((self.x1.shape[0], self.q+1))}
    
    start_time = time.time()
    for it in range(nIter):
        self.sess.run(self.train_op_Adam, tf_dict)
        
        # Print
        if it % 10 == 0:
            elapsed = time.time() - start_time
            loss_value = self.sess.run(self.loss, tf_dict)
            print('It: %d, Loss: %.3e, Time: %.2f' % 
                  (it, loss_value, elapsed))
            start_time = time.time()

    self.optimizer.minimize(self.sess,
                            feed_dict = tf_dict,
                            fetches = [self.loss],
                            loss_callback = self.callback)

def predict(self, x_star):
    
    U1_star = self.sess.run(self.U1_pred, {self.x1_tf: x_star})
                
    return U1_star

@5huanghuai
Copy link

@RoboticAutonomy This is what I did. Change first the files to tensorflow V2.

Example In the command window

pip install tf-nightly !tf_upgrade_v2 --infile AC.py --outfile AC.py

It will give an error that one command was not changed which is tf.contrib.opt.ScipyOptimizerInterface

To fix this then do the following:

Have this libraries imported just in case

import tensorflow as tf import tensorflow_probability as tfp import deepxde as dde import numpy as np import matplotlib.pyplot as plt import scipy.io from scipy.interpolate import griddata from pyDOE import lhs from plotting import newfig, savefig from mpl_toolkits.mplot3d import Axes3D import time import matplotlib.gridspec as gridspec from mpl_toolkits.axes_grid1 import make_axes_locatable

Then replace:

self.optimizer = tf.contrib.opt.ScipyOptimizerInterface(self.loss, 
                                                        method = 'L-BFGS-B', 
                                                        options = {'maxiter': 50000,
                                                                   'maxfun': 50000,
                                                                   'maxcor': 50,
                                                                   'maxls': 50,
                                                                   'ftol' : 1.0 * np.finfo(float).eps})

With:

self.optimizer = dde.optimizers.tensorflow_compat_v1.scipy_optimizer.ScipyOptimizerInterface(self.loss, method = 'L-BFGS-B', options = {'maxiter': 50000, 'maxfun': 50000, 'maxcor': 50, 'maxls': 50, 'ftol' : 1.0 * np.finfo(float).eps})

Hope this helps.

Is the problem solved?

@mBarreau
Copy link
mBarreau commented May 4, 2024

Hey all,

It is a rather old topic, but I just want to say that I created a package from the Pi-Yueh Chuang's code. It makes the use of LBFGS very easy in tensorflow 2. Here is the link: https://github.com/mBarreau/tf2-bfgs/

Now, it resumes to create the optimizer and call it (see readme):

from tf2-bfgs import LBFGS
optimizer_BFGS = LBFGS(get_cost, omega.trainable_variables)
results = optimizer_BFGS.minimize(omega, t, x)

Hope that helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues stat:contribution welcome Status - Contributions welcome TF 2.4 for issues related to TF 2.4 type:feature Feature requests
Projects
None yet
Development

No branches or pull requests