DreamInvoker / GAIN

Source code for EMNLP 2020 paper: Double Graph Based Reasoning for Document-level Relation Extraction
MIT License
142 stars 31 forks source link

TypeError: expected Tensor as element 0 in argument 0, but got str #12

Closed otpas007 closed 3 years ago

otpas007 commented 3 years ago

Hi! I'm trying to train the neural network, using the default values provided in the script run_GAIN_BERT.sh (I just changed the bert_path from ../PLM/bert-base-uncased to bert-base-uncased), but the training script is giving me an error after beginning. The error stack trace is as follows:

Traceback (most recent call last):
  File "train.py", line 233, in <module>
    train(opt)
  File "train.py", line 140, in train
    ht_pair_distance=d['ht_pair_distance']
  File "/home/saptakathaa/nre/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/saptakathaa/GAIN/code/models/GAIN.py", line 306, in forward
    encoder_outputs = torch.cat([encoder_outputs, self.entity_type_emb(params['entity_type'])], dim=-1)
TypeError: expected Tensor as element 0 in argument 0, but got str

Can you please help me in fixing it?

P.S. : The line numbers may vary by 2-3 lines as I have included some print statements to debug it myself.

DreamInvoker commented 3 years ago

Maybe the params['entity_type'] contains a string not a tensor inside it.

otpas007 commented 3 years ago

Maybe the params['entity_type'] contains a string not a tensor inside it.

I have checked the type of params['entity_type']. It is a tensor. Whereas the type of encoder_outputs is string.

Can you please tell me the necessary modifications I have to make to run the code?

DreamInvoker commented 3 years ago

It's weird, encoder_outputs should be a tensor. Have you changed the code?

DreamInvoker commented 3 years ago

encoder_outputs comes from the BERT outputs, are you using transformers == 3.1.0?

otpas007 commented 3 years ago

encoder_outputs comes from the BERT outputs, are you using transformers == 3.1.0?

Yeah, it was a problem relating to the version of the transformers .. I was using a higher version which was causing the issue. Now the code is running fine. Thanks for the help and also great work done with the paper.

DreamInvoker commented 3 years ago

You're welcome!

valid999 commented 1 year ago

You can replace the torch.cat or torch.stack with this problem " TypeError: expected Tensor as element 0 in argument 0, but got float " or "expected Tensor as element 0 in argument 0, but got numpy.float32 " or " TyprError: expected tensor as element 0 in argument 0 , but got int " to torch.tensor and it will work . worked for me .