一、函數式api
tf.keras.Sequential 模型只適用於多層簡單堆疊網絡,不能表示複雜模型。
使用 Keras functional API 可以構建有複雜拓撲結構的模型。比如:
- 多輸入模型(Multi-input models)
- 多輸出模型(Multi-output models)
- 有共享層的模型(同一層被調用多次)
- 具有非順序數據流的模型(如殘差連接)
使用函數式API構建模型要求:
- 層實例可調用並返回張量。
- 輸入張量和輸出張量用於定義 tf.keras.Model 實例
示例:
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers
train_x = np.random.random((1000, 72))
train_y = np.random.random((1000, 10))
val_x = np.random.random((200, 72))
val_y = np.random.random((200, 10))
input_x = tf.keras.Input(shape=(72,)) # 實例化Keras張量
hidden1 = layers.Dense(32, activation='relu')(input_x)
hidden2 = layers.Dense(16, activation='relu')(hidden1)
pred = layers.Dense(10, activation='softmax')(hidden2)
model = tf.keras.Model(inputs=input_x, outputs=pred)
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.categorical_crossentropy,
metrics=['accuracy'])
model.fit(train_x, train_y, batch_size=32, epochs=5)
二、通過類更好地模塊化自己的程序
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers
class MyModel(tf.keras.Model):
def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model')
self.num_classes = num_classes
self.layer1 = layers.Dense(32, activation='relu')
self.layer2 = layers.Dense(num_classes, activation='softmax')
def call(self, inputs):
h1 = self.layer1(inputs)
out = self.layer2(h1)
return out
def compute_output_shape(self, input_shape):
shape = tf.TensorShape(input_shape).as_list()
shape[-1] = self.num_classes
return tf.TensorShape(shape)
model = MyModel(num_classes=10)
model.compile(optimizer=tf.keras.optimizers.RMSprop(0.001),
loss=tf.keras.losses.categorical_crossentropy,
metrics=['accuracy'])
train_x = np.random.random((1000, 72))
train_y = np.random.random((1000, 10))
model.fit(train_x, train_y, batch_size=16, epochs=5)
三、自定義層
通過對 tf.keras.layers.Layer 進行子類化並通過下面方法創建自定義層:
- build:創建層的權重,add_weight 方法設置權重。
- call:定義前向傳播。
- compute_output_shape:在給定輸入形狀的情況下如何計算層的輸出形狀。
也可以通過實現 get_config 方法和 from_config 類方法序列化層。
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
import tensorflow.keras as keras
from tensorflow.keras import layers
class MyLayer(keras.layers.Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MyLayer, self).__init__(**kwargs)
def build(self, input_shape):
shape = tf.TensorShape((input_shape[1], self.output_dim))
# Create a trainable weight variable for this layer.
self.kernel = self.add_weight(name='kernel',
shape=shape,
initializer='uniform',
trainable=True)
# Be sure to call this at the end
super(MyLayer, self).build(input_shape)
def call(self, inputs):
return tf.matmul(inputs, self.kernel)
def compute_output_shape(self, input_shape):
shape = tf.TensorShape(input_shape).as_list()
shape[-1] = self.output_dim
return tf.TensorShape(shape)
def get_config(self):
base_config = super(MyLayer, self).get_config()
base_config['output_dim'] = self.output_dim
return base_config
@classmethod
def from_config(cls, config):
return cls(**config)
# Create a model using the custom layer
model = keras.Sequential([MyLayer(10),
keras.layers.Activation('softmax')])
# The compile step specifies the training configuration
model.compile(optimizer=tf.compat.v1.train.RMSPropOptimizer(0.001),
loss='categorical_crossentropy',
metrics=['accuracy'])
train_x = np.random.random((1000, 72))
train_y = np.random.random((1000, 10))
# Trains for 5 epochs.
model.fit(train_x, train_y, batch_size=32, epochs=5)
四、回調
回調用於在訓練期間自定義和擴展其行為。 可以編寫自己的自定義回調或使用包含以下內置的tf.keras.callbacks:
- tf.keras.callbacks.ModelCheckpoint:定期保存checkpoints。
- tf.keras.callbacks.LearningRateScheduler:動態改變學習速率。
- tf.keras.callbacks.EarlyStopping:當驗證集上的性能不再提高時,終止訓練。
- tf.keras.callbacks.TensorBoard:使用TensorBoard 監測模型的行為。
使用回調是將其傳遞給模型的fit方法:
callbacks = [
# Interrupt training if `val_loss` stops improving for over 2 epochs
keras.callbacks.EarlyStopping(patience=2, monitor='val_loss'),
# Write TensorBoard logs to `./logs` directory
keras.callbacks.TensorBoard(log_dir='./logs')
]
示例:
callbacks = [
tf.keras.callbacks.EarlyStopping(patience=2, monitor='val_loss'),
tf.keras.callbacks.TensorBoard(log_dir='./logs')
]
model.fit(train_x, train_y, batch_size=16, epochs=5,
callbacks=callbacks, validation_data=(val_x, val_y))
閱讀更多 編程圈 的文章