mnist分类

参考网址

代码

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow import keras

import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__) # 1.13.1

# 下载mnist数据
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()


class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

print(train_images.shape) # (60000, 28, 28)
print(train_labels) # [9 0 0 ... 3 0 5]


# 预处理图片数据
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

train_images = train_images / 255.0
test_images = test_images / 255.0


plt.figure(figsize=(10, 10))
for i in range(25):
    plt.subplot(5, 5, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=["accuracy"])

model.fit(train_images, train_labels, epochs=10)

# 评估准确率
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy:', test_acc)
predictions = model.predict(test_images)
print("predictions:", predictions)
print(np.argmax(predictions[0]))

运行结果



1.15.0
(60000, 28, 28)
[9 0 0 ... 3 0 5]

img

img

Train on 60000 samples
Epoch 1/10
60000/60000 [==============================] - 4s 66us/sample - loss: 0.5022 - acc: 0.8227
Epoch 2/10
60000/60000 [==============================] - 4s 66us/sample - loss: 0.3758 - acc: 0.8638
Epoch 3/10
60000/60000 [==============================] - 4s 63us/sample - loss: 0.3376 - acc: 0.8766
Epoch 4/10
60000/60000 [==============================] - 4s 65us/sample - loss: 0.3139 - acc: 0.8847
Epoch 5/10
60000/60000 [==============================] - 4s 66us/sample - loss: 0.2968 - acc: 0.8913
Epoch 6/10
60000/60000 [==============================] - 4s 67us/sample - loss: 0.2839 - acc: 0.8962
Epoch 7/10
60000/60000 [==============================] - 4s 67us/sample - loss: 0.2701 - acc: 0.8991
Epoch 8/10
60000/60000 [==============================] - 4s 66us/sample - loss: 0.2586 - acc: 0.9042
Epoch 9/10
60000/60000 [==============================] - 4s 67us/sample - loss: 0.2489 - acc: 0.9076
Epoch 10/10
60000/60000 [==============================] - 4s 66us/sample - loss: 0.2405 - acc: 0.9098
10000/10000 - 0s - loss: 0.3365 - acc: 0.8819

Test accuracy: 0.8819
predictions: [[2.39766962e-08 4.77301931e-09 6.27682839e-09 ... 7.59751396e-03
  1.05895674e-08 9.92169797e-01]
 [3.52420102e-05 4.35056503e-17 9.98871744e-01 ... 4.13423255e-20
  1.00847865e-10 4.21113415e-17]
 [1.75255295e-07 9.99999881e-01 1.02554363e-11 ... 2.09053005e-18
  9.63780240e-16 6.92412211e-18]
 ...
 [1.00794732e-05 4.70920134e-12 2.08072997e-05 ... 3.61725117e-09
  9.99902725e-01 1.04085707e-12]
 [3.94008381e-09 9.99998331e-01 4.71316985e-10 ... 4.76147120e-15
  1.85048261e-11 1.75750907e-13]
 [7.11079920e-05 1.56867603e-08 2.60988531e-06 ... 3.51900700e-03
  1.42664067e-04 1.85518929e-05]]
9

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦