RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Pata_Naheen: <p>my input tensor is torch.Size([3, 1])<br>
and output tensor shape is [3]</p>
<p>help is requested please</p>

albanD: <p>Hi,</p>
<p>Could you explain what you’re trying to do?<br>
Remember that python is 0 indexed as well !</p>

Pata_Naheen: <p>i have simple problem 4 input length and 3 output required against 4 inputs.<br>
------------------------------ tensor([[3411.0554, 3316.2825, 2679.5391, 2864.1514]])<br>
------------------------------ tensor([[ 587.7465, 1382.9791,  339.3717]], grad_fn=) tensor([ -43.2563, -250.4521, 1020.8250])<br>
_----------------------------------------------------------------- torch.Size([3, 1])</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self, input_size, hidden_size, num_classes):<br>
super(Net, self).<strong>init</strong>()<br>
self.fc1 = nn.Linear(input_size, hidden_size)<br>
self.relu = nn.ReLU()<br>
self.fc2 = nn.Linear(hidden_size, num_classes)</p>
<pre><code>def forward(self, x):
    out = self.fc1(x)
    out = self.relu(out)
    out = self.fc2(out)
    return out
</code></pre>
<p>net = Net(input_size, hidden_size, num_classes)<br>
<span class="hashtag">#net</span>.cuda()</p>
<h1>Loss and Optimizer</h1>
<p>criterion = nn.MSELoss()<br>
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)</p>
<h1>Train the Model</h1>
<p>for epoch in range(num_epochs):<br>
for i, data in enumerate(train_loader, 0):<br>
# get the inputs<br>
inputs, labels = data</p>
<pre><code>    # wrap them in Variable
    inputs = Variable(inputs.view(-1, 1*4))
    labels = Variable(labels.view(3,))
    #target = Variable(torch.FloatTensor()
    print("------------------------------",inputs.data)

    # Forward + Backward + Optimize
    optimizer.zero_grad()  # zero the gradient buffer
    outputs = net(inputs.float())
    print("------------------------------",outputs,labels)
    labels = labels.float()
    outputs = outputs.transpose(0, 1)
    print("_-----------------------------------------------------------------",outputs.shape)
    output1 = outputs.data[0][0]
    label1 = labels.data[0]
    output2 = outputs.data[1][0]
    label2 = labels.data[1]
    output3 = outputs.data[2][0]
    label3 = labels.data[2]
    print(type(label1))
    print("outputssssssssssssss",output1,output2,output3,label3)
    loss1 = criterion(output1.data,label1)
    loss2 = criterion(output2.data,label2)
    loss3 = criterion(output3.data,label3)
    print("loss -----------------------------------",loss1)
    loss = loss1+loss2+loss3

    loss.backward()
    optimizer.step()
    print(loss)
    
    if (i+1) % 100 == 0:
        print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' 
               %(epoch+1, num_epochs, i+1, len(train_loader)//batch_size, loss.data[0]))</code></pre>

Pata_Naheen: <p>now i am getting following error.<br>
------------------------------ tensor([[3841.0667, 3840.0134, 3841.4934, 3840.0000]], device=‘cuda:0’)<br>
------------------------------ tensor([[ 172.9745, -751.5243,  279.0851]],<br>
device=‘cuda:0’, grad_fn=) tensor([-4.4937e-03,  2.6100e-03,  3.4345e+02],<br>
device=‘cuda:0’, grad_fn=)</p>
<p>File “/usr/local/lib/python3.5/dist-packages/torch/autograd/<strong>init</strong>.py”, line 90, in backward<br>
allow_unreachable=True)  # allow_unreachable flag<br>
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</p>
<p>I am new in that. and Thank for your help. what i need to change in code</p>

albanD: <p>Do not use <code>.data</code>, it breaks the computational graph.<br>
In particular, when you compute your losses, since you use <code>.data</code> you prevent gradients from flowing back to the networks.</p>