lululxvi / deepxde

A library for scientific machine learning and physics-informed learning
https://deepxde.readthedocs.io
GNU Lesser General Public License v2.1
2.64k stars 739 forks source link

TypeError: unsupported operand type(s) for -: 'NoneType' and 'float' #47

Closed athtareq closed 4 years ago

athtareq commented 4 years ago

Hello, I'm having the error in the title using this code

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

import deepxde as dde
from deepxde.backend import tf

def main():
    def ode_system(x, y):
        """ODE system.
        dy1/dx = y2
        dy2/dx = -y1
        """
        y1, y2 = y[:, 0:1], y[:, 1:]
        dy1_x = tf.gradients(y1, x)[0]
        dy2_x = tf.gradients(y2, x)[0]
        return [dy1_x + y1*y2*y2, dy2_x - y1*y2*y2]

    def boundary(_, on_initial):
        return on_initial

    geom = dde.geometry.TimeDomain(0, 10)
    ic1 = dde.IC(geom, lambda x: np.full_like(x, 1), boundary, component=0)
    ic2 = dde.IC(geom, lambda x: np.full_like(x, 2), boundary, component=1) 
    data = dde.data.PDE(
        geom, ode_system, [ic1, ic2], 35, 2, num_test=100
    )

    layer_size = [1] + [50] * 3 + [2]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)

    model = dde.Model(data, net)
    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=20000)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)

if __name__ == "__main__":
    main()

And further details:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-24-2e2913918ac8> in <module>()
     43 
     44 if __name__ == "__main__":
---> 45     main()

5 frames

<ipython-input-24-2e2913918ac8> in main()
     37     model = dde.Model(data, net)
     38     model.compile("adam", lr=0.001, metrics=["l2 relative error"])
---> 39     losshistory, train_state = model.train(epochs=20000)
     40 
     41     dde.saveplot(losshistory, train_state, issave=True, isplot=True)

/usr/local/lib/python3.6/dist-packages/deepxde/utils.py in wrapper(*args, **kwargs)
     49     def wrapper(*args, **kwargs):
     50         ts = time.time()
---> 51         result = f(*args, **kwargs)
     52         te = time.time()
     53         print("%r took %f s\n" % (f.__name__, te - ts))

/usr/local/lib/python3.6/dist-packages/deepxde/model.py in train(self, epochs, batch_size, display_every, uncertainty, disregard_previous_best, callbacks, model_restore_path, model_save_path, print_model)
    144         self.train_state.set_data_train(*self.data.train_next_batch(self.batch_size))
    145         self.train_state.set_data_test(*self.data.test())
--> 146         self._test(uncertainty)
    147         self.callbacks.on_train_begin()
    148         if train_module.is_scipy_opts(self.optimizer):

/usr/local/lib/python3.6/dist-packages/deepxde/model.py in _test(self, uncertainty)
    291             self.train_state.metrics_test = [
    292                 m(self.train_state.y_test, self.train_state.y_pred_test)
--> 293                 for m in self.metrics
    294             ]
    295 

/usr/local/lib/python3.6/dist-packages/deepxde/model.py in <listcomp>(.0)
    291             self.train_state.metrics_test = [
    292                 m(self.train_state.y_test, self.train_state.y_pred_test)
--> 293                 for m in self.metrics
    294             ]
    295 

/usr/local/lib/python3.6/dist-packages/deepxde/metrics.py in l2_relative_error(y_true, y_pred)
     13 
     14 def l2_relative_error(y_true, y_pred):
---> 15     return np.linalg.norm(y_true - y_pred) / np.linalg.norm(y_true)
     16 
     17 

TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

when 2 months ago this code


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

import deepxde as dde

def main():
    def ode_system(x, y):
        """ODE system.
        dy1/dx = y2
        dy2/dx = -y1
        """
        u, v = y[:, 0:1], y[:, 1:]
        du_x = tf.gradients(u, x)[0]
        dv_x = tf.gradients(v, x)[0]
        return [du_x + u*v*v, dv_x - u*v*v]

    def boundary(_, on_initial):
        return on_initial 

    geom = dde.geometry.TimeDomain(0,1)
    ic1 = dde.IC(geom, lambda X: 1 * np.ones(X.shape), boundary, component=0)
    ic2 = dde.IC(geom, lambda X: 2 * np.ones(X.shape), boundary, component=1)
    data = dde.data.PDE(geom, 2, ode_system, [ic1, ic2], 35, 2, num_test=100)

    layer_size = [1] + [50] * 3 + [2]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)

    model = dde.Model(data, net)
    model.compile("adam", lr=0.001, metrics=["l2 relative error"])
    losshistory, train_state = model.train(epochs=20000)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)

if __name__ == "__main__":
    main()

worked perfectly. What changed ?

System is: {du/dt= -uv^2 ;dv/dt= uv^2; u(0)=1 , v(0)=2}

Thank you !

lululxvi commented 4 years ago

Because you don't specify the reference solution, then it cannot compute the L2 relative error. Use

model.compile("adam", lr=0.001)
athtareq commented 4 years ago

Thank you I just caught it too !

Also another question, how can I write this PDE system : - d^2u/dx^2 - d^2u/dy^2 =f(x,y) with boundary condition u(x,y)=0 on the board of \W = ]0,1[x]0,1[, is this correct :

    from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

import deepxde as dde

def main():
    def pde (x , y ) :
        dy_x = tf . gradients (y , x ) [0]
        dy_x , dy_y = dy_x [: , 0:1] , dy_x [: , 1:]
        dy_xx = tf . gradients ( dy_x , x ) [0][: , 0:1]
        dy_yy = tf . gradients ( dy_y , x ) [0][: , 1:]
        return - dy_xx - dy_yy 

    def boundary(_, on_boundary):
        return on_boundary  
    def func (x) :
        return np.zeros([len(x),1])

    geom = dde.geometry.Polygon([[0 , 1], [0 , 1], [0 , 1], [0 , 1]])
    bc = dde.DirichletBC(geom,func,boundary)
    data = dde.data.PDE(geom, pde , bc , num_domain =1200 , num_boundary =120)
    layer_size = [2] + [50] * 3 + [1]
    activation = "tanh"
    initializer = "Glorot uniform"
    net = dde.maps.FNN(layer_size, activation, initializer)

    model = dde.Model(data, net)
    model.compile("adam", lr=0.001)
    losshistory, train_state = model.train(epochs=20000)

    dde.saveplot(losshistory, train_state, issave=True, isplot=True)

if __name__ == "__main__":
    main()

Edit: I'm getting the message this polygon is a rectangle, use Rectangle instead but I don't know how to, any help ?

So I did this geom = dde.geometry.Rectangle([0,0],[1,1])

And it's compiling, so good start I guess ! But is it correct ?

lululxvi commented 4 years ago
athtareq commented 4 years ago

Cheers man !