From f2ebd8a37bf1ef3887d9cba1ee01b1f107430f11 Mon Sep 17 00:00:00 2001 From: morvanzhou Date: Fri, 29 Jun 2018 10:09:52 +0800 Subject: [PATCH] update accuracy function --- tutorial-contents/302_classification.py | 2 +- tutorial-contents/401_CNN.py | 4 ++-- tutorial-contents/402_RNN_classifier.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tutorial-contents/302_classification.py b/tutorial-contents/302_classification.py index 53c53fd..0b96e2f 100644 --- a/tutorial-contents/302_classification.py +++ b/tutorial-contents/302_classification.py @@ -62,7 +62,7 @@ for t in range(100): pred_y = prediction.data.numpy().squeeze() target_y = y.data.numpy() plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn') - accuracy = sum(pred_y == target_y)/200. + accuracy = float((pred_y == target_y).astype(int).sum()) / float(target_y.size) plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color': 'red'}) plt.pause(0.1) diff --git a/tutorial-contents/401_CNN.py b/tutorial-contents/401_CNN.py index c46a28f..1f9daea 100644 --- a/tutorial-contents/401_CNN.py +++ b/tutorial-contents/401_CNN.py @@ -115,8 +115,8 @@ for epoch in range(EPOCH): if step % 50 == 0: test_output, last_layer = cnn(test_x) - pred_y = torch.max(test_output, 1)[1].data.squeeze() - accuracy = float(sum(pred_y == test_y)) / float(test_y.size(0)) + pred_y = torch.max(test_output, 1)[1].data.squeeze().numpy() + accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0)) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy) if HAS_SK: # Visualization of trained flatten layer (T-SNE) diff --git a/tutorial-contents/402_RNN_classifier.py b/tutorial-contents/402_RNN_classifier.py index 6cbd93e..9ef11f7 100644 --- a/tutorial-contents/402_RNN_classifier.py +++ b/tutorial-contents/402_RNN_classifier.py @@ -95,7 +95,7 @@ for epoch in range(EPOCH): if step % 50 == 0: test_output = rnn(test_x) # (samples, time_step, input_size) pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze() - accuracy = float(sum(pred_y == test_y)) / float(test_y.size) + accuracy = float((pred_y == test_y).astype(int).sum()) / float(test_y.size) print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy) # print 10 predictions from test data