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: Dimension out of range (expected to be in range of [-1, 0], but got 1) #6

Open
kkkkyp opened this issue Apr 7, 2023 · 2 comments

Comments

@kkkkyp
Copy link

kkkkyp commented Apr 7, 2023

Traceback (most recent call last):
File "D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py", line 189, in
File "D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py", line 114, in fit
acc_val, loss_val, val_output = predict_(alg, model, label, loss_fn, val)
File "D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py", line 145, in predict_
batch_loss = loss_fn(batch_output, label[batch_edges])
File "D:\Anaconda\envs\GraphSAGE\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Anaconda\envs\GraphSAGE\lib\site-packages\torch\nn\modules\loss.py", line 948, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File "D:\Anaconda\envs\GraphSAGE\lib\site-packages\torch\nn\functional.py", line 2422, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File "D:\Anaconda\envs\GraphSAGE\lib\site-packages\torch\nn\functional.py", line 1591, in log_softmax
ret = input.log_softmax(dim)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Hello, can you help me solve this problem?

@George730
Copy link
Owner

you can add one more dimension to 'input', try input = torch.unsqueeze(input, 0).

@kkkkyp
Copy link
Author

kkkkyp commented Apr 14, 2023

I try chage batch_loss = loss_fn(batch_output, label[batch_edges]) to
if len(batch_output.shape) == 1:
batch_output = torch.unsqueeze(batch_output,0)
batch_loss = loss_fn(torch.tensor(batch_output, dtype=torch.float32),
torch.FloatTensor(torch.tensor(label[np.array(batch_edges)], dtype=torch.float32)))
then, This error has occurred。
D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py:148: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
batch_loss = loss_fn(torch.tensor(batch_output, dtype=torch.float32),
D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py:149: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
torch.FloatTensor(torch.tensor(label[np.array(batch_edges)], dtype=torch.float32)))
Traceback (most recent call last):
File "D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py", line 189, in
fit(args)
File "D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py", line 114, in fit
acc_val, loss_val, val_output = predict_(alg, model, label, loss_fn, val) File "D:/PyCharm/Py_project/E-ResGAT-master/fit_model.py", line 149, in predict_
torch.FloatTensor(torch.tensor(label[np.array(batch_edges)], dtype=torch.float32)))
File "D:\Anaconda\envs\gsage\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Anaconda\envs\gsage\lib\site-packages\torch\nn\modules\loss.py", line 948, in forward
ignore_index=self.ignore_index, reduction=self.reduction)
File "D:\Anaconda\envs\gsage\lib\site-packages\torch\nn\functional.py", line 2422, in cross_entropy
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
File "D:\Anaconda\envs\gsage\lib\site-packages\torch\nn\functional.py", line 2216, in nll_loss
.format(input.size(0), target.size(0)))
ValueError: Expected input batch_size (1) to match target batch_size (500).

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