如何在 Python 中使用 TensorFlow 对 Fashion MNIST 数据集进行预测?
Tensorflow 是 Google 提供的一个机器学习框架。它是一个开源框架,与 Python 结合使用以实现算法、深度学习应用程序等等。它用于研究和生产目的。它具有优化技术,有助于快速执行复杂的数学运算。这是因为它使用了 NumPy 和多维数组。这些多维数组也称为“张量”。
可以使用以下代码行在 Windows 上安装“tensorflow”包:
pip install tensorflow
张量是 TensorFlow 中使用的数据结构。它有助于连接流图中的边。此流图称为“数据流图”。张量不过就是多维数组或列表。
“Fashion MNIST”数据集包含各种服装的图像。它包含超过 70,000 件属于 10 个不同类别的服装的灰度图像。这些图像的分辨率较低(28 x 28 像素)。
我们正在使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 有助于通过浏览器运行 Python 代码,并且无需任何配置,并且可以免费访问 GPU(图形处理单元)。Colaboratory 是建立在 Jupyter Notebook 之上的。
以下是进行预测的代码片段:
示例
probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print("The predictions are being made ")
print(predictions[0])
np.argmax(predictions[0])
print("The test labels are")
print(test_labels[0])
def plot_image(i, predictions_array, true_label, img):
true_label, img = true_label[i], img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]), color=color)
def plot_value_array(i, predictions_array, true_label):
true_label = true_label[i]
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color(‘green’)代码来源 − https://tensorflowcn.cn/tutorials/keras/classification
输出
The predictions are being made [1.3008227e−07 9.4930819e−10 2.0181861e−09 5.4944155e−10 3.8257373e−11 1.3896286e−04 1.4776078e−08 3.1724274e−03 9.4210514e−11 9.9668854e−01] The test labels are 9
解释
模型训练完成后,需要对其进行测试。
这可以通过使用构建的模型对图像进行预测来完成。
线性输出、logits 和 softmax 层附加到它。
softmax 层有助于将 logits 转换为概率。
这样做是为了更容易解释所做的预测。
定义了“plot_value_array”方法,该方法显示实际值和预测值。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP