Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number #6

Open
cctsou opened this issue Jun 21, 2019 · 4 comments

Comments

@cctsou
Copy link

cctsou commented Jun 21, 2019

I am trying to train a model using same config.py provided from the package but got the error shown below. Do you know what might have gone wrong? Thank you very much

0 -> *
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> A
6 -> C
7 -> D
8 -> E
9 -> F
10 -> G
11 -> H
12 -> I
13 -> K
14 -> L
15 -> M
16 -> N
17 -> P
18 -> Q
19 -> R
20 -> S
21 -> T
22 -> V
23 -> W
24 -> Y

number of aa: 25
note: using seq mode.
note: using >>>embedding<<< method.
DeepRT: using max length defined by user: 50
Read training data done; source: data/mod_train_2.txt
Note: didn't load val data.
Read testing data done; source: data/mod_test_2.txt
0 -> *
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> A
6 -> C
7 -> D
8 -> E
9 -> F
10 -> G
11 -> H
12 -> I
13 -> K
14 -> L
15 -> M
16 -> N
17 -> P
18 -> Q
19 -> R
20 -> S
21 -> T
22 -> V
23 -> W
24 -> Y

parameters: 2869764
0 -> *
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> A
6 -> C
7 -> D
8 -> E
9 -> F
10 -> G
11 -> H
12 -> I
13 -> K
14 -> L
15 -> M
16 -> N
17 -> P
18 -> Q
19 -> R
20 -> S
21 -> T
22 -> V
23 -> W
24 -> Y

number of aa: 25
note: using seq mode.

C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn_reduction.py:46: UserWarning: size_average and reduce args will be deprecated, please use reduction='sum' instead.
warnings.warn(warning.format(ret))
0%| | 0/192 [00:00<?, ?it/s]Traceback (most recent call last):
File "capsule_network_emb.py", line 738, in
engine.train(processor, get_rt_iterator(True), maxepoch=NUM_EPOCHS, optimizer=optimizer)
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torchnet\engine\engine.py", line 63, in train
state['optimizer'].step(closure)
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\optim\adam.py", line 58, in step
loss = closure()
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torchnet\engine\engine.py", line 56, in closure self.hook('on_forward', state)
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torchnet\engine\engine.py", line 31, in hook
self.hooksname
File "capsule_network_emb.py", line 575, in on_forward
meter_loss.add(state['loss'].data[0])
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python `number

@horsepurve
Copy link
Owner

horsepurve commented Jun 21, 2019

I guess you're using pytorch version >= 1.0 which was released after this package was developed. According to this issue could you try to change from

meter_loss.add(state['loss'].data[0])

to

meter_loss.add(state['loss'].data)

thanks.

@cctsou
Copy link
Author

cctsou commented Jun 23, 2019

Thanks, I changed it accordingly, here is the error after that.

raceback (most recent call last):
File "capsule_network_emb.py", line 738, in
engine.train(processor, get_rt_iterator(True), maxepoch=NUM_EPOCHS, optimizer=optimizer)
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torchnet\engine\engine.py", line 63, in train
state['optimizer'].step(closure)
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\optim\adam.py", line 58, in step
loss = closure()
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torchnet\engine\engine.py", line 56, in closure
self.hook('on_forward', state)
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torchnet\engine\engine.py", line 31, in hook
self.hooksname
File "capsule_network_emb.py", line 575, in on_forward
meter_loss.add(state['loss'].data)
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torchnet\meter\averagevaluemeter.py", line 29, in add
self.std = np.sqrt(self.m_s / (self.n - 1.0))
File "C:\Users\cct\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\tensor.py", line 458, in array
return self.numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

@horsepurve
Copy link
Owner

I have tried pytorch version 1.1.0 and it works well without error. Are you using CPU or GPU and what's your pytorch and cuda version? You may check pytorch version in python using

import torch
torch.__version__

and check cuda version in command line using

nvcc --version

you may also try

meter_loss.add(state['loss'].data.cpu())

@cctsou
Copy link
Author

cctsou commented Jun 24, 2019

Thank you very much again, after changing to "meter_loss.add(state['loss'].data.cpu())" as suggested and it seems working.

Here is the pytorch and cuda version I am using here

pytorch: v1.1.0

cuda:
nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:04_Central_Daylight_Time_2018
Cuda compilation tools, release 10.0, V10.0.130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants