翻译自: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)