deepmodeling / deepmd-kit

A deep learning package for many-body potential energy representation and molecular dynamics
https://docs.deepmodeling.com/projects/deepmd/
GNU Lesser General Public License v3.0
1.51k stars 515 forks source link

[BUG] Cannot convert tensor pt model to tf model #4411

Open ChiahsinChu opened 19 hours ago

ChiahsinChu commented 19 hours ago

Bug summary

I trained a dipole model with pytorch backend. But when I tried to convert it into TensorFlow backed (for further training of the DPLR model), I got an error shown below, which probably comes from the out_bias in the dipole model.

DeePMD-kit Version

DeePMD-kit v3.0.0

Backend and its version

v2.4.1.post302-gUnknown

How did you download the software?

conda

Input Files, Running Commands, Error Log, etc.

Input files: deepmd-kit/examples/water_tensor/dipole/dipole_input_torch.json

Running commands:

dp convert-backend dw_model.pth dw_model.pb

Error Log:

Traceback (most recent call last):
  File "/home/jxzhu/apps/micromamba/2.0.2-2/envs/deepmd-v3.0.0/bin/dp", line 10, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/jxzhu/apps/micromamba/2.0.2-2/envs/deepmd-v3.0.0/lib/python3.12/site-packages/deepmd/main.py", line 927, in main
    deepmd_main(args)
  File "/home/jxzhu/apps/micromamba/2.0.2-2/envs/deepmd-v3.0.0/lib/python3.12/site-packages/deepmd/entrypoints/main.py", line 86, in main
    convert_backend(**dict_args)
  File "/home/jxzhu/apps/micromamba/2.0.2-2/envs/deepmd-v3.0.0/lib/python3.12/site-packages/deepmd/entrypoints/convert_backend.py", line 27, in convert_backend
    out_hook(OUTPUT, data)
  File "/home/jxzhu/apps/micromamba/2.0.2-2/envs/deepmd-v3.0.0/lib/python3.12/site-packages/deepmd/tf/utils/serialization.py", line 77, in deserialize_to_file
    model = Model.deserialize(data["model"])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jxzhu/apps/micromamba/2.0.2-2/envs/deepmd-v3.0.0/lib/python3.12/site-packages/deepmd/tf/model/model.py", line 566, in deserialize
    return Model.get_class_by_type(data.get("type", "standard")).deserialize(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jxzhu/apps/micromamba/2.0.2-2/envs/deepmd-v3.0.0/lib/python3.12/site-packages/deepmd/tf/model/model.py", line 834, in deserialize
    ]["bias_atom_e"] + data["@variables"]["out_bias"].reshape(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot reshape array of size 6 into shape (2,100)

Steps to Reproduce

git clone https://github.com/deepmodeling/deepmd-kit.git deepmd-kit
cd deepmd-kit/examples/water_tensor/dipole
dp --pt train dipole_input_torch.json
dp --pt freeze -o dw_model.pth
dp convert-backend dw_model.pth dw_model.pb

Further Information, Files, and Links

No response

njzjz commented 17 hours ago

The conversation needs more work besides this issue, including #3672 and different implementation for sel_type (see documentation).