如何使用Python和Tensorflow評估CNN模型?


可以使用‘evaluate’方法評估卷積神經網路。此方法將測試資料作為其引數。在此之前,資料使用‘matplotlib’庫和‘imshow’方法在控制檯上繪製。

閱讀更多: 什麼是TensorFlow以及Keras如何與TensorFlow一起建立神經網路?

卷積神經網路已被用於針對特定型別的問題(例如影像識別)產生良好的結果。

我們使用Google Colaboratory執行以下程式碼。Google Colab或Colaboratory有助於透過瀏覽器執行Python程式碼,無需任何配置,並可免費訪問GPU(圖形處理單元)。Colaboratory構建在Jupyter Notebook之上。

print("Plotting accuracy versus epoch")
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
print("The model is being evaluated")
test_loss, test_acc = model.evaluate(test_images,test_labels, verbose=2)
print("The accuracy of the model is:")
print(test_acc)

程式碼來源:https://www.tensorflow.org/tutorials/images/cnn

輸出

Plotting accuracy versus epoch
The model is being evaluated
313/313 - 3s - loss: 0.8884 - accuracy: 0.7053
The accuracy of the model is:
0.705299973487854

解釋

  • 視覺化精度與輪數資料。
  • 這是使用matplotlib庫完成的。
  • 評估模型,並確定損失和精度。

更新於:2021年2月20日

3K+ 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.