簡體   English   中英

在Tensorflow Mist教程中顯示預測圖像的問題

[英]Problem showing the predicted image in Tensorflow mnist tutorial

我正在使用Tensorflow 初學者mnist教程中的代碼(下面的代碼)。

 import tensorflow as tf
 mnist = tf.keras.datasets.mnist

 (x_train, y_train),(x_test, y_test) = mnist.load_data()
 x_train, x_test = x_train / 255.0, x_test / 255.0

 model = tf.keras.models.Sequential([
   tf.keras.layers.Flatten(input_shape=(28, 28)),
   tf.keras.layers.Dense(512, activation=tf.nn.relu),
   tf.keras.layers.Dropout(0.2),
   tf.keras.layers.Dense(10, activation=tf.nn.softmax)
 ])
 model.compile(optimizer='adam',
               loss='sparse_categorical_crossentropy',
               metrics=['accuracy'])

 model.fit(x_train, y_train, epochs=5)
 model.evaluate(x_test, y_test)

由於教程在評估模型后停止,因此我添加了幾行內容進行實際的預測。

 predictions = model.predict([x_test])
 print(np.argmax(predictions[0]))

到目前為止,一切正常。 但是,我想查看模型試圖預測的圖像以了解發生了什么。 因此,我嘗試了以下方法:

 import matplotlib.pyplot as plt
 plt.imshow(x_test[0])
 plt.show()

這是行不通的。 我沒有收到錯誤消息,只是不會顯示圖片。 此外,如果我嘗試在matplotlib代碼之后添加一個簡單的打印語句,該代碼也會被忽略並且不會被打印。 知道發生了什么嗎?

您報告.show()在當前配置中無法正常工作。 與其打電話說,您可能更喜歡這樣:

plt.savefig('plot.png')

如果需要,可以將其與無頭驅動程序一起使用:

import matplotlib
matplotlib.use('Agg')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM