for epoch in rangeepochs optimizerzero_grad outputs = netX_train loss = criterionoutputs y_trainview-1 1 lossbackward optimizerstep # 计算训练准确率和loss with torchno_grad train_p
以下是增加绘制精确率、召回率、F1的曲线图的代码:
初始化精确率、召回率、F1
train_precision = [] train_recall = [] train_f1 = [] test_precision = [] test_recall = [] test_f1 = []
for epoch in range(epochs): optimizer.zero_grad() outputs = net(X_train) loss = criterion(outputs, y_train.view(-1, 1)) loss.backward() optimizer.step() # 计算训练准确率和loss with torch.no_grad(): train_predicted = net(X_train) train_predicted = train_predicted.round() train_accuracy = (train_predicted == y_train.view(-1, 1)).sum().item() / len(y_train) train_losses.append(loss.item()) train_accuracies.append(train_accuracy) # 计算训练精确率、召回率、F1 train_tp = ((train_predicted == 1) & (y_train.view(-1, 1) == 1)).sum().item() train_fp = ((train_predicted == 1) & (y_train.view(-1, 1) == 0)).sum().item() train_fn = ((train_predicted == 0) & (y_train.view(-1, 1) == 1)).sum().item() train_precision.append(train_tp / (train_tp + train_fp)) train_recall.append(train_tp / (train_tp + train_fn)) train_f1.append(2 * train_precision[-1] * train_recall[-1] / (train_precision[-1] + train_recall[-1]))
# 计算测试准确率和loss
test_predicted = net(X_test)
test_predicted = test_predicted.round()
test_loss = criterion(test_predicted, y_test.view(-1, 1))
test_accuracy = (test_predicted == y_test.view(-1, 1)).sum().item() / len(y_test)
test_losses.append(test_loss.item())
test_accuracies.append(test_accuracy)
# 计算测试精确率、召回率、F1
test_tp = ((test_predicted == 1) & (y_test.view(-1, 1) == 1)).sum().item()
test_fp = ((test_predicted == 1) & (y_test.view(-1, 1) == 0)).sum().item()
test_fn = ((test_predicted == 0) & (y_test.view(-1, 1) == 1)).sum().item()
test_precision.append(test_tp / (test_tp + test_fp))
test_recall.append(test_tp / (test_tp + test_fn))
test_f1.append(2 * test_precision[-1] * test_recall[-1] / (test_precision[-1] + test_recall[-1]))
if epoch % 1 == 0:
print("Epoch {}, Train Loss: {:.4f}, Train Accuracy: {:.2f}%, Train Precision: {:.2f}%, Train Recall: {:.2f}%, Train F1: {:.2f}%, Test Loss: {:.4f}, Test Accuracy: {:.2f}%, Test Precision: {:.2f}%, Test Recall: {:.2f}%, Test F1: {:.2f}%".format(
epoch, loss.item(), train_accuracy * 100, train_precision[-1] * 100, train_recall[-1] * 100, train_f1[-1] * 100, test_loss.item(), test_accuracy * 100, test_precision[-1] * 100, test_recall[-1] * 100, test_f1[-1] * 100))
#绘制train和test loss曲线 plt.plot(train_losses, label="Train Loss") plt.plot(test_losses, label="Test Loss") plt.legend() plt.xlabel("Epoch") plt.ylabel("Loss") plt.show()
#绘制train和test accuracy曲线 plt.plot(train_accuracies, label="Train Accuracy") plt.plot(test_accuracies, label="Test Accuracy") plt.legend() plt.xlabel("Epoch") plt.ylabel("Accuracy") plt.show()
#绘制train和test precision曲线 plt.plot(train_precision, label="Train Precision") plt.plot(test_precision, label="Test Precision") plt.legend() plt.xlabel("Epoch") plt.ylabel("Precision") plt.show()
#绘制train和test recall曲线 plt.plot(train_recall, label="Train Recall") plt.plot(test_recall, label="Test Recall") plt.legend() plt.xlabel("Epoch") plt.ylabel("Recall") plt.show()
#绘制train和test F1曲线 plt.plot(train_f1, label="Train F1") plt.plot(test_f1, label="Test F1") plt.legend() plt.xlabel("Epoch") plt.ylabel("F1") plt.show(
原文地址: http://www.cveoy.top/t/topic/dOVz 著作权归作者所有。请勿转载和采集!