IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
carlosdelfino opened this issue · comments
Carlos Delfino commented
I'm trying out your code and I followed the instructions to leave the environment compatable, I have the following modules installed:
numpy==1.16.3
opencv-contrib-python==4.1.0.25
opencv-python==4.1.0.25
Pillow==6.0.0
protobuf==3.7.1
pygame==1.9.6
six==1.12.0
tensorboardX==1.6
torch==1.0.1
torchvision==0.2.2.post3
But with minor nuances, as I'm using virtualenv to isolate the installed modules.
When executing your code I get the following message:
$ python train.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
.\workspace\MachineLearning\Flappy-bird-deep-Q-learning-pytorch\src\deep_q_network.py:21: UserWarning: nn.init.uniform is now deprecated in favor of nn.init.uniform_.
nn.init.uniform(m.weight, -0.01, 0.01)
tensor(0)
Traceback (most recent call last):
File "train.py", line 133, in <module>
train(opt)
File "train.py", line 74, in train
action = torch.argmax(prediction)[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
What I suggest to fix this problem, I have not programmed with Python for some years, and I do not know the frameworks used with Python. I have already used OpenCV with pure C / C ++ on board and succeeded.
raytroop commented
action = torch.argmax(prediction)[0]
action = torch.argmax(prediction).item()
Carlos Delfino commented
thanks.