Python 图像序列的LSTM-属性错误:';布尔';对象没有属性';浮动';
除了我以前的职位, , 我更进一步,现在运行以下代码:Python 图像序列的LSTM-属性错误:';布尔';对象没有属性';浮动';,python,pytorch,conv-neural-network,lstm,Python,Pytorch,Conv Neural Network,Lstm,除了我以前的职位, , 我更进一步,现在运行以下代码: loop += 1 if loop % 50 == 0: # calculate Accuracy correct = 0 total = 0 # Iterate through the test dataset for j in range(iter): seq
loop += 1
if loop % 50 == 0:
# calculate Accuracy
correct = 0
total = 0
# Iterate through the test dataset
for j in range(iter):
sequences = x_test[bs:bs + batch_size, :]
labels_t = y_test[bs:bs + batch_size]
test_images = dataset.load_images(sequences)
bs += batch_size
test_images = (torch.from_numpy(test_images)).view(-1, 4, 784)
print('test_image_shape {} - iter: {}'.format(test_images.shape, j))
labels_t = torch.from_numpy(labels_t)
labels_t = torch.argmax(labels_t, dim=1)
test_outputs = model(test_images).float()
pred_y = torch.max(test_outputs, 1)[1].data.numpy().squeeze()
total += labels_t.size(0)
correct += (pred_y == labels_t).float()
accuracy = 100 * (correct / total)
print('Iteration:{}. Loss:{}. Accuracy{}:'.format(loop, loss.item(), accuracy))
但是,它给了我以下错误:
correct+=(pred\u y==标签)。float()AttributeError:“bool”对象没有属性“float” 正确总是导致0,准确性也是
我怀疑它与某个软件包或需求有关,因为我搜索过,它似乎应该可以工作。知道这里可能缺少什么吗?那是因为pred\y是numpy.ndarray,labels\t是torch.tensor。 若将两个变量比较为torch.tensor,则可以得到浮点值 但变量的类型并不相同。 python说两个变量的类型不同(所以返回False) 您可以使用下面的代码将torch.tensor转换为numpy.ndarray
pred_y = torch.max(test_outputs, 1)[1].data.numpy().squeeze()
我想你可以这样实现
pred_y = torch.max(test_outputs, 1)[1].squeeze()
请试试这个 谢谢,我做到了,并且能够通过这一行,但最后它给出了以下错误:。加上最后的print语句将给出以下结果:精度作为张量返回。首先,您可以计算正确的like(pred_y==label_t).sum().item()的数量。第二,pred_________________________________。