Problem when updating 0.3.0 code to 1.0.1

I’m trying to upgrade this Mem2Seq repository I’m running the following command
python3 main_train.py -lr=0.001 -layer=1 -hdd=128 -dr=0.2 -dec=Mem2Seq -bsz=8 -ds=kvr -t= top_ptr_i.squeeze()[i].item()

Here is the error code:

File "main_train.py", line 62, in <module> acc = model.evaluate(dev,avg_best, BLEU) File "/content/Mem2Seq/models/Mem2Seq.py", line 281, in evaluate data_dev[2],data_dev[3],data_dev[4],data_dev[5],data_dev[6]) File "/content/Mem2Seq/models/Mem2Seq.py", line 202, in evaluate_batch next_in = [top_ptr_i.squeeze()[i].item() if(int(toppi.squeeze()[i].item()) < input_lengths[i]-1) else topvi.squeeze()[i].item() for i in range(batch_size)] File "/content/Mem2Seq/models/Mem2Seq.py", line 202, in <listcomp> next_in = [top_ptr_i.squeeze()[i].item() if(int(toppi.squeeze()[i].item()) < input_lengths[i]-1) else topvi.squeeze()[i].item() for i in range(batch_size)] IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

The specific error is but IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number. However, I’m finding the error code very odd since as you can see in the stack trace I already converted it to .item(). Anyone have experience with this?

I guess you should remove the indexing in top_ptr_i.squeeze()[i].item() and just call top_ptr_i.squeeze().item().
As I’m not familiar with the code, would that make sense in your use case?

I tried changing but I don’t think it’s right as now it’s saying ValueError: only one element tensors can be converted to Python scalars. It should be noted that the previous code ran fine on 96 of the 98 evaluation examples and only failed on the 97 and also ran fine on all 768 training examples. So originally I thought it might just be a data bug but the original repo authors said it ran fine on all examples in 0.3.0. So I’m just really confused on why it fails on the 97.

I’m not familiar with your code, but it seems the shape changes for some batches.
Could the last batch be smaller (e.g. single sample) than the other ones?
What dimension should squeeze() get rid of?
If you just want to squeeze dim1, you could call .squeeze(1). Otherwise if dim0 and dim1 are both 1, both dimension will be squeezed which might yield this error.