TensorFlow如何用于在Python中训练Fashion MNIST数据集的模型?
Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,与Python结合使用以实现算法,深度学习应用程序等等。它用于研究和生产目的。
可以使用下面的代码行在Windows上安装'tensorflow'软件包
pip install tensorflow
Tensor是TensorFlow中使用的数据结构。它有助于连接流程图中的边缘。该流程图称为“数据流程图”。张量不过是多维数组或列表。
“时尚MNIST”数据集包含各种服装的图像。它包含超过10万个类别的7万多件衣服的灰度图像。这些图像的分辨率较低(28x28像素)。
我们正在使用Google合作实验室来运行以下代码。GoogleColab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。合作已建立在JupyterNotebook的基础上。以下是代码-
示例
print("The model is fit to the data") model.fit(train_images, train_labels, epochs=15) print("The accuracy is being computed") test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) print('\nThe test accuracy is :', test_acc)
代码信用-https://www.tensorflow.org/tutorials/keras/classification
输出结果
The model is fit to the data Epoch 1/15 1875/1875 [==============================] - 4s 2ms/step - loss: 0.6337 - accuracy: 0.7799 Epoch 2/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.3806 - accuracy: 0.8622 Epoch 3/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.3469 - accuracy: 0.8738 Epoch 4/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.3131 - accuracy: 0.8853 Epoch 5/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2962 - accuracy: 0.8918 Epoch 6/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2875 - accuracy: 0.8935 Epoch 7/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2705 - accuracy: 0.8998 Epoch 8/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2569 - accuracy: 0.9023 Epoch 9/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2465 - accuracy: 0.9060 Epoch 10/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2440 - accuracy: 0.9088 Epoch 11/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2300 - accuracy: 0.9143 Epoch 12/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2255 - accuracy: 0.9152 Epoch 13/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2114 - accuracy: 0.9203 Epoch 14/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2101 - accuracy: 0.9211 Epoch 15/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2057 - accuracy: 0.9224 The accuracy is being computed 313/313 - 0s - loss: 0.3528 - accuracy: 0.8806 The test accuracy is : 0.8805999755859375
解释
通过首先提供训练数据并构建模型来训练模型。“train_images”和“train_labels”是输入数据的数组。
该模型倾向于使用相应的标签映射图像。
“test_images”存储测试数据。
一旦使用了测试数据集,所做的预测就会与测试数据集中数据的实际标签相匹配。
调用“model.fit”方法,以使其适合训练数据集。
“模型评估”功能可提供准确性和与训练相关的损失。