lululxvi / deepxde

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

AttributeError: 'float' object has no attribute 'ones' #52

Closed athtareq closed 4 years ago

athtareq commented 4 years ago

Hello Lu !

Here's the error log:


     36     geom = dde.geometry.TimeDomain(0, 100)
---> 37     ic1 = dde.IC(geom, lambda X: 1000 * np.ones(X.shape), boundary, component=0)
     38     ic2 = dde.IC(geom, lambda X: 5000 * np.ones(X.shape), boundary, component=1)
     39     ic3 = dde.IC(geom, lambda X: 200 * np.ones(X.shape), boundary, component=2)

AttributeError: 'float' object has no attribute 'ones'

I also get the same error using np.full_like, so I assume the problem is in the component i.e the declared functions in ode_system, what do you think ? And here's my 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
omegap=0.1923
gammap=0.1724
deltap=0.5
kappa=0.5
c=0.5
np=0.0018
mp=0.0018
eps=0.1
bp=3.58
bw=2.30

def main():
    def ode_system(t, y):
        sp, ep, ip,ap,rp,w = y[:, 0:1], y[:, 1:2], y[:, 2:3],y[:, 3:4],y[:, 4:5],y[:, 5:]
        dsp_t = tf.gradients(sp, t)[0]
        dep_t = tf.gradients(ep, t)[0]
        dip_t = tf.gradients(ip, t)[0]
        dap_t = tf.gradients(ap, t)[0]
        drp_t = tf.gradients(rp, t)[0]
        dw_t = tf.gradients(w, t)[0]

        return [
                dsp_t - np + mp * sp + bp * sp * ( ip + kappa * ap ) + bw * sp * w,
                dep_t - bp * sp * ( ip + kappa * ap ) - bw * sp * w + ( 1 - deltap ) * omegap * ep + deltap *  omegap * ep + mp * ep,
                dip_t - ( 1 - deltap ) * omegap * ep + ( gammap + mp ) * ip, 
                dap_t - deltap * omegap * ep + ( gammap + mp ) * ap, 
                drp_t - gammap * ip - gammap * ap + mp * rp,
                dw_t - eps * ( ip + c * ap - w )
                ]

    def boundary(_, on_initial):
        return on_initial

    geom = dde.geometry.TimeDomain(0, 100)
    ic1 = dde.IC(geom, lambda X: 1000 * np.ones(X.shape), boundary, component=0)
    ic2 = dde.IC(geom, lambda X: 5000 * np.ones(X.shape), boundary, component=1)
    ic3 = dde.IC(geom, lambda X: 200 * np.ones(X.shape), boundary, component=2)
    ic4 = dde.IC(geom, lambda X: 800 * np.ones(X.shape), boundary, component=3)
    ic5 = dde.IC(geom, lambda X: 20 * np.ones(X.shape), boundary, component=4)
    ic6 = dde.IC(geom, lambda X: 10 * np.ones(X.shape), boundary, component=5)

    data = dde.data.PDE(
        geom, ode_system, [ic1, ic2, ic3, ic4, ic5, ic6], 1200, 1200, num_test=1500
    )

    layer_size = [1] + [50] * 3 + [6]
    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=40000)

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

if __name__ == "__main__":
    main()
smao-astro commented 4 years ago

Hi @oubhch ,

np=0.0018

overwrite numpy package, please use a different name for the constant variable.

athtareq commented 4 years ago

@smao-astro My man you have no idea the amount of documentation I've peered through because of this error, the amount of stackoverflow questions I've browsed, the amount of changes I made to this code. And the error is in something as small as this.

Thank you so much !