hello
I got same error
AttributeError: ‘tuple’ object has no attribute ‘dim’
print(‘Starting’)
print(‘Running for’,epochs,‘Epochs’)
#print(‘Batch Size:’, mnist.trainloader.batch_size)
#n_of_batches = int(len(mnist.train_data.data)/mnist.trainloader.batch_size)
#print(‘No of batches per epoch:’, n_of_batches)
losses = []
test_acc = []
for epoch in range(epochs):
loss2 = []
for i,data in enumerate(zip(train_loader, train_labels),0):
pcae.train()
inputs, labels = data
inputs = torch.tensor(inputs, dtype = torch.float32)
labels = torch.tensor(labels, dtype = torch.float32)
inputs, labels = inputs.to(‘cuda’), labels.to(‘cuda’)
optimizer.zero_grad()
out = pcae(inputs)
loss = loss_func(out)
loss.mean().backward()
optimizer.step()
loss2.append(loss.mean().item())
# print("\rEpoch #:",epoch+1, "  Batch #:", i+1, “\tImage Likelihood:”,"{0:.2f}".format(-img_lik.item()),
#       “\tPart Likelihood:”,"{0:.2f}".format(-part_lik.item()), “\tPrior Sparsity:”,"{0:.2f}".format(prior_sparsity.item()),
#       “\tPosterior Sparsity:”,"{0:.2f}".format(posterior_sparsity.item()) end="")
print("\rEpoch #:",epoch+1, "  Batch #:", i+1, “\tImage Likelihood:”,"{0:.2f}".format(loss.mean().item()), end="")
l = np.sum(loss2)/(len(loss2))
correct = 0
total = 0
with torch.no_grad():
for data in zip(train_loader, train_labels) :
pcae.eval()
inputs, labels = data
inputs = torch.tensor(inputs, dtype = torch.float32)
labels = torch.tensor(labels, dtype = torch.float32)
inputs, labels = inputs.to(‘cuda’), labels.to(‘cuda’)
outs= pcae(inputs)
norm = torch.norm(outs, dim=2)
predicted = np.argmax(norm.cpu().numpy(),axis=1)
total += labels.size(0)
correct += (predicted == labels.cuda().numpy()).sum().item()
acc = (100 * correct / total)
print(’\rEpoch #:’, epoch+1, ’  Training Loss: ',"{0:.2f}".format(l))
print(‘CapsNet Testing Accuracy on MNIST: ‘,acc, ‘%’)
test_acc.append(acc)
if max(test_acc)==acc:
#torch.save(capsnet.state_dict(), ‘gdrive/My Drive/capsnet_40_epochs.pth’)
losses.append(l)
print(’\rEpoch #:’, epoch+1, ’  Training Loss: ',"{0:.2f}".format(l))
losses.append(l)
#torch.save(pcae.state_dict(), “./gdrive/My Drive/trained_pcae_2.pth”)
print(‘Finished’)