本文使用的数据集名为Fashion MNIST,是一个关于衣服的分类数据集。与MNIST手写数据集类似,都是作为图片分类入门的新手数据集,包含了70000张尺寸为28*28的黑白图片,共有十个类别。
[站外图片上传中...(image-1f2f91-1537627931284)]
导入工具
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
1.10.1
下载数据
TensorFlow中包含了这个数据集的API,但是很尴尬,国内的网络很难顺利地通过把这个数据集下载下来,所以这里需要自行下载数据。
from tensorflow.examples.tutorials.mnist import input_data
data = input_data.read_data_sets('data/fashion')
注意检查下载的数据格式,如果通过TensorFlow的API下载,获得的是28*28的图片格式数据,但是自行下载有可能会下载到784长度的一维数组格式的数据
探索数据
(train_images,train_labels) = data.train.images,data.train.labels
train_images.shape
(55000,784)
可以看到,虽然我的数据是从教程中提供的github地址下载的,但是数据集的长度和图片格式都与教程中有所不同。
查看数据
在开始处理数据之前,先查看一下数据的形式
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(train_images[0].reshape(28,28))
plt.colorbar()
plt.grid(False)
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
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].reshape(28,28),cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
建立序列模型并训练
这里使用了TensorFlow内嵌的keras建立了包含两个全连接层的神经网络,因为是分类问题,所以最后一层使用了softmax作为激活函数,spare_categorical_crossentropy为损失函数。
选错了损失函数或者激活函数都可能导致模型不收敛
from tensorflow import keras
import tensorflow as tf
model = keras.Sequential([
keras.layers.Dense(128,activation=tf.nn.relu),
keras.layers.Dense(10,activation=tf.nn.softmax)
])
model.compile(
optimizer = keras.optimizers.Adam(lr=0.1),
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy']
)
model.fit(train_images,train_labels,batch_size=100,epochs=5)
Epoch 1/5
55000/55000 [==============================] - 6s 106us/step - loss: 0.5668 - acc: 0.7905
Epoch 2/5
55000/55000 [==============================] - 4s 77us/step - loss: 0.4308 - acc: 0.8414
Epoch 3/5
55000/55000 [==============================] - 4s 77us/step - loss: 0.3967 - acc: 0.8543
Epoch 4/5
55000/55000 [==============================] - 4s 77us/step - loss: 0.3856 - acc: 0.8589
Epoch 5/5
55000/55000 [==============================] - 4s 77us/step - loss: 0.3660 - acc: 0.8640
验证模型
test_images,test_labels = data.test.images,data.test.labels
test_images = test_images / 255.0
test_loss,test_acc = model.evaluate(test_images,test_labels)
print("Test accuracy:",test_acc)
10000/10000 [==============================] - 1s 77us/step
Test accuracy: 0.8496
预测数据
predictions = model.predict(test_images)
展示预测结果
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array[i], true_label[i], img[i].reshape(28,28)
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):
predictions_array, true_label = predictions_array[i], true_label[i]
plt.grid(False)
plt.xticks([])
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('blue')
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_value_array(i, predictions, test_labels)
原文地址:https://www.tensorflow.org/tutorials/keras/basic_classification