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

my input tensor is torch.Size([3, 1])
and output tensor shape is [3]

help is requested please

Hi,

Could you explain what you’re trying to do?
Remember that python is 0 indexed as well !

i have simple problem 4 input length and 3 output required against 4 inputs.
------------------------------ tensor([[3411.0554, 3316.2825, 2679.5391, 2864.1514]])
------------------------------ tensor([[ 587.7465, 1382.9791, 339.3717]], grad_fn=) tensor([ -43.2563, -250.4521, 1020.8250])
_----------------------------------------------------------------- torch.Size([3, 1])

class Net(nn.Module):
def init(self, input_size, hidden_size, num_classes):
super(Net, self).init()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)

def forward(self, x):
    out = self.fc1(x)
    out = self.relu(out)
    out = self.fc2(out)
    return out

net = Net(input_size, hidden_size, num_classes)
#net.cuda()

Loss and Optimizer

criterion = nn.MSELoss()
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)

Train the Model

for epoch in range(num_epochs):
for i, data in enumerate(train_loader, 0):
# get the inputs
inputs, labels = data

    # 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]))

now i am getting following error.
------------------------------ tensor([[3841.0667, 3840.0134, 3841.4934, 3840.0000]], device=‘cuda:0’)
------------------------------ tensor([[ 172.9745, -751.5243, 279.0851]],
device=‘cuda:0’, grad_fn=) tensor([-4.4937e-03, 2.6100e-03, 3.4345e+02],
device=‘cuda:0’, grad_fn=)

File “/usr/local/lib/python3.5/dist-packages/torch/autograd/init.py”, line 90, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

I am new in that. and Thank for your help. what i need to change in code

Do not use .data, it breaks the computational graph.
In particular, when you compute your losses, since you use .data you prevent gradients from flowing back to the networks.