Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Can you help me with this error? IndexError: invalid index of a 0-dim tensor.
Hello, I am getting this Error and I dont know how to fix it. I tried.
This is my code:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable

kwargs = {}
train_data ='data',train=True, download=True,
                                         batch_size=64, shuffle=True, **kwargs)
test_data ='data', train=False,
                                        batch_size=64, shuffle=True, **kwargs)

class Netz(nn.Module):
    def __init__(self):
        super(Netz, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv_dropout = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 60)
        self.fc2 = nn.Linear(60, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.max_pool2d(x, 2)
        x = F.relu(x)
        x = self.conv2(x)
        x = self.conv_dropout(x)
        x = F.max_pool2d(x, 2)
        x = F.relu(x)
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x)

model = Netz()

optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.8)
def train(epoch):
    for batch_id, (data, target) in enumerate(train_data):
        data = Variable(data)
        target = Variable(target)
        out = model(data)
        criterion = F.nll_loss
        loss = criterion(out, target)
        print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(epoch, batch_id * len(data),
                                                                       100. * batch_id / len(train_data),

for epoch in range(1, 30):
/Users/PycharmProjects/Pytorch/venv/ UserWarning: Implicit dimension choice for log_softmax has been deprecated. Change the call to include dim=X as an argument. return F.log_softmax(x) Traceback (most recent call last): File "/Users/PycharmProjects/Pytorch/venv/", line 63, in <module> train(epoch) File "/Users/PycharmProjects/Pytorch/venv/", line 59, in train[0])) IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
I am not familiar with this package, but a search on your error brings up:

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  IndexError: index 0 is out of bounds for axis 0 with size 0 tmhsa 0 121 Apr-24-2020, 10:00 AM
Last Post: tmhsa
  Getting Index Error - list index out of range RahulSingh 2 584 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 198 Jan-28-2020, 09:30 AM
Last Post: axelle
  Executing Scipy Tensor Product John_Doe 1 259 Dec-06-2019, 10:28 AM
Last Post: paul18fr
  IndexError: index out of bounds LeoGER 3 1,653 Sep-05-2019, 02:05 PM
Last Post: LeoGER
  Install tensor flow for Python 2.7 in Windows 10 ErnestTBass 4 1,346 Jul-26-2019, 02:11 PM
Last Post: ThomasL
  Unable to identify Tuple index error appmkl 4 1,000 Jun-28-2019, 10:12 AM
Last Post: appmkl
  keras tensor mainpulation output to next input JohnyBeGood 0 582 Nov-21-2018, 03:30 PM
Last Post: JohnyBeGood
  Using pandas, index error fyec 1 1,834 Aug-01-2018, 09:25 AM
Last Post: volcano63
  Regarding index out of bounds error and implementing Loop error in backpropagation al geevaprasa 6 4,327 Apr-20-2018, 09:35 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)