From 3638b841c044c6ddb81a14745527a4f61511bccf Mon Sep 17 00:00:00 2001 From: xusuyong <2209245477@qq.com> Date: Wed, 13 Dec 2023 10:47:16 +0800 Subject: [PATCH 1/3] add water tensor dipole --- deepmd/entrypoints/test.py | 4 +- deepmd/fit/dipole.py | 266 +++++++++++++++++-------- deepmd/infer/deep_eval.py | 19 +- deepmd/infer/deep_tensor.py | 384 ++++++++++++++++++++++++++++-------- deepmd/loss/tensor.py | 102 +++++++--- deepmd/model/tensor.py | 88 +++++---- deepmd/train/trainer.py | 10 +- 7 files changed, 631 insertions(+), 242 deletions(-) diff --git a/deepmd/entrypoints/test.py b/deepmd/entrypoints/test.py index 761862c330..5f5bce6256 100644 --- a/deepmd/entrypoints/test.py +++ b/deepmd/entrypoints/test.py @@ -890,12 +890,12 @@ def test_dipole( atomic=atomic, must=True, high_prec=False, - type_sel=dp.get_sel_type(), + type_sel=dp.tselt, ) test_data = data.get_test() dipole, numb_test, atype = run_test(dp, test_data, numb_test, data) - sel_type = dp.get_sel_type() + sel_type = dp.tselt sel_natoms = 0 for ii in sel_type: sel_natoms += sum(atype == ii) diff --git a/deepmd/fit/dipole.py b/deepmd/fit/dipole.py index 52d68af480..2ef9de14e2 100644 --- a/deepmd/fit/dipole.py +++ b/deepmd/fit/dipole.py @@ -1,20 +1,37 @@ +import logging from typing import List from typing import Optional - +import pdb import numpy as np +from paddle import nn +from deepmd.common import add_data_requirement from deepmd.common import cast_precision from deepmd.common import get_activation_func from deepmd.common import get_precision +from deepmd.env import GLOBAL_PD_FLOAT_PRECISION +from deepmd.env import GLOBAL_TF_FLOAT_PRECISION +from deepmd.env import global_cvt_2_pd_float +from deepmd.env import global_cvt_2_tf_float +from deepmd.env import paddle from deepmd.env import tf from deepmd.fit.fitting import Fitting from deepmd.utils.graph import get_fitting_net_variables_from_graph_def from deepmd.utils.network import one_layer from deepmd.utils.network import one_layer_rand_seed_shift +# from deepmd.infer import DeepPotential +from deepmd.nvnmd.fit.ener import one_layer_nvnmd +from deepmd.nvnmd.utils.config import nvnmd_cfg +from deepmd.utils.errors import GraphWithoutTensorError +from deepmd.utils.graph import get_fitting_net_variables_from_graph_def +from deepmd.utils.graph import get_tensor_by_name_from_graph +from deepmd.utils.network import OneLayer as OneLayer_deepmd +from deepmd.utils.network import one_layer as one_layer_deepmd +from deepmd.utils.network import one_layer_rand_seed_shift +from deepmd.utils.spin import Spin - -@Fitting.register("dipole") -class DipoleFittingSeA(Fitting): +# @Fitting.register("dipole") +class DipoleFittingSeA(nn.Layer): r"""Fit the atomic dipole with descriptor se_a. Parameters @@ -40,7 +57,7 @@ class DipoleFittingSeA(Fitting): def __init__( self, - descrpt: tf.Tensor, + descrpt: paddle.Tensor, neuron: List[int] = [120, 120, 120], resnet_dt: bool = True, sel_type: Optional[List[int]] = None, @@ -49,8 +66,9 @@ def __init__( precision: str = "default", uniform_seed: bool = False, ) -> None: + super().__init__(name_scope="DipoleFittingSeA") """Constructor.""" - self.ntypes = descrpt.get_ntypes() + self.ntypes = descrpt.get_ntypes()#2 self.dim_descrpt = descrpt.get_dim_out() self.n_neuron = neuron self.resnet_dt = resnet_dt @@ -62,6 +80,7 @@ def __init__( ) self.seed = seed self.uniform_seed = uniform_seed + self.ntypes_spin = 0 self.seed_shift = one_layer_rand_seed_shift() self.fitting_activation_fn = get_activation_func(activation_function) self.fitting_precision = get_precision(precision) @@ -71,6 +90,55 @@ def __init__( self.fitting_net_variables = None self.mixed_prec = None + type_suffix = "" + suffix = "" + self.one_layers = nn.LayerList() + self.final_layers = nn.LayerList() + ntypes_atom = self.ntypes - self.ntypes_spin + for type_i in range(0, ntypes_atom): + type_i_layers = nn.LayerList() + for ii in range(0, len(self.n_neuron)): + + layer_suffix = "layer_" + str(ii) + type_suffix + suffix + + if ii >= 1 and self.n_neuron[ii] == self.n_neuron[ii - 1]: + type_i_layers.append( + OneLayer_deepmd( + self.n_neuron[ii - 1], + self.n_neuron[ii], + activation_fn=self.fitting_activation_fn, + precision=self.fitting_precision, + name=layer_suffix, + seed=self.seed, + use_timestep=self.resnet_dt, + ) + ) + else: + type_i_layers.append( + OneLayer_deepmd( + self.dim_descrpt, + self.n_neuron[ii], + activation_fn=self.fitting_activation_fn, + precision=self.fitting_precision, + name=layer_suffix, + seed=self.seed, + ) + ) + if (not self.uniform_seed) and (self.seed is not None): + self.seed += self.seed_shift + + self.one_layers.append(type_i_layers) + self.final_layers.append( + OneLayer_deepmd( + self.n_neuron[-1], + self.dim_rot_mat_1, + activation_fn=None, + precision=self.fitting_precision, + name=layer_suffix, + seed=self.seed, + ) + ) + def get_sel_type(self) -> int: """Get selected type.""" return self.sel_type @@ -78,80 +146,102 @@ def get_sel_type(self) -> int: def get_out_size(self) -> int: """Get the output size. Should be 3.""" return 3 - - def _build_lower(self, start_index, natoms, inputs, rot_mat, suffix="", reuse=None): + + def _build_lower(self, start_index, natoms, inputs, rot_mat, suffix="", reuse=None, + type_i=None, + ): # cut-out inputs - inputs_i = tf.slice(inputs, [0, start_index, 0], [-1, natoms, -1]) - inputs_i = tf.reshape(inputs_i, [-1, self.dim_descrpt]) - rot_mat_i = tf.slice(rot_mat, [0, start_index, 0], [-1, natoms, -1]) - rot_mat_i = tf.reshape(rot_mat_i, [-1, self.dim_rot_mat_1, 3]) + inputs_i = paddle.slice( + inputs, + [0, 1, 2], + [0, start_index, 0], + [inputs.shape[0], start_index + natoms, inputs.shape[2]], + ) + inputs_i = paddle.reshape(inputs_i, [-1, self.dim_descrpt]) + rot_mat_i = paddle.slice( + rot_mat, + [0, 1, 2], + [0, start_index, 0], + [rot_mat.shape[0], start_index + natoms, rot_mat.shape[2]], + ) + # paddle.slice(rot_mat, [0, start_index, 0], [-1, natoms, -1]) + rot_mat_i = paddle.reshape(rot_mat_i, [-1, self.dim_rot_mat_1, 3]) layer = inputs_i for ii in range(0, len(self.n_neuron)): if ii >= 1 and self.n_neuron[ii] == self.n_neuron[ii - 1]: - layer += one_layer( - layer, - self.n_neuron[ii], - name="layer_" + str(ii) + suffix, - reuse=reuse, - seed=self.seed, - use_timestep=self.resnet_dt, - activation_fn=self.fitting_activation_fn, - precision=self.fitting_precision, - uniform_seed=self.uniform_seed, - initial_variables=self.fitting_net_variables, - mixed_prec=self.mixed_prec, - ) + layer += self.one_layers[type_i][ii](layer) else: - layer = one_layer( - layer, - self.n_neuron[ii], - name="layer_" + str(ii) + suffix, - reuse=reuse, - seed=self.seed, - activation_fn=self.fitting_activation_fn, - precision=self.fitting_precision, - uniform_seed=self.uniform_seed, - initial_variables=self.fitting_net_variables, - mixed_prec=self.mixed_prec, - ) + layer = self.one_layers[type_i][ii](layer) + # if ii >= 1 and self.n_neuron[ii] == self.n_neuron[ii - 1]: + # layer += one_layer( + # layer, + # self.n_neuron[ii], + # name="layer_" + str(ii) + suffix, + # reuse=reuse, + # seed=self.seed, + # use_timestep=self.resnet_dt, + # activation_fn=self.fitting_activation_fn, + # precision=self.fitting_precision, + # uniform_seed=self.uniform_seed, + # initial_variables=self.fitting_net_variables, + # mixed_prec=self.mixed_prec, + # ) + # else: + # layer = one_layer( + # layer, + # self.n_neuron[ii], + # name="layer_" + str(ii) + suffix, + # reuse=reuse, + # seed=self.seed, + # activation_fn=self.fitting_activation_fn, + # precision=self.fitting_precision, + # uniform_seed=self.uniform_seed, + # initial_variables=self.fitting_net_variables, + # mixed_prec=self.mixed_prec, + # ) if (not self.uniform_seed) and (self.seed is not None): self.seed += self.seed_shift - # (nframes x natoms) x naxis - final_layer = one_layer( + + final_layer = self.final_layers[type_i]( layer, - self.dim_rot_mat_1, - activation_fn=None, - name="final_layer" + suffix, - reuse=reuse, - seed=self.seed, - precision=self.fitting_precision, - uniform_seed=self.uniform_seed, - initial_variables=self.fitting_net_variables, - mixed_prec=self.mixed_prec, - final_layer=True, ) + # # (nframes x natoms) x naxis + # final_layer = one_layer( + # layer, + # self.dim_rot_mat_1, + # activation_fn=None, + # name="final_layer" + suffix, + # reuse=reuse, + # seed=self.seed, + # precision=self.fitting_precision, + # uniform_seed=self.uniform_seed, + # initial_variables=self.fitting_net_variables, + # mixed_prec=self.mixed_prec, + # final_layer=True, + # ) if (not self.uniform_seed) and (self.seed is not None): self.seed += self.seed_shift # (nframes x natoms) x 1 * naxis - final_layer = tf.reshape( - final_layer, [tf.shape(inputs)[0] * natoms, 1, self.dim_rot_mat_1] - ) + final_layer = paddle.reshape( + final_layer, [paddle.shape(inputs)[0] * natoms, 1, self.dim_rot_mat_1] + )#natoms=64, self.dim_rot_mat_1=100 # (nframes x natoms) x 1 x 3(coord) - final_layer = tf.matmul(final_layer, rot_mat_i) + final_layer = paddle.matmul(final_layer, rot_mat_i) # nframes x natoms x 3 - final_layer = tf.reshape(final_layer, [tf.shape(inputs)[0], natoms, 3]) - return final_layer + final_layer = paddle.reshape(final_layer, [paddle.shape(inputs)[0], natoms, 3]) + # pdb.set_trace() + return final_layer # [1, 64, 3] + - @cast_precision - def build( + def forward( self, - input_d: tf.Tensor, - rot_mat: tf.Tensor, - natoms: tf.Tensor, + input_d: paddle.Tensor, + rot_mat: paddle.Tensor, + natoms: paddle.Tensor, input_dict: Optional[dict] = None, reuse: Optional[bool] = None, suffix: str = "", - ) -> tf.Tensor: + ) -> paddle.Tensor: """Build the computational graph for fitting net. Parameters @@ -183,22 +273,22 @@ def build( atype = input_dict.get("atype", None) nframes = input_dict.get("nframes") start_index = 0 - inputs = tf.reshape(input_d, [-1, natoms[0], self.dim_descrpt]) - rot_mat = tf.reshape(rot_mat, [-1, natoms[0], self.dim_rot_mat]) + inputs = paddle.reshape(input_d, [-1, natoms[0], self.dim_descrpt]) + rot_mat = paddle.reshape(rot_mat, [-1, natoms[0], self.dim_rot_mat]) if type_embedding is not None: - nloc_mask = tf.reshape( - tf.tile(tf.repeat(self.sel_mask, natoms[2:]), [nframes]), [nframes, -1] + nloc_mask = paddle.reshape( + paddle.tile(paddle.repeat(self.sel_mask, natoms[2:]), [nframes]), [nframes, -1] ) - atype_nall = tf.reshape(atype, [-1, natoms[1]]) + atype_nall = paddle.reshape(atype, [-1, natoms[1]]) # (nframes x nloc_masked) - self.atype_nloc_masked = tf.reshape( - tf.slice(atype_nall, [0, 0], [-1, natoms[0]])[nloc_mask], [-1] + self.atype_nloc_masked = paddle.reshape( + paddle.slice(atype_nall, [0, 0], [-1, natoms[0]])[nloc_mask], [-1] ) ## lammps will make error - self.nloc_masked = tf.shape( - tf.reshape(self.atype_nloc_masked, [nframes, -1]) + self.nloc_masked = paddle.shape( + paddle.reshape(self.atype_nloc_masked, [nframes, -1]) )[1] - atype_embed = tf.nn.embedding_lookup(type_embedding, self.atype_nloc_masked) + atype_embed = paddle.nn.embedding_lookup(type_embedding, self.atype_nloc_masked) else: atype_embed = None @@ -207,10 +297,11 @@ def build( if atype_embed is None: count = 0 outs_list = [] - for type_i in range(self.ntypes): + # pdb.set_trace() + for type_i in range(self.ntypes):#2 if type_i not in self.sel_type: start_index += natoms[2 + type_i] - continue + continue #sel_type是0,所以就循环了一次 final_layer = self._build_lower( start_index, natoms[2 + type_i], @@ -218,39 +309,44 @@ def build( rot_mat, suffix="_type_" + str(type_i) + suffix, reuse=reuse, + type_i=type_i, ) start_index += natoms[2 + type_i] # concat the results outs_list.append(final_layer) count += 1 - outs = tf.concat(outs_list, axis=1) + # pdb.set_trace() + + outs = paddle.concat(outs_list, axis=1) # [1, 64, 3] else: - inputs = tf.reshape( - tf.reshape(inputs, [nframes, natoms[0], self.dim_descrpt])[nloc_mask], + inputs = paddle.reshape( + paddle.reshape(inputs, [nframes, natoms[0], self.dim_descrpt])[nloc_mask], [-1, self.dim_descrpt], ) - rot_mat = tf.reshape( - tf.reshape(rot_mat, [nframes, natoms[0], self.dim_rot_mat_1 * 3])[ + rot_mat = paddle.reshape( + paddle.reshape(rot_mat, [nframes, natoms[0], self.dim_rot_mat_1 * 3])[ nloc_mask ], [-1, self.dim_rot_mat_1, 3], ) - atype_embed = tf.cast(atype_embed, self.fitting_precision) + atype_embed = paddle.cast(atype_embed, self.fitting_precision) type_shape = atype_embed.get_shape().as_list() - inputs = tf.concat([inputs, atype_embed], axis=1) + inputs = paddle.concat([inputs, atype_embed], axis=1) self.dim_descrpt = self.dim_descrpt + type_shape[1] - inputs = tf.reshape(inputs, [nframes, self.nloc_masked, self.dim_descrpt]) - rot_mat = tf.reshape( + inputs = paddle.reshape(inputs, [nframes, self.nloc_masked, self.dim_descrpt]) + rot_mat = paddle.reshape( rot_mat, [nframes, self.nloc_masked, self.dim_rot_mat_1 * 3] ) final_layer = self._build_lower( 0, self.nloc_masked, inputs, rot_mat, suffix=suffix, reuse=reuse ) + pdb.set_trace() + # nframes x natoms x 3 - outs = tf.reshape(final_layer, [nframes, self.nloc_masked, 3]) + outs = paddle.reshape(final_layer, [nframes, self.nloc_masked, 3]) - tf.summary.histogram("fitting_net_output", outs) - return tf.reshape(outs, [-1]) + # paddle.summary.histogram("fitting_net_output", outs) + return paddle.reshape(outs, [-1]) # return tf.reshape(outs, [tf.shape(inputs)[0] * natoms[0] * 3 // 3]) def init_variables( diff --git a/deepmd/infer/deep_eval.py b/deepmd/infer/deep_eval.py index e1dac07f81..a8faee32b9 100644 --- a/deepmd/infer/deep_eval.py +++ b/deepmd/infer/deep_eval.py @@ -8,7 +8,7 @@ # Descriptor, # ) import numpy as np - +import os import deepmd from deepmd.common import data_requirement from deepmd.common import expand_sys_str @@ -20,7 +20,9 @@ from deepmd.env import paddle from deepmd.env import tf from deepmd.fit import ener +from deepmd.fit import dipole from deepmd.model import EnerModel +from deepmd.model import DipoleModel from deepmd.utils.argcheck import type_embedding_args from deepmd.utils.batch_size import AutoBatchSize from deepmd.utils.sess import run_sess @@ -77,7 +79,7 @@ def __init__( default_tf_graph: bool = False, auto_batch_size: Union[bool, int, AutoBatchSize] = False, ): - jdata = j_loader("input.json") + jdata = j_loader("input.json" if os.path.isfile("input.json") else "dipole_input.json") remove_comment_in_json(jdata) model_param = j_must_have(jdata, "model") self.multi_task_mode = "fitting_net_dict" in model_param @@ -140,7 +142,12 @@ def __init__( if fitting_type == "ener": fitting_param["spin"] = spin fitting_param.pop("type", None) - fitting = ener.EnerFitting(**fitting_param) + fitting = ener.EnerFitting(**fitting_param) + elif fitting_type == "dipole": + fitting_param.pop("type", None) + fitting = dipole.DipoleFittingSeA(**fitting_param) + else: + pass else: self.fitting_dict = {} self.fitting_type_dict = {} @@ -216,7 +223,6 @@ def __init__( ) elif self.fitting_type == "dipole": - raise NotImplementedError() self.model = DipoleModel( descrpt, fitting, @@ -352,7 +358,8 @@ def __init__( @property @lru_cache(maxsize=None) def model_type(self) -> str: - return "ener" + + return self.model.model_type """Get type of model. :type:str @@ -411,7 +418,7 @@ def _graph_compatable(self) -> bool: def _get_value( self, tensor_name: str, attr_name: Optional[str] = None - ) -> tf.Tensor: + ) -> paddle.Tensor: """Get TF graph tensor and assign it to class namespace. Parameters ---------- diff --git a/deepmd/infer/deep_tensor.py b/deepmd/infer/deep_tensor.py index 8b31d91b2d..a2653cacd1 100644 --- a/deepmd/infer/deep_tensor.py +++ b/deepmd/infer/deep_tensor.py @@ -1,11 +1,15 @@ from typing import TYPE_CHECKING +from typing import Callable from typing import List from typing import Optional from typing import Tuple +from typing import Union + import numpy as np from deepmd.common import make_default_mesh +from deepmd.env import paddle from deepmd.infer.deep_eval import DeepEval from deepmd.utils.sess import run_sess @@ -60,7 +64,7 @@ def __init__( # now load tensors to object attributes for attr_name, tensor_name in self.tensors.items(): - self._get_tensor(tensor_name, attr_name) + self._get_value(tensor_name, attr_name) # load optional tensors if possible optional_tensors = { @@ -72,18 +76,20 @@ def __init__( try: # first make sure these tensor all exists (but do not modify self attr) for attr_name, tensor_name in optional_tensors.items(): - self._get_tensor(tensor_name) + self._get_value(tensor_name) # then put those into self.attrs for attr_name, tensor_name in optional_tensors.items(): - self._get_tensor(tensor_name, attr_name) + self._get_value(tensor_name, attr_name) except KeyError: self._support_gfv = False else: self.tensors.update(optional_tensors) self._support_gfv = True - self._run_default_sess() - self.tmap = self.tmap.decode("UTF-8").split() + # self._run_default_sess() + self.ntypes = int(self.model.descrpt.buffer_ntypes) + self.tselt=self.model.fitting.sel_type + # self.tmap = self.tmap.decode("UTF-8").split() def _run_default_sess(self): [self.ntypes, self.rcut, self.tmap, self.tselt, self.output_dim] = run_sess( @@ -120,19 +126,260 @@ def get_dim_fparam(self) -> int: def get_dim_aparam(self) -> int: """Get the number (dimension) of atomic parameters of this DP.""" return self.daparam + + def _eval_func(self, inner_func: Callable, numb_test: int, natoms: int) -> Callable: + """Wrapper method with auto batch size. + + Parameters + ---------- + inner_func : Callable + the method to be wrapped + numb_test : int + number of tests + natoms : int + number of atoms + + Returns + ------- + Callable + the wrapper + """ + if self.auto_batch_size is not None: + def eval_func(*args, **kwargs): + return self.auto_batch_size.execute_all( + inner_func, numb_test, natoms, *args, **kwargs + ) + + else: + eval_func = inner_func + return eval_func + + def _get_natoms_and_nframes( + self, + coords: np.ndarray, + atom_types: Union[List[int], np.ndarray], + mixed_type: bool = False, + ) -> Tuple[int, int]: + if mixed_type: + natoms = len(atom_types[0]) + else: + natoms = len(atom_types) + coords = np.reshape(np.array(coords), [-1, natoms * 3]) + nframes = coords.shape[0] + return natoms, nframes + def _prepare_feed_dict( + self, + coords, + cells, + atom_types, + fparam=None, + aparam=None, + atomic=False, + efield=None, + mixed_type=False, + ): + # standarize the shape of inputs + natoms, nframes = self._get_natoms_and_nframes( + coords, atom_types, mixed_type=mixed_type + ) + if mixed_type: + atom_types = np.array(atom_types, dtype=int).reshape([-1, natoms]) + else: + atom_types = np.array(atom_types, dtype=int).reshape([-1]) + coords = np.reshape(np.array(coords), [-1, natoms * 3]) + if cells is None: + pbc = False + # make cells to work around the requirement of pbc + cells = np.tile(np.eye(3), [nframes, 1]).reshape([nframes, 9]) + else: + pbc = True + cells = np.array(cells).reshape([nframes, 9]) + + # if self.has_fparam: + # assert fparam is not None + # fparam = np.array(fparam) + # if self.has_aparam: + # assert aparam is not None + # aparam = np.array(aparam) + # if self.has_efield: + # assert ( + # efield is not None + # ), "you are using a model with external field, parameter efield should be provided" + # efield = np.array(efield) + + # reshape the inputs + # if self.has_fparam: + # fdim = self.get_dim_fparam() + # if fparam.size == nframes * fdim: + # fparam = np.reshape(fparam, [nframes, fdim]) + # elif fparam.size == fdim: + # fparam = np.tile(fparam.reshape([-1]), [nframes, 1]) + # else: + # raise RuntimeError( + # "got wrong size of frame param, should be either %d x %d or %d" + # % (nframes, fdim, fdim) + # ) + # if self.has_aparam: + # fdim = self.get_dim_aparam() + # if aparam.size == nframes * natoms * fdim: + # aparam = np.reshape(aparam, [nframes, natoms * fdim]) + # elif aparam.size == natoms * fdim: + # aparam = np.tile(aparam.reshape([-1]), [nframes, 1]) + # elif aparam.size == fdim: + # aparam = np.tile(aparam.reshape([-1]), [nframes, natoms]) + # else: + # raise RuntimeError( + # "got wrong size of frame param, should be either %d x %d x %d or %d x %d or %d" + # % (nframes, natoms, fdim, natoms, fdim, fdim) + # ) + + # sort inputs + # coords, atom_types, imap = self.sort_input( + # coords, atom_types, mixed_type=mixed_type + # ) + # if self.has_efield: + # efield = np.reshape(efield, [nframes, natoms, 3]) + # efield = efield[:, imap, :] + # efield = np.reshape(efield, [nframes, natoms * 3]) + + # make natoms_vec and default_mesh + natoms_vec = self.make_natoms_vec(atom_types, mixed_type=mixed_type) + assert natoms_vec[0] == natoms + + # evaluate + # feed_dict_test = {} + # feed_dict_test[self.t_natoms] = natoms_vec + # if mixed_type: + # feed_dict_test[self.t_type] = atom_types.reshape([-1]) + # else: + # feed_dict_test[self.t_type] = np.tile(atom_types, [nframes, 1]).reshape( + # [-1] + # ) + # feed_dict_test[self.t_coord] = np.reshape(coords, [-1]) + + # if len(self.t_box.shape) == 1: + # feed_dict_test[self.t_box] = np.reshape(cells, [-1]) + # elif len(self.t_box.shape) == 2: + # feed_dict_test[self.t_box] = cells + # else: + # raise RuntimeError + # if self.has_efield: + # feed_dict_test[self.t_efield] = np.reshape(efield, [-1]) + # if pbc: + # feed_dict_test[self.t_mesh] = make_default_mesh(cells) + # else: + # feed_dict_test[self.t_mesh] = np.array([], dtype=np.int32) + # if self.has_fparam: + # feed_dict_test[self.t_fparam] = np.reshape(fparam, [-1]) + # if self.has_aparam: + # feed_dict_test[self.t_aparam] = np.reshape(aparam, [-1]) + return None, None, natoms_vec + def _eval_inner( + self, + coords, + cells, + atom_types, + fparam=None, + aparam=None, + atomic=False, + efield=None, + mixed_type=False, + ): + natoms, nframes = self._get_natoms_and_nframes( + coords, atom_types, mixed_type=mixed_type + ) + feed_dict_test, imap, natoms_vec = self._prepare_feed_dict( + coords, cells, atom_types, fparam, aparam, efield, mixed_type=mixed_type + ) + if cells is None: + pbc = False + cells = np.tile(np.eye(3), [nframes, 1]).reshape([nframes, 9]) + else: + pbc = True + cells = np.array(cells).reshape([nframes, 9]) + eval_inputs = {} + eval_inputs["coord"] = paddle.to_tensor( + np.reshape(coords, [-1]), dtype="float64" + ) + eval_inputs["type"] = paddle.to_tensor( + np.tile(atom_types, [nframes, 1]).reshape([-1]), dtype="int32" + ) + eval_inputs["natoms_vec"] = paddle.to_tensor( + natoms_vec, dtype="int32", place="cpu" + ) + eval_inputs["box"] = paddle.to_tensor(np.reshape(cells, [-1]), dtype="float64") + import pdb + # pdb.set_trace() + # if self.has_fparam: + # eval_inputs["fparam"] = paddle.to_tensor( + # np.reshape(fparam, [-1], dtype="float64") + # ) + # if self.has_aparam: + # eval_inputs["aparam"] = paddle.to_tensor( + # np.reshape(aparam, [-1], dtype="float64") + # ) + # if se.pbc: + # eval_inputs["default_mesh"] = paddle.to_tensor( + # make_default_mesh(cells), dtype="int32" + # ) + # else: + eval_inputs['default_mesh'] = paddle.to_tensor(np.array([], dtype = np.int32)) + + if hasattr(self, "st_model"): + # NOTE: 使用静态图模型推理 + eval_outputs = self.st_model( + eval_inputs["coord"], # [2880] paddle.float64 + eval_inputs["type"], # [960] paddle.int32 + eval_inputs["natoms_vec"], # [2+num_type_atoms] paddle.int32 + eval_inputs["box"], # [45] paddle.float64 + eval_inputs["default_mesh"], # [6] paddle.int32 + ) + eval_outputs = { + "atom_ener": eval_outputs[0], + "atom_virial": eval_outputs[1], + "atype": eval_outputs[2], + "coord": eval_outputs[3], + "energy": eval_outputs[4], + "force": eval_outputs[5], + "virial": eval_outputs[6], + } + else: + # NOTE: 使用动态图模型推理 + eval_outputs = self.model( + eval_inputs["coord"], # + eval_inputs["type"], # + eval_inputs["natoms_vec"], # + eval_inputs["box"], # + eval_inputs["default_mesh"], # + eval_inputs, + suffix="", + reuse=False, + ) + dipole = eval_outputs["dipole"].numpy() + + return dipole + + + # if atomic: + # ae = eval_outputs["atom_ener"].numpy() + # av = eval_outputs["atom_virial"].numpy() + # return energy, force, virial, ae, av + # else: + # return energy, force, virial + def eval( self, coords: np.ndarray, cells: np.ndarray, atom_types: List[int], - atomic: bool = True, + atomic: bool = False, fparam: Optional[np.ndarray] = None, aparam: Optional[np.ndarray] = None, efield: Optional[np.ndarray] = None, mixed_type: bool = False, - ) -> np.ndarray: - """Evaluate the model. + ) -> Tuple[np.ndarray, ...]: + """Evaluate the energy, force and virial by using this DP. Parameters ---------- @@ -147,14 +394,21 @@ def eval( The atom types The list should contain natoms ints atomic - If True (default), return the atomic tensor - Otherwise return the global tensor + Calculate the atomic energy and virial fparam - Not used in this model + The frame parameter. + The array can be of size : + - nframes x dim_fparam. + - dim_fparam. Then all frames are assumed to be provided with the same fparam. aparam - Not used in this model + The atomic parameter + The array can be of size : + - nframes x natoms x dim_aparam. + - natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam. + - dim_aparam. Then all frames and atoms are provided with the same aparam. efield - Not used in this model + The external field on atoms. + The array should be of size nframes x natoms x 3 mixed_type Whether to perform the mixed_type mode. If True, the input data has the mixed_type format (see doc/model/train_se_atten.md), @@ -162,77 +416,45 @@ def eval( Returns ------- - tensor - The returned tensor - If atomic == False then of size nframes x output_dim - else of size nframes x natoms x output_dim + energy + The system energy. + force + The force on each atom + virial + The virial + atom_energy + The atomic energy. Only returned when atomic == True + atom_virial + The atomic virial. Only returned when atomic == True """ - # standarize the shape of inputs - if mixed_type: - natoms = atom_types[0].size - atom_types = np.array(atom_types, dtype=int).reshape([-1, natoms]) - else: - atom_types = np.array(atom_types, dtype=int).reshape([-1]) - natoms = atom_types.size - coords = np.reshape(np.array(coords), [-1, natoms * 3]) - nframes = coords.shape[0] - if cells is None: - pbc = False - cells = np.tile(np.eye(3), [nframes, 1]).reshape([nframes, 9]) - else: - pbc = True - cells = np.array(cells).reshape([nframes, 9]) - - # sort inputs - coords, atom_types, imap, sel_at, sel_imap = self.sort_input( - coords, atom_types, sel_atoms=self.get_sel_type(), mixed_type=mixed_type + # reshape coords before getting shape + natoms, numb_test = self._get_natoms_and_nframes( + coords, atom_types, mixed_type=mixed_type + ) # 192, 30 + output = self._eval_func(self._eval_inner, numb_test, natoms)( + coords, + cells, + atom_types, + fparam=fparam, + aparam=aparam, + atomic=atomic, + efield=efield, + mixed_type=mixed_type, ) - # make natoms_vec and default_mesh - natoms_vec = self.make_natoms_vec(atom_types, mixed_type=mixed_type) - assert natoms_vec[0] == natoms - - # evaluate - feed_dict_test = {} - feed_dict_test[self.t_natoms] = natoms_vec - if mixed_type: - feed_dict_test[self.t_type] = atom_types.reshape([-1]) - else: - feed_dict_test[self.t_type] = np.tile(atom_types, [nframes, 1]).reshape( - [-1] - ) - feed_dict_test[self.t_coord] = np.reshape(coords, [-1]) - feed_dict_test[self.t_box] = np.reshape(cells, [-1]) - if pbc: - feed_dict_test[self.t_mesh] = make_default_mesh(cells) - else: - feed_dict_test[self.t_mesh] = np.array([], dtype=np.int32) - - if atomic: - assert ( - "global" not in self.model_type - ), f"cannot do atomic evaluation with model type {self.model_type}" - t_out = [self.t_tensor] - else: - assert ( - self._support_gfv or "global" in self.model_type - ), f"do not support global tensor evaluation with old {self.model_type} model" - t_out = [self.t_global_tensor if self._support_gfv else self.t_tensor] - v_out = self.sess.run(t_out, feed_dict=feed_dict_test) - tensor = v_out[0] - - # reverse map of the outputs - if atomic: - tensor = np.array(tensor) - tensor = self.reverse_map( - np.reshape(tensor, [nframes, -1, self.output_dim]), sel_imap - ) - tensor = np.reshape(tensor, [nframes, len(sel_at), self.output_dim]) - else: - tensor = np.reshape(tensor, [nframes, self.output_dim]) - - return tensor - + # if self.modifier_type is not None: # 这里不会运行 + # if atomic: + # raise RuntimeError("modifier does not support atomic modification") + # me, mf, mv = self.dm.eval(coords, cells, atom_types) + # output = list(output) # tuple to list + # e, f, v = output[:3] + # output[0] += me.reshape(e.shape) + # output[1] += mf.reshape(f.shape) + # output[2] += mv.reshape(v.shape) + # output = tuple(output) + + return output + def eval_full( self, coords: np.ndarray, diff --git a/deepmd/loss/tensor.py b/deepmd/loss/tensor.py index ff28b78ed5..ff5e49b996 100644 --- a/deepmd/loss/tensor.py +++ b/deepmd/loss/tensor.py @@ -2,6 +2,7 @@ from deepmd.common import add_data_requirement from deepmd.env import global_cvt_2_tf_float +from deepmd.env import paddle from deepmd.env import tf from deepmd.utils.sess import run_sess @@ -59,30 +60,30 @@ def __init__(self, jdata, **kwarg): type_sel=self.type_sel, ) - def build(self, learning_rate, natoms, model_dict, label_dict, suffix): + def compute_loss(self, learning_rate, natoms, model_dict, label_dict, suffix): polar_hat = label_dict[self.label_name] atomic_polar_hat = label_dict["atomic_" + self.label_name] - polar = tf.reshape(model_dict[self.tensor_name], [-1]) + polar = paddle.reshape(model_dict[self.tensor_name], [-1]) find_global = label_dict["find_" + self.label_name] find_atomic = label_dict["find_atomic_" + self.label_name] # YHT: added for global / local dipole combination - l2_loss = global_cvt_2_tf_float(0.0) + l2_loss = 0.0 more_loss = { - "local_loss": global_cvt_2_tf_float(0.0), - "global_loss": global_cvt_2_tf_float(0.0), + "local_loss": 0.0, + "global_loss": 0.0, } if self.local_weight > 0.0: - local_loss = global_cvt_2_tf_float(find_atomic) * tf.reduce_mean( - tf.square(self.scale * (polar - atomic_polar_hat)), name="l2_" + suffix + local_loss = find_atomic * paddle.mean( + paddle.square(self.scale * (polar - atomic_polar_hat)), name="l2_" + suffix ) more_loss["local_loss"] = local_loss l2_loss += self.local_weight * local_loss - self.l2_loss_local_summary = tf.summary.scalar( - "l2_local_loss_" + suffix, tf.sqrt(more_loss["local_loss"]) - ) + # self.l2_loss_local_summary = paddle.summary.scalar( + # "l2_local_loss_" + suffix, paddle.sqrt(more_loss["local_loss"]) + # ) if self.global_weight > 0.0: # Need global loss atoms = 0 @@ -91,33 +92,33 @@ def build(self, learning_rate, natoms, model_dict, label_dict, suffix): atoms += natoms[2 + w] else: atoms = natoms[0] - nframes = tf.shape(polar)[0] // self.tensor_size // atoms + nframes = paddle.shape(polar)[0] // self.tensor_size // atoms # get global results - global_polar = tf.reshape( - tf.reduce_sum( - tf.reshape(polar, [nframes, -1, self.tensor_size]), axis=1 + global_polar = paddle.reshape( + paddle.sum( + paddle.reshape(polar, [nframes, -1, self.tensor_size]), axis=1 ), [-1], ) # if self.atomic: # If label is local, however - # global_polar_hat = tf.reshape(tf.reduce_sum(tf.reshape( + # global_polar_hat = paddle.reshape(paddle.sum(paddle.reshape( # polar_hat, [nframes, -1, self.tensor_size]), axis=1),[-1]) # else: # global_polar_hat = polar_hat - global_loss = global_cvt_2_tf_float(find_global) * tf.reduce_mean( - tf.square(self.scale * (global_polar - polar_hat)), name="l2_" + suffix + global_loss = find_global * paddle.mean( + paddle.square(self.scale * (global_polar - polar_hat)), name="l2_" + suffix ) more_loss["global_loss"] = global_loss - self.l2_loss_global_summary = tf.summary.scalar( - "l2_global_loss_" + suffix, - tf.sqrt(more_loss["global_loss"]) / global_cvt_2_tf_float(atoms), - ) + # self.l2_loss_global_summary = paddle.summary.scalar( + # "l2_global_loss_" + suffix, + # paddle.sqrt(more_loss["global_loss"]) / global_cvt_2_tf_float(atoms), + # ) # YWolfeee: should only consider atoms with dipole, i.e. atoms # atom_norm = 1./ global_cvt_2_tf_float(natoms[0]) - atom_norm = 1.0 / global_cvt_2_tf_float(atoms) + atom_norm = 1.0 / atoms global_loss *= atom_norm l2_loss += self.global_weight * global_loss @@ -125,10 +126,11 @@ def build(self, learning_rate, natoms, model_dict, label_dict, suffix): self.l2_more = more_loss self.l2_l = l2_loss - self.l2_loss_summary = tf.summary.scalar("l2_loss_" + suffix, tf.sqrt(l2_loss)) + # self.l2_loss_summary = paddle.summary.scalar("l2_loss_" + suffix, tf.sqrt(l2_loss)) return l2_loss, more_loss - def eval(self, sess, feed_dict, natoms): + + def eval(self, model, batch_data, natoms): atoms = 0 if self.type_sel is not None: for w in self.type_sel: @@ -136,8 +138,56 @@ def eval(self, sess, feed_dict, natoms): else: atoms = natoms[0] - run_data = [self.l2_l, self.l2_more["local_loss"], self.l2_more["global_loss"]] - error, error_lc, error_gl = run_sess(sess, run_data, feed_dict=feed_dict) + model_inputs = {} + for kk in batch_data.keys(): + if kk == "find_type" or kk == "type": + continue + prec = "float64" + if "find_" in kk: + model_inputs[kk] = paddle.to_tensor(batch_data[kk], dtype="float64") + else: + model_inputs[kk] = paddle.to_tensor( + np.reshape(batch_data[kk], [-1]), dtype=prec + ) + + for ii in ["type"]: + model_inputs[ii] = paddle.to_tensor( + np.reshape(batch_data[ii], [-1]), dtype="int32" + ) + for ii in ["natoms_vec", "default_mesh"]: + model_inputs[ii] = paddle.to_tensor(batch_data[ii], dtype="int32") + model_inputs["is_training"] = paddle.to_tensor(False) + model_inputs["natoms_vec"] = paddle.to_tensor( + model_inputs["natoms_vec"], place="cpu" + ) + + model_pred = model( + model_inputs["coord"], + model_inputs["type"], + model_inputs["natoms_vec"], + model_inputs["box"], + model_inputs["default_mesh"], + model_inputs, + suffix="", + reuse=False, + ) + + l2_l, l2_more = self.compute_loss( + # 0.0, natoms, model_dict, batch_data + 0.0, + model_inputs["natoms_vec"], + model_pred, + model_inputs, + suffix="test", + ) + # pdb.set_trace() + + run_data = [ + (float(l2_l)), + (float(l2_more["local_loss"])), + (float(l2_more["global_loss"])), + ] + error, error_lc, error_gl = run_data results = {"natoms": atoms, "rmse": np.sqrt(error)} if self.local_weight > 0.0: diff --git a/deepmd/model/tensor.py b/deepmd/model/tensor.py index bff3af526f..8fd7a6c18c 100644 --- a/deepmd/model/tensor.py +++ b/deepmd/model/tensor.py @@ -1,7 +1,9 @@ from typing import List from typing import Optional +import pdb from deepmd.env import MODEL_VERSION +from deepmd.env import paddle from deepmd.env import tf from .model import Model @@ -9,7 +11,7 @@ from .model_stat import merge_sys_stat -class TensorModel(Model): +class TensorModel(Model, paddle.nn.Layer): """Tensor model. Parameters @@ -41,6 +43,7 @@ def __init__( data_stat_nbatch: int = 10, data_stat_protect: float = 1e-2, ) -> None: + super().__init__() """Constructor.""" self.model_type = tensor_name # descriptor @@ -96,7 +99,7 @@ def _compute_output_stat(self, all_stat): if hasattr(self.fitting, "compute_output_stats"): self.fitting.compute_output_stats(all_stat) - def build( + def forward( self, coord_, atype_, @@ -111,63 +114,66 @@ def build( ): if input_dict is None: input_dict = {} - with tf.variable_scope("model_attr" + suffix, reuse=reuse): - t_tmap = tf.constant(" ".join(self.type_map), name="tmap", dtype=tf.string) - t_st = tf.constant(self.get_sel_type(), name="sel_type", dtype=tf.int32) - t_mt = tf.constant(self.model_type, name="model_type", dtype=tf.string) - t_ver = tf.constant(MODEL_VERSION, name="model_version", dtype=tf.string) - t_od = tf.constant(self.get_out_size(), name="output_dim", dtype=tf.int32) + # with tf.variable_scope("model_attr" + suffix, reuse=reuse): + # t_tmap = tf.constant(" ".join(self.type_map), name="tmap", dtype=tf.string) + # t_st = tf.constant(self.get_sel_type(), name="sel_type", dtype=tf.int32) + # t_mt = tf.constant(self.model_type, name="model_type", dtype=tf.string) + # t_ver = tf.constant(MODEL_VERSION, name="model_version", dtype=tf.string) + # t_od = tf.constant(self.get_out_size(), name="output_dim", dtype=tf.int32) - natomsel = sum(natoms[2 + type_i] for type_i in self.get_sel_type()) - nout = self.get_out_size() + natomsel = sum(natoms[2 + type_i] for type_i in self.get_sel_type())#n_atom_selected + nout = self.get_out_size() # 3 - coord = tf.reshape(coord_, [-1, natoms[1] * 3]) - atype = tf.reshape(atype_, [-1, natoms[1]]) - input_dict["nframes"] = tf.shape(coord)[0] + coord = paddle.reshape(coord_, [-1, natoms[1] * 3]) + atype = paddle.reshape(atype_, [-1, natoms[1]]) + # input_dict["nframes"] = paddle.shape(coord)[0]# 推理模型导出的时候注释掉这里,否则会报错 # type embedding if any - if self.typeebd is not None: - type_embedding = self.typeebd.build( - self.ntypes, - reuse=reuse, - suffix=suffix, - ) - input_dict["type_embedding"] = type_embedding - input_dict["atype"] = atype_ - - dout = self.build_descrpt( + # if self.typeebd is not None: + # type_embedding = self.typeebd.build( + # self.ntypes, + # reuse=reuse, + # suffix=suffix, + # ) + # input_dict["type_embedding"] = type_embedding + input_dict["atype"] = atype_ + + dout = self.descrpt( coord, atype, natoms, box, mesh, input_dict, - frz_model=frz_model, - ckpt_meta=ckpt_meta, + # frz_model=frz_model, + # ckpt_meta=ckpt_meta, suffix=suffix, reuse=reuse, ) rot_mat = self.descrpt.get_rot_mat() - rot_mat = tf.identity(rot_mat, name="o_rot_mat" + suffix) + rot_mat = paddle.clone(rot_mat, name="o_rot_mat" + suffix) + # rot_mat = paddle.fluid.layers.assign(rot_mat, name="o_rot_mat" + suffix) + # rot_mat = paddle.tensor.clone(rot_mat, name="o_rot_mat" + suffix) - output = self.fitting.build( + output = self.fitting( dout, rot_mat, natoms, input_dict, reuse=reuse, suffix=suffix ) - framesize = nout if "global" in self.model_type else natomsel * nout - output = tf.reshape( + + framesize = nout if "global" in self.model_type else natomsel * nout + output = paddle.reshape( output, [-1, framesize], name="o_" + self.model_type + suffix ) model_dict = {self.model_type: output} if "global" not in self.model_type: - gname = "global_" + self.model_type - atom_out = tf.reshape(output, [-1, natomsel, nout]) - global_out = tf.reduce_sum(atom_out, axis=1) - global_out = tf.reshape(global_out, [-1, nout], name="o_" + gname + suffix) + gname = "global_" + self.model_type # "global_dipole" + atom_out = paddle.reshape(output, [-1, natomsel, nout])# nout=3 + global_out = paddle.sum(atom_out, axis=1) + global_out = paddle.reshape(global_out, [-1, nout], name="o_" + gname + suffix) - out_cpnts = tf.split(atom_out, nout, axis=-1) + out_cpnts = paddle.split(atom_out, nout, axis=-1) force_cpnts = [] virial_cpnts = [] atom_virial_cpnts = [] @@ -176,20 +182,20 @@ def build( force_i, virial_i, atom_virial_i = self.descrpt.prod_force_virial( out_i, natoms ) - force_cpnts.append(tf.reshape(force_i, [-1, 3 * natoms[1]])) - virial_cpnts.append(tf.reshape(virial_i, [-1, 9])) - atom_virial_cpnts.append(tf.reshape(atom_virial_i, [-1, 9 * natoms[1]])) + force_cpnts.append(paddle.reshape(force_i, [-1, 3 * natoms[1]])) + virial_cpnts.append(paddle.reshape(virial_i, [-1, 9])) + atom_virial_cpnts.append(paddle.reshape(atom_virial_i, [-1, 9 * natoms[1]])) # [nframe x nout x (natom x 3)] - force = tf.concat(force_cpnts, axis=1, name="o_force" + suffix) + force = paddle.concat(force_cpnts, axis=1, name="o_force" + suffix) # [nframe x nout x 9] - virial = tf.concat(virial_cpnts, axis=1, name="o_virial" + suffix) + virial = paddle.concat(virial_cpnts, axis=1, name="o_virial" + suffix) # [nframe x nout x (natom x 9)] - atom_virial = tf.concat( + atom_virial = paddle.concat( atom_virial_cpnts, axis=1, name="o_atom_virial" + suffix ) - model_dict[gname] = global_out + model_dict[gname] = global_out # "global_dipole":global_out model_dict["force"] = force model_dict["virial"] = virial model_dict["atom_virial"] = atom_virial diff --git a/deepmd/train/trainer.py b/deepmd/train/trainer.py index 02bfaf4474..5ed9898c4e 100644 --- a/deepmd/train/trainer.py +++ b/deepmd/train/trainer.py @@ -29,6 +29,7 @@ from deepmd.env import tfv2 from deepmd.fit import Fitting from deepmd.fit import ener +from deepmd.fit import dipole from deepmd.loss import DOSLoss from deepmd.loss import EnerDipoleLoss from deepmd.loss import EnerSpinLoss @@ -136,7 +137,12 @@ def _init_param(self, jdata): if fitting_type == "ener": fitting_param["spin"] = self.spin fitting_param.pop("type") - self.fitting = ener.EnerFitting(**fitting_param) + self.fitting = ener.EnerFitting(**fitting_param) + elif fitting_type == "dipole": + fitting_param.pop("type") + self.fitting = dipole.DipoleFittingSeA(**fitting_param) + else: + pass else: self.fitting_dict = {} self.fitting_type_dict = {} @@ -798,6 +804,8 @@ def train(self, train_data=None, valid_data=None, stop_batch: int = 10): cur_batch = self.global_step is_first_step = True self.cur_batch = cur_batch + import pdb + # pdb.set_trace() self.optimizer = paddle.optimizer.Adam( learning_rate=self.learning_rate, parameters=self.model.parameters() ) From af971cb4263fdfe0359503d6b9a903b10ac80996 Mon Sep 17 00:00:00 2001 From: xusuyong <2209245477@qq.com> Date: Wed, 13 Dec 2023 10:57:45 +0800 Subject: [PATCH 2/3] fix --- deepmd/fit/dipole.py | 58 +++++++++++++++++++++---------------- deepmd/infer/deep_eval.py | 11 ++++--- deepmd/infer/deep_tensor.py | 25 ++++++++-------- deepmd/loss/tensor.py | 10 +++---- deepmd/model/tensor.py | 23 +++++++++------ deepmd/train/trainer.py | 3 +- 6 files changed, 72 insertions(+), 58 deletions(-) diff --git a/deepmd/fit/dipole.py b/deepmd/fit/dipole.py index 2ef9de14e2..0d050b5da8 100644 --- a/deepmd/fit/dipole.py +++ b/deepmd/fit/dipole.py @@ -1,7 +1,6 @@ import logging from typing import List from typing import Optional -import pdb import numpy as np from paddle import nn @@ -16,9 +15,7 @@ from deepmd.env import paddle from deepmd.env import tf from deepmd.fit.fitting import Fitting -from deepmd.utils.graph import get_fitting_net_variables_from_graph_def -from deepmd.utils.network import one_layer -from deepmd.utils.network import one_layer_rand_seed_shift + # from deepmd.infer import DeepPotential from deepmd.nvnmd.fit.ener import one_layer_nvnmd from deepmd.nvnmd.utils.config import nvnmd_cfg @@ -26,10 +23,12 @@ from deepmd.utils.graph import get_fitting_net_variables_from_graph_def from deepmd.utils.graph import get_tensor_by_name_from_graph from deepmd.utils.network import OneLayer as OneLayer_deepmd +from deepmd.utils.network import one_layer from deepmd.utils.network import one_layer as one_layer_deepmd from deepmd.utils.network import one_layer_rand_seed_shift from deepmd.utils.spin import Spin + # @Fitting.register("dipole") class DipoleFittingSeA(nn.Layer): r"""Fit the atomic dipole with descriptor se_a. @@ -68,7 +67,7 @@ def __init__( ) -> None: super().__init__(name_scope="DipoleFittingSeA") """Constructor.""" - self.ntypes = descrpt.get_ntypes()#2 + self.ntypes = descrpt.get_ntypes() # 2 self.dim_descrpt = descrpt.get_dim_out() self.n_neuron = neuron self.resnet_dt = resnet_dt @@ -146,9 +145,16 @@ def get_sel_type(self) -> int: def get_out_size(self) -> int: """Get the output size. Should be 3.""" return 3 - - def _build_lower(self, start_index, natoms, inputs, rot_mat, suffix="", reuse=None, - type_i=None, + + def _build_lower( + self, + start_index, + natoms, + inputs, + rot_mat, + suffix="", + reuse=None, + type_i=None, ): # cut-out inputs inputs_i = paddle.slice( @@ -156,14 +162,14 @@ def _build_lower(self, start_index, natoms, inputs, rot_mat, suffix="", reuse=No [0, 1, 2], [0, start_index, 0], [inputs.shape[0], start_index + natoms, inputs.shape[2]], - ) + ) inputs_i = paddle.reshape(inputs_i, [-1, self.dim_descrpt]) rot_mat_i = paddle.slice( rot_mat, [0, 1, 2], [0, start_index, 0], [rot_mat.shape[0], start_index + natoms, rot_mat.shape[2]], - ) + ) # paddle.slice(rot_mat, [0, start_index, 0], [-1, natoms, -1]) rot_mat_i = paddle.reshape(rot_mat_i, [-1, self.dim_rot_mat_1, 3]) layer = inputs_i @@ -171,7 +177,7 @@ def _build_lower(self, start_index, natoms, inputs, rot_mat, suffix="", reuse=No if ii >= 1 and self.n_neuron[ii] == self.n_neuron[ii - 1]: layer += self.one_layers[type_i][ii](layer) else: - layer = self.one_layers[type_i][ii](layer) + layer = self.one_layers[type_i][ii](layer) # if ii >= 1 and self.n_neuron[ii] == self.n_neuron[ii - 1]: # layer += one_layer( # layer, @@ -224,14 +230,12 @@ def _build_lower(self, start_index, natoms, inputs, rot_mat, suffix="", reuse=No # (nframes x natoms) x 1 * naxis final_layer = paddle.reshape( final_layer, [paddle.shape(inputs)[0] * natoms, 1, self.dim_rot_mat_1] - )#natoms=64, self.dim_rot_mat_1=100 + ) # natoms=64, self.dim_rot_mat_1=100 # (nframes x natoms) x 1 x 3(coord) final_layer = paddle.matmul(final_layer, rot_mat_i) # nframes x natoms x 3 final_layer = paddle.reshape(final_layer, [paddle.shape(inputs)[0], natoms, 3]) - # pdb.set_trace() - return final_layer # [1, 64, 3] - + return final_layer # [1, 64, 3] def forward( self, @@ -278,7 +282,8 @@ def forward( if type_embedding is not None: nloc_mask = paddle.reshape( - paddle.tile(paddle.repeat(self.sel_mask, natoms[2:]), [nframes]), [nframes, -1] + paddle.tile(paddle.repeat(self.sel_mask, natoms[2:]), [nframes]), + [nframes, -1], ) atype_nall = paddle.reshape(atype, [-1, natoms[1]]) # (nframes x nloc_masked) @@ -288,7 +293,9 @@ def forward( self.nloc_masked = paddle.shape( paddle.reshape(self.atype_nloc_masked, [nframes, -1]) )[1] - atype_embed = paddle.nn.embedding_lookup(type_embedding, self.atype_nloc_masked) + atype_embed = paddle.nn.embedding_lookup( + type_embedding, self.atype_nloc_masked + ) else: atype_embed = None @@ -297,11 +304,10 @@ def forward( if atype_embed is None: count = 0 outs_list = [] - # pdb.set_trace() - for type_i in range(self.ntypes):#2 + for type_i in range(self.ntypes): # 2 if type_i not in self.sel_type: start_index += natoms[2 + type_i] - continue #sel_type是0,所以就循环了一次 + continue # sel_type是0,所以就循环了一次 final_layer = self._build_lower( start_index, natoms[2 + type_i], @@ -315,12 +321,13 @@ def forward( # concat the results outs_list.append(final_layer) count += 1 - # pdb.set_trace() - outs = paddle.concat(outs_list, axis=1) # [1, 64, 3] + outs = paddle.concat(outs_list, axis=1) # [1, 64, 3] else: inputs = paddle.reshape( - paddle.reshape(inputs, [nframes, natoms[0], self.dim_descrpt])[nloc_mask], + paddle.reshape(inputs, [nframes, natoms[0], self.dim_descrpt])[ + nloc_mask + ], [-1, self.dim_descrpt], ) rot_mat = paddle.reshape( @@ -333,14 +340,15 @@ def forward( type_shape = atype_embed.get_shape().as_list() inputs = paddle.concat([inputs, atype_embed], axis=1) self.dim_descrpt = self.dim_descrpt + type_shape[1] - inputs = paddle.reshape(inputs, [nframes, self.nloc_masked, self.dim_descrpt]) + inputs = paddle.reshape( + inputs, [nframes, self.nloc_masked, self.dim_descrpt] + ) rot_mat = paddle.reshape( rot_mat, [nframes, self.nloc_masked, self.dim_rot_mat_1 * 3] ) final_layer = self._build_lower( 0, self.nloc_masked, inputs, rot_mat, suffix=suffix, reuse=reuse ) - pdb.set_trace() # nframes x natoms x 3 outs = paddle.reshape(final_layer, [nframes, self.nloc_masked, 3]) diff --git a/deepmd/infer/deep_eval.py b/deepmd/infer/deep_eval.py index a8faee32b9..8a524a684d 100644 --- a/deepmd/infer/deep_eval.py +++ b/deepmd/infer/deep_eval.py @@ -1,3 +1,4 @@ +import os from functools import lru_cache from typing import TYPE_CHECKING from typing import List @@ -8,7 +9,7 @@ # Descriptor, # ) import numpy as np -import os + import deepmd from deepmd.common import data_requirement from deepmd.common import expand_sys_str @@ -19,10 +20,10 @@ from deepmd.env import default_tf_session_config from deepmd.env import paddle from deepmd.env import tf -from deepmd.fit import ener from deepmd.fit import dipole -from deepmd.model import EnerModel +from deepmd.fit import ener from deepmd.model import DipoleModel +from deepmd.model import EnerModel from deepmd.utils.argcheck import type_embedding_args from deepmd.utils.batch_size import AutoBatchSize from deepmd.utils.sess import run_sess @@ -79,7 +80,9 @@ def __init__( default_tf_graph: bool = False, auto_batch_size: Union[bool, int, AutoBatchSize] = False, ): - jdata = j_loader("input.json" if os.path.isfile("input.json") else "dipole_input.json") + jdata = j_loader( + "input.json" if os.path.isfile("input.json") else "dipole_input.json" + ) remove_comment_in_json(jdata) model_param = j_must_have(jdata, "model") self.multi_task_mode = "fitting_net_dict" in model_param diff --git a/deepmd/infer/deep_tensor.py b/deepmd/infer/deep_tensor.py index a2653cacd1..9d780cd64b 100644 --- a/deepmd/infer/deep_tensor.py +++ b/deepmd/infer/deep_tensor.py @@ -5,7 +5,6 @@ from typing import Tuple from typing import Union - import numpy as np from deepmd.common import make_default_mesh @@ -88,7 +87,7 @@ def __init__( # self._run_default_sess() self.ntypes = int(self.model.descrpt.buffer_ntypes) - self.tselt=self.model.fitting.sel_type + self.tselt = self.model.fitting.sel_type # self.tmap = self.tmap.decode("UTF-8").split() def _run_default_sess(self): @@ -126,7 +125,7 @@ def get_dim_fparam(self) -> int: def get_dim_aparam(self) -> int: """Get the number (dimension) of atomic parameters of this DP.""" return self.daparam - + def _eval_func(self, inner_func: Callable, numb_test: int, natoms: int) -> Callable: """Wrapper method with auto batch size. @@ -154,7 +153,7 @@ def eval_func(*args, **kwargs): else: eval_func = inner_func return eval_func - + def _get_natoms_and_nframes( self, coords: np.ndarray, @@ -168,6 +167,7 @@ def _get_natoms_and_nframes( coords = np.reshape(np.array(coords), [-1, natoms * 3]) nframes = coords.shape[0] return natoms, nframes + def _prepare_feed_dict( self, coords, @@ -275,6 +275,7 @@ def _prepare_feed_dict( # if self.has_aparam: # feed_dict_test[self.t_aparam] = np.reshape(aparam, [-1]) return None, None, natoms_vec + def _eval_inner( self, coords, @@ -309,8 +310,7 @@ def _eval_inner( natoms_vec, dtype="int32", place="cpu" ) eval_inputs["box"] = paddle.to_tensor(np.reshape(cells, [-1]), dtype="float64") - import pdb - # pdb.set_trace() + # if self.has_fparam: # eval_inputs["fparam"] = paddle.to_tensor( # np.reshape(fparam, [-1], dtype="float64") @@ -324,7 +324,7 @@ def _eval_inner( # make_default_mesh(cells), dtype="int32" # ) # else: - eval_inputs['default_mesh'] = paddle.to_tensor(np.array([], dtype = np.int32)) + eval_inputs["default_mesh"] = paddle.to_tensor(np.array([], dtype=np.int32)) if hasattr(self, "st_model"): # NOTE: 使用静态图模型推理 @@ -348,9 +348,9 @@ def _eval_inner( # NOTE: 使用动态图模型推理 eval_outputs = self.model( eval_inputs["coord"], # - eval_inputs["type"], # - eval_inputs["natoms_vec"], # - eval_inputs["box"], # + eval_inputs["type"], # + eval_inputs["natoms_vec"], # + eval_inputs["box"], # eval_inputs["default_mesh"], # eval_inputs, suffix="", @@ -360,14 +360,13 @@ def _eval_inner( return dipole - # if atomic: # ae = eval_outputs["atom_ener"].numpy() # av = eval_outputs["atom_virial"].numpy() # return energy, force, virial, ae, av # else: # return energy, force, virial - + def eval( self, coords: np.ndarray, @@ -454,7 +453,7 @@ def eval( # output = tuple(output) return output - + def eval_full( self, coords: np.ndarray, diff --git a/deepmd/loss/tensor.py b/deepmd/loss/tensor.py index ff5e49b996..ad0ba97b16 100644 --- a/deepmd/loss/tensor.py +++ b/deepmd/loss/tensor.py @@ -77,7 +77,8 @@ def compute_loss(self, learning_rate, natoms, model_dict, label_dict, suffix): if self.local_weight > 0.0: local_loss = find_atomic * paddle.mean( - paddle.square(self.scale * (polar - atomic_polar_hat)), name="l2_" + suffix + paddle.square(self.scale * (polar - atomic_polar_hat)), + name="l2_" + suffix, ) more_loss["local_loss"] = local_loss l2_loss += self.local_weight * local_loss @@ -107,7 +108,8 @@ def compute_loss(self, learning_rate, natoms, model_dict, label_dict, suffix): # global_polar_hat = polar_hat global_loss = find_global * paddle.mean( - paddle.square(self.scale * (global_polar - polar_hat)), name="l2_" + suffix + paddle.square(self.scale * (global_polar - polar_hat)), + name="l2_" + suffix, ) more_loss["global_loss"] = global_loss @@ -129,7 +131,6 @@ def compute_loss(self, learning_rate, natoms, model_dict, label_dict, suffix): # self.l2_loss_summary = paddle.summary.scalar("l2_loss_" + suffix, tf.sqrt(l2_loss)) return l2_loss, more_loss - def eval(self, model, batch_data, natoms): atoms = 0 if self.type_sel is not None: @@ -171,7 +172,7 @@ def eval(self, model, batch_data, natoms): suffix="", reuse=False, ) - + l2_l, l2_more = self.compute_loss( # 0.0, natoms, model_dict, batch_data 0.0, @@ -180,7 +181,6 @@ def eval(self, model, batch_data, natoms): model_inputs, suffix="test", ) - # pdb.set_trace() run_data = [ (float(l2_l)), diff --git a/deepmd/model/tensor.py b/deepmd/model/tensor.py index 8fd7a6c18c..240bc99f8a 100644 --- a/deepmd/model/tensor.py +++ b/deepmd/model/tensor.py @@ -1,6 +1,5 @@ from typing import List from typing import Optional -import pdb from deepmd.env import MODEL_VERSION from deepmd.env import paddle @@ -121,8 +120,10 @@ def forward( # t_ver = tf.constant(MODEL_VERSION, name="model_version", dtype=tf.string) # t_od = tf.constant(self.get_out_size(), name="output_dim", dtype=tf.int32) - natomsel = sum(natoms[2 + type_i] for type_i in self.get_sel_type())#n_atom_selected - nout = self.get_out_size() # 3 + natomsel = sum( + natoms[2 + type_i] for type_i in self.get_sel_type() + ) # n_atom_selected + nout = self.get_out_size() # 3 coord = paddle.reshape(coord_, [-1, natoms[1] * 3]) atype = paddle.reshape(atype_, [-1, natoms[1]]) @@ -160,7 +161,7 @@ def forward( dout, rot_mat, natoms, input_dict, reuse=reuse, suffix=suffix ) - framesize = nout if "global" in self.model_type else natomsel * nout + framesize = nout if "global" in self.model_type else natomsel * nout output = paddle.reshape( output, [-1, framesize], name="o_" + self.model_type + suffix ) @@ -168,10 +169,12 @@ def forward( model_dict = {self.model_type: output} if "global" not in self.model_type: - gname = "global_" + self.model_type # "global_dipole" - atom_out = paddle.reshape(output, [-1, natomsel, nout])# nout=3 + gname = "global_" + self.model_type # "global_dipole" + atom_out = paddle.reshape(output, [-1, natomsel, nout]) # nout=3 global_out = paddle.sum(atom_out, axis=1) - global_out = paddle.reshape(global_out, [-1, nout], name="o_" + gname + suffix) + global_out = paddle.reshape( + global_out, [-1, nout], name="o_" + gname + suffix + ) out_cpnts = paddle.split(atom_out, nout, axis=-1) force_cpnts = [] @@ -184,7 +187,9 @@ def forward( ) force_cpnts.append(paddle.reshape(force_i, [-1, 3 * natoms[1]])) virial_cpnts.append(paddle.reshape(virial_i, [-1, 9])) - atom_virial_cpnts.append(paddle.reshape(atom_virial_i, [-1, 9 * natoms[1]])) + atom_virial_cpnts.append( + paddle.reshape(atom_virial_i, [-1, 9 * natoms[1]]) + ) # [nframe x nout x (natom x 3)] force = paddle.concat(force_cpnts, axis=1, name="o_force" + suffix) @@ -195,7 +200,7 @@ def forward( atom_virial_cpnts, axis=1, name="o_atom_virial" + suffix ) - model_dict[gname] = global_out # "global_dipole":global_out + model_dict[gname] = global_out model_dict["force"] = force model_dict["virial"] = virial model_dict["atom_virial"] = atom_virial diff --git a/deepmd/train/trainer.py b/deepmd/train/trainer.py index 5ed9898c4e..a51d0c7182 100644 --- a/deepmd/train/trainer.py +++ b/deepmd/train/trainer.py @@ -804,8 +804,7 @@ def train(self, train_data=None, valid_data=None, stop_batch: int = 10): cur_batch = self.global_step is_first_step = True self.cur_batch = cur_batch - import pdb - # pdb.set_trace() + self.optimizer = paddle.optimizer.Adam( learning_rate=self.learning_rate, parameters=self.model.parameters() ) From 0cb4fdea91ebee49d959347647d91af035331333 Mon Sep 17 00:00:00 2001 From: xusuyong <2209245477@qq.com> Date: Wed, 13 Dec 2023 20:06:28 +0800 Subject: [PATCH 3/3] fix --- deepmd/entrypoints/test.py | 4 +-- deepmd/fit/dipole.py | 66 +++++++------------------------------- deepmd/infer/deep_eval.py | 5 ++- deepmd/loss/tensor.py | 1 - deepmd/model/tensor.py | 4 +-- deepmd/train/trainer.py | 3 +- 6 files changed, 17 insertions(+), 66 deletions(-) diff --git a/deepmd/entrypoints/test.py b/deepmd/entrypoints/test.py index 5f5bce6256..761862c330 100644 --- a/deepmd/entrypoints/test.py +++ b/deepmd/entrypoints/test.py @@ -890,12 +890,12 @@ def test_dipole( atomic=atomic, must=True, high_prec=False, - type_sel=dp.tselt, + type_sel=dp.get_sel_type(), ) test_data = data.get_test() dipole, numb_test, atype = run_test(dp, test_data, numb_test, data) - sel_type = dp.tselt + sel_type = dp.get_sel_type() sel_natoms = 0 for ii in sel_type: sel_natoms += sum(atype == ii) diff --git a/deepmd/fit/dipole.py b/deepmd/fit/dipole.py index 0d050b5da8..66eec26c49 100644 --- a/deepmd/fit/dipole.py +++ b/deepmd/fit/dipole.py @@ -67,7 +67,7 @@ def __init__( ) -> None: super().__init__(name_scope="DipoleFittingSeA") """Constructor.""" - self.ntypes = descrpt.get_ntypes() # 2 + self.ntypes = descrpt.get_ntypes() self.dim_descrpt = descrpt.get_dim_out() self.n_neuron = neuron self.resnet_dt = resnet_dt @@ -170,7 +170,6 @@ def _build_lower( [0, start_index, 0], [rot_mat.shape[0], start_index + natoms, rot_mat.shape[2]], ) - # paddle.slice(rot_mat, [0, start_index, 0], [-1, natoms, -1]) rot_mat_i = paddle.reshape(rot_mat_i, [-1, self.dim_rot_mat_1, 3]) layer = inputs_i for ii in range(0, len(self.n_neuron)): @@ -178,64 +177,25 @@ def _build_lower( layer += self.one_layers[type_i][ii](layer) else: layer = self.one_layers[type_i][ii](layer) - # if ii >= 1 and self.n_neuron[ii] == self.n_neuron[ii - 1]: - # layer += one_layer( - # layer, - # self.n_neuron[ii], - # name="layer_" + str(ii) + suffix, - # reuse=reuse, - # seed=self.seed, - # use_timestep=self.resnet_dt, - # activation_fn=self.fitting_activation_fn, - # precision=self.fitting_precision, - # uniform_seed=self.uniform_seed, - # initial_variables=self.fitting_net_variables, - # mixed_prec=self.mixed_prec, - # ) - # else: - # layer = one_layer( - # layer, - # self.n_neuron[ii], - # name="layer_" + str(ii) + suffix, - # reuse=reuse, - # seed=self.seed, - # activation_fn=self.fitting_activation_fn, - # precision=self.fitting_precision, - # uniform_seed=self.uniform_seed, - # initial_variables=self.fitting_net_variables, - # mixed_prec=self.mixed_prec, - # ) + if (not self.uniform_seed) and (self.seed is not None): self.seed += self.seed_shift - + # (nframes x natoms) x naxis final_layer = self.final_layers[type_i]( layer, ) - # # (nframes x natoms) x naxis - # final_layer = one_layer( - # layer, - # self.dim_rot_mat_1, - # activation_fn=None, - # name="final_layer" + suffix, - # reuse=reuse, - # seed=self.seed, - # precision=self.fitting_precision, - # uniform_seed=self.uniform_seed, - # initial_variables=self.fitting_net_variables, - # mixed_prec=self.mixed_prec, - # final_layer=True, - # ) + if (not self.uniform_seed) and (self.seed is not None): self.seed += self.seed_shift # (nframes x natoms) x 1 * naxis final_layer = paddle.reshape( final_layer, [paddle.shape(inputs)[0] * natoms, 1, self.dim_rot_mat_1] - ) # natoms=64, self.dim_rot_mat_1=100 + ) # (nframes x natoms) x 1 x 3(coord) final_layer = paddle.matmul(final_layer, rot_mat_i) # nframes x natoms x 3 final_layer = paddle.reshape(final_layer, [paddle.shape(inputs)[0], natoms, 3]) - return final_layer # [1, 64, 3] + return final_layer def forward( self, @@ -282,7 +242,7 @@ def forward( if type_embedding is not None: nloc_mask = paddle.reshape( - paddle.tile(paddle.repeat(self.sel_mask, natoms[2:]), [nframes]), + paddle.tile(paddle.repeat_interleave(self.sel_mask, natoms[2:]), [nframes]), [nframes, -1], ) atype_nall = paddle.reshape(atype, [-1, natoms[1]]) @@ -293,7 +253,7 @@ def forward( self.nloc_masked = paddle.shape( paddle.reshape(self.atype_nloc_masked, [nframes, -1]) )[1] - atype_embed = paddle.nn.embedding_lookup( + atype_embed = nn.embedding_lookup( type_embedding, self.atype_nloc_masked ) else: @@ -304,10 +264,10 @@ def forward( if atype_embed is None: count = 0 outs_list = [] - for type_i in range(self.ntypes): # 2 + for type_i in range(self.ntypes): if type_i not in self.sel_type: start_index += natoms[2 + type_i] - continue # sel_type是0,所以就循环了一次 + continue final_layer = self._build_lower( start_index, natoms[2 + type_i], @@ -321,8 +281,7 @@ def forward( # concat the results outs_list.append(final_layer) count += 1 - - outs = paddle.concat(outs_list, axis=1) # [1, 64, 3] + outs = paddle.concat(outs_list, axis=1) else: inputs = paddle.reshape( paddle.reshape(inputs, [nframes, natoms[0], self.dim_descrpt])[ @@ -349,13 +308,10 @@ def forward( final_layer = self._build_lower( 0, self.nloc_masked, inputs, rot_mat, suffix=suffix, reuse=reuse ) - # nframes x natoms x 3 outs = paddle.reshape(final_layer, [nframes, self.nloc_masked, 3]) - # paddle.summary.histogram("fitting_net_output", outs) return paddle.reshape(outs, [-1]) - # return tf.reshape(outs, [tf.shape(inputs)[0] * natoms[0] * 3 // 3]) def init_variables( self, diff --git a/deepmd/infer/deep_eval.py b/deepmd/infer/deep_eval.py index 8a524a684d..38d0b6d166 100644 --- a/deepmd/infer/deep_eval.py +++ b/deepmd/infer/deep_eval.py @@ -81,7 +81,7 @@ def __init__( auto_batch_size: Union[bool, int, AutoBatchSize] = False, ): jdata = j_loader( - "input.json" if os.path.isfile("input.json") else "dipole_input.json" + "input.json" if os.path.exists("input.json") else "dipole_input.json" ) remove_comment_in_json(jdata) model_param = j_must_have(jdata, "model") @@ -150,7 +150,7 @@ def __init__( fitting_param.pop("type", None) fitting = dipole.DipoleFittingSeA(**fitting_param) else: - pass + raise NotImplementedError() else: self.fitting_dict = {} self.fitting_type_dict = {} @@ -361,7 +361,6 @@ def __init__( @property @lru_cache(maxsize=None) def model_type(self) -> str: - return self.model.model_type """Get type of model. diff --git a/deepmd/loss/tensor.py b/deepmd/loss/tensor.py index ad0ba97b16..68dcdcb40c 100644 --- a/deepmd/loss/tensor.py +++ b/deepmd/loss/tensor.py @@ -174,7 +174,6 @@ def eval(self, model, batch_data, natoms): ) l2_l, l2_more = self.compute_loss( - # 0.0, natoms, model_dict, batch_data 0.0, model_inputs["natoms_vec"], model_pred, diff --git a/deepmd/model/tensor.py b/deepmd/model/tensor.py index 240bc99f8a..c04912c6fe 100644 --- a/deepmd/model/tensor.py +++ b/deepmd/model/tensor.py @@ -154,9 +154,7 @@ def forward( rot_mat = self.descrpt.get_rot_mat() rot_mat = paddle.clone(rot_mat, name="o_rot_mat" + suffix) - # rot_mat = paddle.fluid.layers.assign(rot_mat, name="o_rot_mat" + suffix) - # rot_mat = paddle.tensor.clone(rot_mat, name="o_rot_mat" + suffix) - + output = self.fitting( dout, rot_mat, natoms, input_dict, reuse=reuse, suffix=suffix ) diff --git a/deepmd/train/trainer.py b/deepmd/train/trainer.py index a51d0c7182..3424aa95c8 100644 --- a/deepmd/train/trainer.py +++ b/deepmd/train/trainer.py @@ -142,7 +142,7 @@ def _init_param(self, jdata): fitting_param.pop("type") self.fitting = dipole.DipoleFittingSeA(**fitting_param) else: - pass + raise NotImplementedError else: self.fitting_dict = {} self.fitting_type_dict = {} @@ -804,7 +804,6 @@ def train(self, train_data=None, valid_data=None, stop_batch: int = 10): cur_batch = self.global_step is_first_step = True self.cur_batch = cur_batch - self.optimizer = paddle.optimizer.Adam( learning_rate=self.learning_rate, parameters=self.model.parameters() )