0%

TensorFlow2.0 In Action

TensorFlow2.0简化的模型开发流程

  • 使用tf.data加载数据
  • 使用tf.keras构建模型,也可以使用premade estimator来验证模型(使用tesorflow hub进行迁移学习)
  • 使用eager mode进行运行和调试
  • 使用分发策略来进行分布式训练
  • 导出到SavedModel
  • 使用TesorFlow Serve、TensorFlow Lite、TensorFlow.js部署模型

TensorFlow-Keras

tf.keras和keras的区别

  • tf.keras全面支持eager mode
  • tf.keras支持基于tf.data的模型训练
  • tf.keras支持TPU训练
  • tf.keras支持tf.distribution中的分布式策略
  • tf.keras可以与TensorFlow中的estimator集成
  • tf.keras可以保存为SavedModel

搭建分类模型

数据读取与展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tensorflow as tf
from tensorflow import keras

fashion_mnist = keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
x_valid, x_train = x_train_all[:5000], x_train_all[5000:] #将数据集的前5000张作为训练集,后面的作为验证集
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]

print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)


(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)
1
2
3
4
5
# 展示一张图片
def show_single_image(img_arr):
plt.imshow(img_arr, cmap="binary")
plt.show()
show_single_image(x_train_all[0]) # 查看训练集中的第一张图片

show_image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 展示多张图片
def show_imgs(n_rows, n_cols, x_data, y_data, class_names):
assert len(x_data) == len(y_data)
assert n_rows * n_cols < len(x_data) # 查看图片的数量不能大于原本的样本数
plt.figure(figsize = (n_cols * 1.4, n_rows * 1.6))
for row in range(n_rows):
for col in range(n_cols):
index = n_cols * row + col
plt.subplot(n_rows, n_cols, index+1)
plt.imshow(x_data[index], cmap="binary",
interpolation = 'nearest')
plt.axis('off')
plt.title(class_names[y_data[index]])
plt.show()

class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress',
'Coat', 'Sandal', 'Shirt','Sneaker',
'Bag', 'Ankle boot']
show_imgs(3, 5, x_train, y_train, class_names)

show_images

模型构建

1
2
3
4
5
6
7
8
9
10
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(10, activation="softmax"))

model.compile(loss="sparse_categorical_crossentropy",
optimizer="sgd",
metrics=["accuracy"])
model.layers
1
2
3
4
[<tensorflow.python.keras.layers.core.Flatten at 0x12e0e8a58>,
<tensorflow.python.keras.layers.core.Dense at 0x12e1777b8>,
<tensorflow.python.keras.layers.core.Dense at 0x12dfdb3c8>,
<tensorflow.python.keras.layers.core.Dense at 0x12dfdbdd8>]
  • 查看模型层次
1
model.summary()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_4 (Flatten) (None, 784) 0
_________________________________________________________________
dense_12 (Dense) (None, 300) 235500
_________________________________________________________________
dense_13 (Dense) (None, 100) 30100
_________________________________________________________________
dense_14 (Dense) (None, 10) 1010
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
  • 开始训练
1
2
history = model.fit(x_train, y_train, epochs=10,
validation_data=(x_valid, y_valid))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 4s 80us/sample - loss: 42355302163052.2422 - accuracy: 0.0995 - val_loss: 2.3028 - val_accuracy: 0.0914
Epoch 2/10
55000/55000 [==============================] - 4s 73us/sample - loss: 2.3027 - accuracy: 0.0994 - val_loss: 2.3026 - val_accuracy: 0.1012
Epoch 3/10
55000/55000 [==============================] - 4s 71us/sample - loss: 2.3027 - accuracy: 0.0986 - val_loss: 2.3027 - val_accuracy: 0.1002
Epoch 4/10
55000/55000 [==============================] - 4s 70us/sample - loss: 2.3027 - accuracy: 0.0977 - val_loss: 2.3027 - val_accuracy: 0.0986
Epoch 5/10
55000/55000 [==============================] - 4s 72us/sample - loss: 2.3027 - accuracy: 0.0971 - val_loss: 2.3029 - val_accuracy: 0.0914
Epoch 6/10
55000/55000 [==============================] - 4s 75us/sample - loss: 2.3027 - accuracy: 0.0973 - val_loss: 2.3028 - val_accuracy: 0.0914
Epoch 7/10
55000/55000 [==============================] - 4s 72us/sample - loss: 2.3027 - accuracy: 0.0981 - val_loss: 2.3027 - val_accuracy: 0.0986
Epoch 8/10
55000/55000 [==============================] - 4s 72us/sample - loss: 2.3027 - accuracy: 0.0968 - val_loss: 2.3028 - val_accuracy: 0.0914
Epoch 9/10
55000/55000 [==============================] - 4s 73us/sample - loss: 2.3027 - accuracy: 0.0982 - val_loss: 2.3029 - val_accuracy: 0.0914
Epoch 10/10
55000/55000 [==============================] - 4s 73us/sample - loss: 2.3027 - accuracy: 0.0969 - val_loss: 2.3028 - val_accuracy: 0.0914
  • 画图
1
2
3
4
5
6
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0,1)
plt.show()
plot_learning_curves(history)

loss_model

Keras回调函数

搭建回归模型

搭建深度神经网络

实现wide&deep模型

Keras与scikit-learn实现超参数搜索

参考文献