aiqm / torchani

Accurate Neural Network Potential on PyTorch
https://aiqm.github.io/torchani/
MIT License
464 stars 128 forks source link

An error with nnp_traing.py #467

Closed hjh19970102 closed 4 years ago

hjh19970102 commented 4 years ago

Hi, Dear developers,when I run the nn_training.py that you supplied in the content "examples" and get this error. IndexError: tensors used as indices must be long, byte or bool tensors. Here are the output: Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information.

IPython 7.4.0 -- An enhanced Interactive Python.

runfile('C:/Users/john/Desktop/ML/torchani-master/torchani/examples/nnp_training.py', wdir='C:/Users/john/Desktop/ML/torchani-master/torchani/examples') Self atomic energies: tensor([ -0.6078, -38.0497, -54.7041, -75.1696], dtype=torch.float64) ANIModel( (0): Sequential( (0): Linear(in_features=384, out_features=160, bias=True) (1): CELU(alpha=0.1) (2): Linear(in_features=160, out_features=128, bias=True) (3): CELU(alpha=0.1) (4): Linear(in_features=128, out_features=96, bias=True) (5): CELU(alpha=0.1) (6): Linear(in_features=96, out_features=1, bias=True) ) (1): Sequential( (0): Linear(in_features=384, out_features=144, bias=True) (1): CELU(alpha=0.1) (2): Linear(in_features=144, out_features=112, bias=True) (3): CELU(alpha=0.1) (4): Linear(in_features=112, out_features=96, bias=True) (5): CELU(alpha=0.1) (6): Linear(in_features=96, out_features=1, bias=True) ) (2): Sequential( (0): Linear(in_features=384, out_features=128, bias=True) (1): CELU(alpha=0.1) (2): Linear(in_features=128, out_features=112, bias=True) (3): CELU(alpha=0.1) (4): Linear(in_features=112, out_features=96, bias=True) (5): CELU(alpha=0.1) (6): Linear(in_features=96, out_features=1, bias=True) ) (3): Sequential( (0): Linear(in_features=384, out_features=128, bias=True) (1): CELU(alpha=0.1) (2): Linear(in_features=128, out_features=112, bias=True) (3): CELU(alpha=0.1) (4): Linear(in_features=112, out_features=96, bias=True) (5): CELU(alpha=0.1) (6): Linear(in_features=96, out_features=1, bias=True) ) ) training starting from epoch 1 Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/john/Desktop/ML/torchani-master/torchani/examples/nnp_training.py', wdir='C:/Users/john/Desktop/ML/torchani-master/torchani/examples')

File "C:\Users\john\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile execfile(filename, namespace)

File "C:\Users\john\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/john/Desktop/ML/torchani-master/torchani/examples/nnp_training.py", line 289, in rmse = validate()

File "C:/Users/john/Desktop/ML/torchani-master/torchani/examples/nnptraining.py", line 265, in validate , predicted_energies = model((species, coordinates))

File "C:\Users\john\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 541, in call result = self.forward(*input, **kwargs)

File "C:\Users\john\Anaconda3\lib\site-packages\torchani\nn.py", line 100, in forward input = module(input, cell=cell, pbc=pbc)

File "C:\Users\john\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 541, in call result = self.forward(*input, **kwargs)

File "C:\Users\john\Anaconda3\lib\site-packages\torchani\aev.py", line 438, in forward aev = compute_aev(species, coordinates, self.triu_index, self.constants(), self.sizes, None)

File "C:\Users\john\Anaconda3\lib\site-packages\torchani\aev.py", line 309, in compute_aev index = central_atom_index * num_species_pairs + triuindex[species12[0], species12_[1]]

IndexError: tensors used as indices must be long, byte or bool tensors

could you please give me some suggestions to solve that?Thanks a lot.

zasdfgbnm commented 4 years ago

Hi @hjh19970102, could you try upgrading your PyTorch to latest nightly and see if the error is gone? The installation command is at https://pytorch.org/

hjh19970102 commented 4 years ago

I am so sorry,I have tried to upgrad my PyTorch to latest nightly,but still have the same error... Couly you give me some other solutions to solve that?

hjh19970102 commented 4 years ago

@zasdfgbnm

IgnacioJPickering commented 4 years ago

Hello @hjh19970102 , can you show the output to torch.__version__ on your ipython? Is this the plain example or have you introduced some modifications?

IgnacioJPickering commented 4 years ago

If you introduced some modifications it may be that the input to "species" is not a tensor of type torch.long. Try inspecting the type of your species tensor. Right before your call to model((species, coordinates)), can you try to call print(species.dtype) and tell us the output?

hjh19970102 commented 4 years ago

Thank you for your reply. I am so sorry so late reply to you.I have solve that problem according to reinstall my python yesterday. torch.version: 1.6.0.dev20200610+cpu also have one question, I try to train a simple model and get a best.pt file,and when I want to predict energy ,it will need to use something similar to your energy_force.py file,I think that instead of loading the pre-trained ANI1x model, I need to define my own model according to https://pytorch.org/tutorials/beginner/saving_loading_models.html.

But when I substitute "model = torch.load("best.pt")" for "model = torchani.models.ANI1ccx(periodic_table_index=True).to(device)",it didn"t work. My nnp_training.py had the same format as you supplied in the content "examples".

could please give me some guidances for how to use the model that be trained to predict? Or how to describe the model you supplied in nnp_training.py in the content "examples"? I would much appreciate it if you would give me some guidances.Thank so much.

IgnacioJPickering commented 4 years ago

@hjh19970102 currently torchani provides the necessary pieces (modules) for you to build your own model from what you have trained with, but in order to actually assemble the model you will have to do that yourself (i. e. best.pt doesn't really have a full model, only the parameters for the main neural network) Models can be pretty complicated and there are many different things you may want to do with your model. I suggest reading pytorch's documentation, and a couple of the ANI papers and trying to assemble the model from the piece stored in the best.pt and the other static pieces that you probably used (AEVComputer, EnergyShifter, SpeciesConverter, etc) I'm pretty sure you will be able to do that yourself once you understand pytorch a bit better, and maybe with a bit of help of the torchani API documentation. Probably the simplest thing is to wrap all the pieces in a "torhani.nn.Sequential"

nn = torch.load("best.pt")
model = torchani.nn.Sequential(species_converter, aev_computer, nn, energy_shifter)

In this case nn is what is inside best.pt and not the whole model. Maybe checkout the other examples and you will be able to see some things similar to this that will help you if you are trying something different.

I want to add that you should not do this blindly as if this was a black box, only do this if you understand to a certain extent the workings of pytorch and how ANI itself works otherwise you will get unpredictable results from what I just wrote.

hjh19970102 commented 4 years ago

Thank you so much for your guidances,I will go to read pytorch's documentation and some ANI papers to understand better.Thank you.

IgnacioJPickering commented 4 years ago

No problem tell us if you have further issues