为什么Matplotlib不起作用?我有NN,想绘制一个图,但没有用!
# Train the network
num_epochs = 200
loss_X = []
loss_Y = []
loss_Y_test = []
for epoch in range(num_epochs):
for inputs,labels in train_loader:
inputs = inputs.float()
labels = labels.float()
# Feed Forward
output = net(inputs)
# Loss Calculation
loss = criterion(output, labels)
# Clear the gradient buffer (we don't want to accumulate gradients)
optimizer.zero_grad()
# Backpropagation
loss.backward()
# Weight Update: w <-- w - lr * gradient
optimizer.step()
loss_X.append(epoch)
loss_Y.append(loss)
# Test the the network
for inputs_T,labels_T in test_loader:
inputs_T = inputs_T.float()
labels_T = labels_T.float()
# Feed Forward
output_T = net(inputs_T)
# Loss Calculation
loss_T = criterion(output_T, labels_T)
# Clear the gradient buffer (we don't want to accumulate gradients)
optimizer.zero_grad()
# Backpropagation
loss_T.backward()
# Weight Update: w <-- w - lr * gradient
optimizer.step()
loss_Y_test.append(loss_T)
#Accuracy
# Since we are using a sigmoid, we will need to perform some thresholding
output = (output>0.5).float()
output_T = (output_T>0.5).float()
# Accuracy: (output == labels).float().sum() / output.shape[0]
accuracy = (output == labels).float().mean()
accuracy_T = (output_T == labels_T).float().mean()
# Print statistics
#print("Epoch {}/{}, Loss: {:.3f},Loss_T: {:.3f}, Accuracy: {:.3f}, Accuracy_T: {:.3f}".format(epoch+1,num_epochs, loss,loss_T, accuracy, accuracy_T))
print("Epoch {}, Loss: {:.3f},Loss_T: {:.3f}".format(epoch+1, loss,loss_T))
这是绘制火车和测试图的飞行员代码
import matplotlib.pyplot as plt
plt.plot(loss_X, loss_Y, label="train")
plt.plot(loss_X, loss_Y_test, label="test")
plt.ylim([0, 2000])
plt.legend()
plt.savefig("chech.png")
plt.show()
这是错误消息“只能将一个元素张量转换为Python标量”
ValueError Traceback (most recent call last)
<ipython-input-1196-8a4ea3f00ea2> in <module>
----> 1 plt.plot(loss_X, loss_Y, label="train")
2 plt.plot(loss_X, loss_Y_test, label="test")
3 plt.ylim([0, 2000])
4 plt.legend()
5 plt.savefig("chech.png")
ValueError: only one element tensors can be converted to Python scalars
如何使用x = epoch,y = train,z = test进行绘图?