翻譯自:https://tensorflow.google.cn/tutorials/quickstart/beginner
這是一個的使用Keras做如下事情的簡短介紹:
- 建立圖像分類的一個神經網絡。
- 訓練這個神經網絡。
- 最後,評估模型的精確度
下載和安裝TensorFlow 2。在你的應用程序中導入TensorFlow:
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
載入並準備好MNIST數據集,將樣本由整數轉化爲浮點數:
mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train,x_test = x_train / 255.0,x_test / 255.0
將模型的各層堆疊起來,以搭建tf.keras.Sequential模型。爲訓練選擇優化器和損失函數。
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
爲每個例子模型返回一個向量的"logits"和”log-odds”分數,一個案例如下:
predictions = model(x_train[:1]).numpy()
print(predictions)
輸出結果如下:
[[ 0.3304124 0.21085967 -0.26375788 0.18900187 -0.38255388 -0.42568913
-0.5831184 0.68005246 0.11979596 0.22090217]]
tf.nn.softmax函數將logits轉化爲每個類的概率。
tf.nn.softmax(predictions).numpy()
print(tf.nn.softmax(predictions).numpy())
輸出結果如下:
[[0.08072349 0.15725857 0.10213858 0.08166214 0.12849897 0.11941642
0.09160781 0.10133947 0.05636909 0.08098544]]
The losses.SparseCategoricalCrossentropy loss takes a vector of logits and a True index and returns a scalar loss for each example.
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
這個損失等於真實類的負對數概率,如果模型確定這個類是正確的,那麼它是零。
這個未經訓練的模型給出了接近隨機的概率(每個類的1/10),因此這個最原初的loss可能接近於-tf.log(1/10) ~= 2.3。
loss_fn(y_train[:1], predictions).numpy()
輸出結果:
3.2944663
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
The Model.fit method adjusts the model parameters to minimize the loss:
model.fit(x_train,y_train,epochs=5)
輸出結果:
Epoch 1/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2981 - accuracy: 0.9123
Epoch 2/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1431 - accuracy: 0.9582
Epoch 3/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.1062 - accuracy: 0.9679
Epoch 4/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0860 - accuracy: 0.9735
Epoch 5/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0731 - accuracy: 0.9776
Model.evaluate方法檢查這個模型的性能,通過是在”驗證集”或”測試集”上。
model.evaluate(x_test,y_test,verbose=2)
輸出結果:
[0.0841476172208786, 0.9760000109672546]
在這個數據集上,這個圖片分類的模型訓練的準確率約等於98%。如果想了解更多關於這方面的內容,可以閱讀 TensorFlow tutorials.(https://tensorflow.google.cn/tutorials/)
如果你想你的模型返回一個概率,你可以包裹你的model對象,並且附帶softmax。如下:
probability_model = tf.keras.Sequential([
model,
tf.keras.layers.Softmax()
])
print(probability_model(x_test[:5]))
輸出結果:
tf.Tensor(
[[6.64528272e-08 7.67723236e-08 7.54364783e-06 3.53052717e-04
6.87170390e-11 3.60848901e-07 2.23684981e-12 9.99635696e-01
1.65168018e-07 3.13543705e-06]
[4.35788428e-09 1.10600071e-04 9.99865294e-01 8.18846547e-06
7.86288894e-14 1.47804167e-06 2.56378456e-07 2.10501798e-12
1.42125145e-05 1.13504149e-16]
[4.76928733e-07 9.98138189e-01 6.59115176e-05 5.00368878e-05
1.88655074e-04 4.11117344e-06 2.92819695e-05 9.81064513e-04
5.41346439e-04 7.17659077e-07]
[9.99821723e-01 2.42099341e-10 8.32889509e-06 8.88995942e-07
4.28884217e-09 8.21065169e-06 1.50513850e-04 8.60030013e-06
5.22520800e-08 1.76794254e-06]
[7.08432890e-06 4.52548754e-09 8.52964968e-06 8.69868177e-09
9.96985734e-01 3.78219802e-08 3.12174535e-07 1.97316593e-04
2.47502101e-07 2.80086603e-03]], shape=(5, 10), dtype=float32)
整體的代碼是:
# -*- coding: UTF-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Add a channels dimension
x_train = x_train[...,tf.newaxis]
x_test = x_test[...,tf.newaxis]
train_ds = tf.data.Dataset.from_tensor_slices(
(x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
model = MyModel()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
predictions = model(images)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(labels, predictions)
@tf.function
def test_step(images, labels):
predictions = model(images)
t_loss = loss_object(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
EPOCHS = 5
for epoch in range(EPOCHS):
# 在下一個epoch開始是,重置評估指標
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
for images, labels in train_ds:
train_step(images,labels)
for test_images,test_labels in test_ds:
test_step(test_images,test_labels)
template = "Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}"
print(template.format(
epoch + 1,
train_loss.result(),
train_accuracy.result() * 100,
test_loss.result(),
test_accuracy.result() * 100))
輸出:
[[-0.28416255 0.22787774 0.1536147 0.1858716 0.06911337 0.82922894
0.21581247 0.5613003 0.44787267 -0.02493771]]
[[0.0566914 0.09460051 0.0878297 0.09070901 0.0807129 0.17260642
0.09346598 0.13203742 0.11787888 0.07346781]]
1.7567413
Epoch 1/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2980 - accuracy: 0.9137
Epoch 2/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.1436 - accuracy: 0.9563
Epoch 3/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.1051 - accuracy: 0.9681
Epoch 4/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0875 - accuracy: 0.9730
Epoch 5/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0754 - accuracy: 0.9762
313/313 - 0s - loss: 0.0728 - accuracy: 0.9784
[0.07283254712820053, 0.9783999919891357]
tf.Tensor(
[[6.64528272e-08 7.67723236e-08 7.54364783e-06 3.53052717e-04
6.87170390e-11 3.60848901e-07 2.23684981e-12 9.99635696e-01
1.65168018e-07 3.13543705e-06]
[4.35788428e-09 1.10600071e-04 9.99865294e-01 8.18846547e-06
7.86288894e-14 1.47804167e-06 2.56378456e-07 2.10501798e-12
1.42125145e-05 1.13504149e-16]
[4.76928733e-07 9.98138189e-01 6.59115176e-05 5.00368878e-05
1.88655074e-04 4.11117344e-06 2.92819695e-05 9.81064513e-04
5.41346439e-04 7.17659077e-07]
[9.99821723e-01 2.42099341e-10 8.32889509e-06 8.88995942e-07
4.28884217e-09 8.21065169e-06 1.50513850e-04 8.60030013e-06
5.22520800e-08 1.76794254e-06]
[7.08432890e-06 4.52548754e-09 8.52964968e-06 8.69868177e-09
9.96985734e-01 3.78219802e-08 3.12174535e-07 1.97316593e-04
2.47502101e-07 2.80086603e-03]], shape=(5, 10), dtype=float32)