一、函数式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))