RuntimeError: The size of tensor a (132) must match the size of tensor b (64) at non-singleton dimension 2
my output size is 1,2,132 *132
This error is raised, if an operation cannot be applied to tensors having a different shape.
In this case dim2
seems to be different, so you would need to check the shape of both tensors and make sure they are in the expected shape (depends on the operation).
Feel free to add more information to this thread in case you get stuck.
ii am getting this error now please help.
model = UNet()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)
num_epochs=7
total_step = len(test_data)
loss_list = []
acc_list = []
lbls=[]
pred=[]
for epoch in range(num_epochs):
for i ,(images, labels) in enumerate(test_data):
outputs = model(images)
#preds = torch.argmax(outputs,dim=1)
preds = outputs.unsqueeze(dim=1)
labels=labels.unsqueeze(dim=-1)
#outputs= outputs.argmax(outputs,dim=1)###
#loss = F.nll_loss(pred, labels)
loss = criterion(preds.float() , labels)
loss_list.append(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
total = labels.size(0)
_, predicted = torch.max(preds, 1)
correct = (predicted == labels).sum().item()
acc_list.append(correct / total)
lbls+=labels
pred+=predicted
if (i + 1) % 1 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'
.format(epoch + 1, num_epochs, i + 1, total_step, loss.item(),
(correct / total) * 100))
“”“ValueError: Expected target size (2, 2, 132, 132), got torch.Size([2, 1])”""