[TensorFlow 2.0] Keras三种搭建模型的方式——序列式、函数式、Model子类

数据集:
在这里插入图片描述

序列式(Sequential API)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# coding = utf-8
import numpy as np
import tensorflow as tf
from tensorflow import keras

# Prepare data X_train: ndarray,(60000, 28, 28) y_train: ndarray, (60000,)
(X_train, y_train), (X_valid, y_valid) = keras.datasets.fashion_mnist.load_data()
X_train = X_train / 255.0
X_valid = X_valid / 255.0

# Build model
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),  # input layer
    keras.layers.Dense(128, activation='relu'),  # hidden layer
    keras.layers.Dense(10, activation='softmax')  # output layer
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train model
model.fit(X_train, y_train, epochs=10)

# Evaluate model
valid_loss, valid_acc = model.evaluate(X_valid, y_valid, verbose=1)
print(f"Valid loss:{valid_loss}")
print(f"Valid accuracy:{valid_acc}")

# Make one prediction
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
y_predicts = model.predict(X_valid)
print(y_predicts[0])
y_index = np.argmax(y_predicts[0])
print(y_index)
y_label = class_names[y_index]
print(y_label)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Train on 60000 samples
Epoch 1/10
60000/60000 [==============================] - 3s 50us/sample - loss: 0.4946 - accuracy: 0.8256
Epoch 2/10
60000/60000 [==============================] - 2s 41us/sample - loss: 0.3703 - accuracy: 0.8667
Epoch 3/10
60000/60000 [==============================] - 2s 41us/sample - loss: 0.3342 - accuracy: 0.8781
Epoch 4/10
60000/60000 [==============================] - 2s 40us/sample - loss: 0.3099 - accuracy: 0.8863
Epoch 5/10
60000/60000 [==============================] - 2s 41us/sample - loss: 0.2922 - accuracy: 0.8919
Epoch 6/10
60000/60000 [==============================] - 3s 43us/sample - loss: 0.2778 - accuracy: 0.8971
Epoch 7/10
60000/60000 [==============================] - 3s 42us/sample - loss: 0.2664 - accuracy: 0.9019
Epoch 8/10
60000/60000 [==============================] - 3s 42us/sample - loss: 0.2557 - accuracy: 0.9050
Epoch 9/10
60000/60000 [==============================] - 2s 41us/sample - loss: 0.2462 - accuracy: 0.9080
Epoch 10/10
60000/60000 [==============================] - 3s 42us/sample - loss: 0.2367 - accuracy: 0.9121

10000/10000 [==============================] - 0s 44us/sample - loss: 0.3562 - accuracy: 0.8769
Valid loss:0.3562260051012039
Valid accuracy:0.8769000172615051

[5.4778798e-08 4.9685767e-10 1.1157469e-08 8.1381698e-12 3.3885932e-09 6.8309897e-04 7.2993487e-09 2.9371075e-02 2.6147852e-08 9.6994579e-01]
9
Ankle boot

sparse_categorical_crossentropy和categorical_crossentropy区别:如果输出只用一位整数表示类别,则用sparse_categorical_crossentropy,如果输出是One-Hot编码了的,则用categorical_crossentropy。

函数式(Functional API)

1
2
3
4
5
6
7
# Build model
inputs = keras.Input(shape=(28, 28))
x = keras.layers.Flatten()(inputs)
x = keras.layers.Dense(128, activation='relu')(x)
outputs = keras.layers.Dense(10, activation='softmax')(x)

model = keras.Model(inputs, outputs)

Model子类

继承Keras的Model类,并重写call方法即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Build model
class MyModel(keras.Model):
    def __init__(self):
        super(MyModel, self).__init__()
        self.input_layer = keras.layers.Flatten(input_shape=(28, 28))
        self.hidden_layer = keras.layers.Dense(128, activation='relu')
        self.output_layer = keras.layers.Dense(10, activation='softmax')

    def call(self, inputs):
        x = self.input_layer(inputs)
        x = self.hidden_layer(x)
        x = self.output_layer(x)
        return x


model = MyModel()