全部課程
發(fā)布時(shí)間: 2019-07-25 15:16:59
本指南使用的是tf.keras,它是一種用于在 TensorFlow 中構(gòu)建和訓(xùn)練模型的高階 API。
from __future__ import absolute_import, division, print_function, unicode_literals
# TensorFlow and tf.keras
import tensorflow as tf
try:
import tensorflow.keras as keras
except:
import tensorflow.python.keras as keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
本指南使用Fashion MNIST <https://github.com/zalandoresearch/fashion-mnist>數(shù)據(jù)集,其中包含 70000 張灰度圖像,涵蓋 10 個(gè)類(lèi)別。以下圖像顯示了單件服飾在較低分辨率(28x28 像素)下的效果:

Fashion MNIST sprite
Fashion MNIST 的作用是成為經(jīng)典 MNIST 數(shù)據(jù)集的簡(jiǎn)易替換,后者通常用作計(jì)算機(jī)視覺(jué)機(jī)器學(xué)習(xí)程序的“Hello, World”入門(mén)數(shù)據(jù)集。MNIST數(shù)據(jù)集包含手寫(xiě)數(shù)字(0、1、2 等)的圖像,這些圖像的格式與我們?cè)诒窘坛讨惺褂玫姆棃D像的格式相同。 train_images和train_labels數(shù)組是訓(xùn)練集,即模型用于學(xué)習(xí)的數(shù)據(jù)。
測(cè)試集 test_images 和 test_labels 數(shù)組用于測(cè)試模型。
圖像為28x28的NumPy數(shù)組,像素值介于0到255之間。標(biāo)簽是整數(shù)數(shù)組,介于0到9之間。這些標(biāo)簽對(duì)應(yīng)于圖像代表的服飾所屬的類(lèi)別:
Label | Class |
0 | T-shirt/top(T 恤衫/上衣) |
1 | Trouser(褲子) |
2 | Pullover (套衫) |
3 | Dress(裙子) |
4 | Coat(外套) |
5 | Sandal(涼鞋) |
6 | Shirt(襯衫) |
7 | Sneaker(運(yùn)動(dòng)鞋) |
8 | Bag(包包) |
9 | Ankle boot(踝靴) |
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
我們先探索數(shù)據(jù)集的格式,然后再訓(xùn)練模型。以下內(nèi)容顯示訓(xùn)練集中有 60000 張圖像,每張圖像都表示為 28x28 像素:
train_images.shape
(60000, 28, 28)
同樣,訓(xùn)練集中有60,000個(gè)標(biāo)簽:
len(train_labels)
60000
每個(gè)標(biāo)簽都是0到9之間的整數(shù):
train_labels
array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
測(cè)試集中有10,000個(gè)圖像。同樣,每個(gè)圖像表示為28 x 28像素:
test_images.shape
(10000, 28, 28)
測(cè)試集包含10,000個(gè)圖像標(biāo)簽:
len(test_labels)
10000
在訓(xùn)練網(wǎng)絡(luò)之前必須對(duì)數(shù)據(jù)進(jìn)行預(yù)處理。 如果您檢查訓(xùn)練集中的第一個(gè)圖像,您將看到像素值落在0到255的范圍內(nèi):
#預(yù)處理數(shù)據(jù)
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
我們將這些值縮小到 0 到 1 之間,然后將其饋送到神經(jīng)網(wǎng)絡(luò)模型。為此,將圖像組件的數(shù)據(jù)類(lèi)型從整數(shù)轉(zhuǎn)換為浮點(diǎn)數(shù),然后除以 255。以下是預(yù)處理圖像的函數(shù):務(wù)必要以相同的方式對(duì)訓(xùn)練集和測(cè)試集進(jìn)行預(yù)處理:
train_images = train_images / 255.0
test_images = test_images / 255.0
為了驗(yàn)證數(shù)據(jù)的格式是否正確以及我們是否已準(zhǔn)備好構(gòu)建和訓(xùn)練網(wǎng)絡(luò),讓我們顯示訓(xùn)練集中的前25個(gè)圖像,并在每個(gè)圖像下方顯示類(lèi)名。
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()

模型還需要再進(jìn)行幾項(xiàng)設(shè)置才可以開(kāi)始訓(xùn)練。這些設(shè)置會(huì)添加到模型的編譯步驟:
損失函數(shù):衡量模型在訓(xùn)練期間的準(zhǔn)確率。我們希望盡可能縮小該函數(shù),以“引導(dǎo)”模型朝著正確的方向優(yōu)化。
優(yōu)化器:根據(jù)模型看到的數(shù)據(jù)及其損失函數(shù)更新模型的方式。
度量標(biāo)準(zhǔn):用于監(jiān)控訓(xùn)練和測(cè)試步驟。以下示例使用準(zhǔn)確率,即圖像被正確分類(lèi)的比例。
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
接下來(lái),比較模型在測(cè)試數(shù)據(jù)集上的表現(xiàn)情況:
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('\nTest accuracy:', test_acc)
輸出:
10000/10000 [==============================] - 1s 50us/step
Test accuracy: 0.8734
結(jié)果表明,模型在測(cè)試數(shù)據(jù)集上的準(zhǔn)確率略低于在訓(xùn)練數(shù)據(jù)集上的準(zhǔn)確率。訓(xùn)練準(zhǔn)確率和測(cè)試準(zhǔn)確率之間的這種差異表示出現(xiàn)過(guò)擬合(overfitting)。如果機(jī)器學(xué)習(xí)模型在新數(shù)據(jù)上的表現(xiàn)不如在訓(xùn)練數(shù)據(jù)上的表現(xiàn),也就是泛化性不好,就表示出現(xiàn)過(guò)擬合。
???輸出:
array([6.2482708e-05, 2.4860196e-08, 9.7165821e-07, 4.7436039e-08,
2.0804382e-06, 1.3316551e-02, 9.8731316e-06, 3.4591161e-02,
1.2390658e-04, 9.5189297e-01], dtype=float32)
?預(yù)測(cè)結(jié)果是一個(gè)具有 10 個(gè)數(shù)字的數(shù)組,這些數(shù)字說(shuō)明模型對(duì)于圖像對(duì)應(yīng)于 10 種不同服飾中每一個(gè)服飾的“confidence(置信度)”。我們可以看到哪個(gè)標(biāo)簽的置信度值較大:
np.argmax(predictions[0])
因此,模型非常確信這張圖像是踝靴或?qū)儆?class_names[9]。我們可以檢查測(cè)試標(biāo)簽以查看該預(yù)測(cè)是否正確:
test_labels[0]
我們可以將該預(yù)測(cè)繪制成圖來(lái)查看全部 10 個(gè)通道
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array[i], 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):
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')
讓我們看看第0個(gè)圖像,預(yù)測(cè)和預(yù)測(cè)數(shù)組。??
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)
plt.show()
?
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions, test_labels)
plt.show()
?
我們用它們的預(yù)測(cè)繪制幾張圖像。正確的預(yù)測(cè)標(biāo)簽為藍(lán)色,錯(cuò)誤的預(yù)測(cè)標(biāo)簽為紅色。數(shù)字表示預(yù)測(cè)標(biāo)簽的百分比(總計(jì)為 100)。請(qǐng)注意,即使置信度非常高,也有可能預(yù)測(cè)錯(cuò)誤。
# 繪制前X個(gè)測(cè)試圖像,預(yù)測(cè)標(biāo)簽和真實(shí)標(biāo)簽。
# 用藍(lán)色標(biāo)記正確的預(yù)測(cè),用紅色標(biāo)記錯(cuò)誤的預(yù)測(cè)。
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)
plt.show()
??
最后,使用訓(xùn)練的模型對(duì)單個(gè)圖像進(jìn)行預(yù)測(cè)。
# 從測(cè)試數(shù)據(jù)集中獲取圖像
img = test_images[0]
print(img.shape)
tf.keras模型已經(jīng)過(guò)優(yōu)化,可以一次性對(duì)樣本批次或樣本集進(jìn)行預(yù)測(cè)。因此,即使我們使用單個(gè)圖像,仍需要將其添加到列表中:
# 將圖像添加到批次中,它是唯一的成員。
img = (np.expand_dims(img,0))
print(img.shape)
(1, 28, 28)
現(xiàn)在預(yù)測(cè)此圖像的正確標(biāo)簽:
predictions_single = model.predict(img)
print(predictions_single)
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
?
model.predict返回一組列表,每個(gè)列表對(duì)應(yīng)批次數(shù)據(jù)中的每張圖像。(僅)獲取批次數(shù)據(jù)中相應(yīng)圖像的預(yù)測(cè)結(jié)果:
np.argmax(predictions_single[0])
本實(shí)驗(yàn)利用網(wǎng)上已有的北京房?jī)r(jià)數(shù)據(jù)集預(yù)測(cè)了北京的房?jī)r(jià),實(shí)現(xiàn)了TensorFlow的線性回歸應(yīng)用。?
上一篇: 大數(shù)據(jù)培訓(xùn)_如何使用Spark連接與操作Mysql數(shù)據(jù)庫(kù)