sebastkm / hybrid-fem-nn-examples

30 stars 10 forks source link

The problem in fem-nn training #1

Open mfh-github opened 3 years ago

mfh-github commented 3 years ago

Dear Sebastian Mitusch: Recently, I run your custom neural network (UFL form) in every fem-nn-examples, but i always get an error in the training.py (minimize) function, and it seems that the value transferred into the Constant(ufl.coefficient) is a value like f_2846, the detials of error are listed below, how to solve it? Thanks a lot.

Traceback (most recent call last): File "/home/mfh/PycharmProjects/FEM-ANN/main.py", line 94, in weights, loss = train(Jhat, maxiter=args.maxiter, ftol=1e-20, tol=1e-20, gtol=1e-20) File "/home/mfh/PycharmProjects/FEM-ANN/training.py", line 57, in train options=options) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/tape.py", line 46, in wrapper return function(args, kwargs) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/optimization/optimization.py", line 254, in minimize opt = algorithm(rf_np, kwargs) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/optimization/optimization.py", line 135, in minimize_scipy_generic res = scipy_minimize(J, m_global, method=method, kwargs) File "/home/mfh/anaconda3/lib/python3.7/site-packages/scipy/optimize/_minimize.py", line 624, in minimize callback=callback, options) File "/home/mfh/anaconda3/lib/python3.7/site-packages/scipy/optimize/lbfgsb.py", line 308, in _minimize_lbfgsb finite_diff_rel_step=finite_diff_rel_step) File "/home/mfh/anaconda3/lib/python3.7/site-packages/scipy/optimize/optimize.py", line 262, in _prepare_scalar_function finite_diff_rel_step, bounds, epsilon=epsilon) File "/home/mfh/anaconda3/lib/python3.7/site-packages/scipy/optimize/_differentiable_functions.py", line 140, in init self._update_fun() File "/home/mfh/anaconda3/lib/python3.7/site-packages/scipy/optimize/_differentiable_functions.py", line 233, in _update_fun self._update_fun_impl() File "/home/mfh/anaconda3/lib/python3.7/site-packages/scipy/optimize/_differentiable_functions.py", line 137, in update_fun self.f = fun_wrapped(self.x) File "/home/mfh/anaconda3/lib/python3.7/site-packages/scipy/optimize/_differentiable_functions.py", line 134, in fun_wrapped return fun(np.copy(x), args) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/reduced_functional_numpy.py", line 36, in call return self.rf.call(self.set_local(m_copies, m_array)) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/reduced_functional_numpy.py", line 41, in set_local m[i], offset = control.assign_numpy(m[i], m_array, offset) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/control.py", line 78, in assign_numpy return self.control._ad_assign_numpy(dst, src, offset) File "/home/mfh/anaconda3/lib/python3.7/site-packages/fenics_adjoint/types/constant.py", line 71, in _ad_assign_numpy dst.assign(backend.Constant(numpy.reshape(src[offset:offset + dst.value_size()], dst.ufl_shape))) File "/home/mfh/anaconda3/lib/python3.7/site-packages/fenics_adjoint/types/constant.py", line 22, in assign other = create_overloaded_object(other) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/overloaded_type.py", line 33, in create_overloaded_object return overloaded_type._ad_init_object(obj) File "/home/mfh/anaconda3/lib/python3.7/site-packages/pyadjoint/overloaded_type.py", line 96, in _ad_init_object return cls(obj) File "/home/mfh/anaconda3/lib/python3.7/site-packages/fenics_adjoint/types/constant.py", line 15, in init backend.Constant.init(self, *args, **kwargs) File "/home/mfh/anaconda3/lib/python3.7/site-packages/dolfin/function/constant.py", line 71, in init floats = list(map(float, array.flat)) File "/home/mfh/anaconda3/lib/python3.7/site-packages/dolfin/function/constant.py", line 128, in float raise TypeError("Cannot convert nonscalar constant to float.") TypeError: Cannot convert nonscalar constant to float.

sebastkm commented 3 years ago

Hi,

Which version of dolfin-adjoint do you have installed? See the solution posted here: https://github.com/sebastkm/hybrid-fem-nn/issues/1

The master branch of dolfin-adjoint should be working with the UFL ANN package since the branch is merged.