Keras是一个用于构建和训练深度学习模型的高级API。 它用于快速原型设计,高级研究和生产,具有三个主要优点:
编写自定义构建块以表达研究的新想法。 创建新图层,损失函数并开发最先进的模型。
tf.keras是 Keras API 在TensorFlow 里的实现。这是一个高级API,用于构建和训练模型,同时兼容 TensorFlow 的绝大部分功能,比如,eager execution, tf.data模块及 Estimators。 tf.keras使得 TensorFlow 更容易使用,且保持 TF 的灵活性和性能。
首先需要在您的代码开始时导入tf.keras:
import tensorflow as tffrom tensorflow import keras1.2.
tf.keras可以运行任何与Keras兼容的代码,但请记住:
在Keras中,您可以组装图层来构建模型。 模型(通常)是图层图。 最常见的模型类型是一堆层:tf.keras.Sequential 模型。
构建一个简单的全连接网络(即多层感知器):
model = keras.Sequential()# Adds a densely-connected layer with 64 units to the model:model
.add(keras.layers.Dense(64, activation='relu'))# Add another:model
.add(keras.layers.Dense(64, activation='relu'))# Add a softmax layer with 10 output units:model
.add(keras.layers.Dense(10, activation='softmax'))1.2.3.4.5.6.7.
在 tf.keras.layers 中有很多层,下面是一些通用的构造函数的参数:
以下实例化tf.keras。 layers.Dense图层使用构造函数参数:
# Create a sigmoid layer:layers.Dense(64, activation='sigmoid')# Or:layers
.Dense(64, activation=tf.sigmoid)# A linear layer with L1 regularization of factor 0.01
applied to the kernel matrix:layers.Dense(64, kernel_regularizer=keras.regularizers.l1(0.01))
# A linear layer with L2 regularization of factor 0.01 applied to the bias vector:layers
.Dense(64, bias_regularizer=keras.regularizers.l2(0.01))
# A linear layer with a kernel initialized to a random orthogonal matrix:layers
.Dense(64, kernel_initializer='orthogonal')# A linear layer with a bias vector initialized to 2.0s:layers
.Dense(64, bias_initializer=keras.initializers.constant(2.0))1.2.3.4.5.6.7.8.9.10.11.12.13.14.
构建模型后,通过调用compile方法配置其训练过程:
model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='categorical_crossentropy',
metrics=['accuracy'])1.2.3.
tf.keras.Model.compile有三个重要参数:
指定方法:名称 或 可调用对象 from the tf.keras.metrics 模块。
以下显示了配置培训模型的几个示例:
# Configure a model for mean-squared error regression.model.compile(optimizer=tf.train.AdamOptimizer(0.01),
loss='mse', # mean squared error metrics=['mae'])
# mean absolute error# Configure a model for categorical classification.model
.compile(optimizer=tf.train.RMSPropOptimizer(0.01), loss=keras.losses.categorical_crossentropy,
metrics=[keras.metrics.categorical_accuracy])1.2.3.4.5.6.7.8.9.
对于小的数据集,可以直接使用 NumPy 格式的数据进行训练、评估模型。模型使用 fit 方法训练数据:
import numpy as npdata = np.random.random((1000, 32))labels = np.random.random((1000, 10))model
.fit(data, labels, epochs=10, batch_size=32)1.2.3.4.5.6.
tf.keras.Model.fit 有三个重要的参数:
这是使用validation_data的示例:
import numpy as npdata = np.random.random((1000, 32))labels = np.random.random((1000, 10))val_data = np
.random.random((100, 32))val_labels = np.random.random((100, 10))model.fit(data, labels, epochs=10,
batch_size=32, validation_data=(val_data, val_labels))1.2.3.4.5.6.7.8.9.10.
使用数据集API可扩展到大型数据集或多设备培训。 将tf.data.Dataset实例传递给fit方法:
使用 Datasets API 可扩展到大型数据集或多设备训练。 给 fit 方法传递一个 tf.data.Dataset 实例:
# Instantiates a toy dataset instance:dataset = tf.data.Dataset
.from_tensor_slices((data, labels))dataset = dataset.batch(32)dataset = dataset.repeat()
# Don't forget to specify `steps_per_epoch` when calling `fit` on a dataset.model
.fit(dataset, epochs=10, steps_per_epoch=30)1.2.3.4.5.6.7.
这里,fit方法使用steps_per_epoch参数 - 这是模型在移动到下一个epoch之前运行的训练步数。 由于数据集生成批量数据,因此此代码段不需要batch_size。
Dataset API 也可以用于验证:
dataset = tf.data.Dataset.from_tensor_slices((data, labels)) dataset = dataset.batch(32)
.repeat() val_dataset = tf.data.Dataset.from_tensor_slices((val_data, val_labels))
val_dataset = val_dataset.batch(32).repeat() model.fit(dataset, epochs=10, steps_per_epoch=30,
validation_data=val_dataset, validation_steps=3)1.
tf.keras.Model.evaluate 和 tf.keras.Model.predict 方法能够使用 NumPy 数据 和 tf.data.Dataset 数据。
要评估所提供数据的推理模式损失和指标:
model.evaluate(x, y, batch_size=32)model.evaluate(dataset, steps=30)1.2.3.
并且作为NumPy数组,预测所提供数据的推断中最后一层的输出:
model.predict(x, batch_size=32)model.predict(dataset, steps=30)1.2.3.
tf.keras.Sequential 模型只适用于多层简单堆叠网络,不能表示复杂模型。使用 Keras functional API 可以构建有复杂拓扑结构的模型。比如:
多输入模型(Multi-input models)
使用函数式API构建模型的工作方式如下:
以下示例使用函数式API构建一个简单,全连接的网络:
inputs = keras.Input(shape=(32,)) # Returns a placeholder tensor# A layer instance is callable on a tensor,
and returns a tensor.x = keras.layers.Dense(64, activation='relu')(inputs)x = keras.layers
.Dense(64, activation='relu')(x)predictions = keras.layers.Dense(10, activation='softmax')(x)
# Instantiate the model given inputs and outputs.model = keras.Model(inputs=inputs, outputs=predictions)
# The compile step specifies the training configuration.model.compile(optimizer=tf
.train.RMSPropOptimizer(0.001), loss='categorical_crossentropy',
metrics=['accuracy'])# Trains for 5 epochsmodel.fit(data, labels, batch_size=32, epochs=5)1.2.3.4.5.6.7
.8.9.10.11.12.13.14.15.16.17.
通过继承tf.keras.Model并定义自己的前向传递来构建完全可自定义的模型。 在init方法中创建图层并将它们设置为类实例的属性。 在call方法中定义正向传递。
当启用eager执行时,模型子类化特别有用,因为可以强制写入正向传递。
提示:为工作使用正确的API。 虽然模型子类化提供了灵活性,但其代价是更高的复杂性和更多的用户错误机会。 如果可能,请选择功能API。
以下示例显示了使用自定义正向传递的子类tf.keras.Model:
class MyModel(keras.Model): def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model') self.num_classes = num_classes
# Define your layers here. self.dense_1 = keras.layers.Dense(32, activation='relu')
self.dense_2 = keras.layers.Dense(num_classes, activation='sigmoid') def call(self, inputs):
# Define your forward pass here, # using layers you previously defined (in `__init__`).
x = self.dense_1(inputs) return self.dense_2(x) def compute_output_shape(self, input_shape):
# You need to override this function if you want to use the subclassed model
# as part of a functional-style model. # Otherwise, this method is optional.
shape = tf.TensorShape(input_shape).as_list() shape[-1] = self.num_classes
return tf.TensorShape(shape)# Instantiates the subclassed model.model = MyModel(num_classes=10)
# The compile step specifies the training configuration.model.compile(optimizer=tf
.train.RMSPropOptimizer(0.001), loss='categorical_crossentropy',
metrics=['accuracy'])# Trains for 5 epochs.model.fit(data, labels, batch_size=32, epochs=5)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.
通过继承tf.keras.layers.Layer并实现以下方法来创建自定义层:
这里有一个自定义 layer 的例子,该 layer 将输入和一个矩阵进行相乘:
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 layermodel = keras.Sequential([MyLayer(10),
keras.layers.Activation('softmax')])# The compile step specifies the training configurationmodel
.compile(optimizer=tf.train.RMSPropOptimizer(0.001), loss='categorical_crossentropy',
metrics=['accuracy'])# Trains for 5 epochs.model.fit(data, targets, batch_size=32, epochs=5)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.38.39.40.41.42.43.44.
回调是传递给模型的对象,用于在训练期间自定义和扩展其行为。 您可以编写自己的自定义回调,或使用包含以下内置的tf.keras.callbacks:
要使用tf.keras.callbacks.Callback,请将其传递给模型的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')]model.fit(data, labels, batch_size=32,
epochs=5, callbacks=callbacks, validation_data=(val_data, val_targets))1.2.3.4.5.6.7.8.
使用 tf.keras.Model.save_weights 来保存和加载模型的 weights:
# Save weights to a TensorFlow Checkpoint filemodel.save_weights('./my_model')# Restore the model's state,
# this requires a model with the same architecture.model.load_weights('my_model')1.2.3.4.5.6.
默认情况下,这会以 TensorFlow checkpoint 格式保存模型的 weights。weights 也可以保存为 HDF5 格式(Keras 默认的保存格式):
# Save weights to a HDF5 filemodel.save_weights('my_model.h5', save_format='h5')
# Restore the model's statemodel.load_weights('my_model.h5')1.2.3.4.5.
一个模型的 configuration 可以被保存,序列化过程中不包含任何 weights。保存的 configuration 可以用来重新创建、初始化出相同的模型,即使没有模型原始的定义代码。Keras 支持 JSON,YAML 序列化格式:
# Serialize a model to JSON formatjson_string = model.to_json()
# Recreate the model (freshly initialized)fresh_model = keras.models.model_from_json(json_string)
# Serializes a model to YAML formatyaml_string = model.to_yaml()# Recreate the modelfresh_model = keras
.models.model_from_yaml(yaml_string)1.2.3.4.5.6.7.8.9.10.11.
注意:子类模型不可序列化,因为它们的体系结构由调用方法体中的Python代码定义。
整个模型可以保存到包含权重值,模型配置甚至优化器配置的文件中。 这允许您检查模型并稍后从完全相同的状态恢复训练 - 无需访问原始代码。
# Create a trivial modelmodel = keras.Sequential([ keras.layers.Dense(10, activation='softmax',
input_shape=(32,)), keras.layers.Dense(10, activation='softmax')])model.compile(optimizer='rmsprop',
loss='categorical_crossentropy', metrics=['accuracy'])model.fit(data, targets, batch_size=32,
epochs=5)# Save entire model to a HDF5 filemodel.save('my_model.h5')
# Recreate the exact same model, including weights and optimizer.model = keras.models
.load_model('my_model.h5')1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.
Eager execution是一个必要的编程环境,可以立即评估操作。 这对于Keras不是必需的,但是由tf.keras支持,对于检查程序和调试很有用。
tf.keras 里所有的模型构建 API 兼容 eager execution。并且,在编写 model subclassing 和 custom layers 时使用 eager execution
虽然可以使用Sequential和函数式API,但是eager execution尤其有利于模型子类化和构建自定义层 - 需要您将正向传递作为代码编写的API(而不是通过组合现有层来创建模型的API)。
请看 eager execution guide 里的例子:使用 Keras models with custom training loops and tf.GradientTape。
Estimators API 被用来在分布时环境训练模型。Estimator API 旨在大型数据集的分布式训练,该 API 能够导出工业生产可用的模型。
一个 tf.keras.Model 可以用 tf.estimator API 来训练(通过 tf.keras.estimator.model_to_estimator 将模型转为一个 tf.estimator.Estimator 对象)。详情见 Creating Estimators from Keras models。
model = keras.Sequential([layers.Dense(10,activation='softmax'),
layers.Dense(10,activation='softmax')])model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),
loss='categorical_crossentropy', metrics=['accuracy'])estimator = keras.estimator
.model_to_estimator(model)1.2.3.4.5.6.7.8.
注意: 可以通过开启 eager execution 来调试 Estimator input functions、检查数据。
tf.keras 模型可以使用 tf.contrib.distribute.DistributionStrategy 在多个 GPU 上运行。此API在多个GPU上提供分布式培训,几乎不对现有代码进行任何更改。
当前,tf.contrib.distribute.MirroredStrategy 是唯一支持的分布式策略。MirroredStrategy 对图进行复制,以同步的方式训练,并且梯度最后聚集在一个机器上。 为了使用 DistributionStrategy with Keras,首先用 tf.keras.estimator.model_to_estimator 将 tf.keras.Model 转化为一个 tf.estimator.Estimator,然后训练转化来的estimator。
以下示例在单个计算机上的多个GPU之间分发tf.keras.Model。
首先,定义一个简单的模型:
model = keras.Sequential()model.add(keras.layers.Dense(16, activation='relu', input_shape=(10,)))model
.add(keras.layers.Dense(1, activation='sigmoid'))optimizer = tf.train.GradientDescentOptimizer(0.2)model
.compile(loss='binary_crossentropy', optimizer=optimizer)model.summary()1.2.3.4.5.6.7.8.
定义输入pipeline。 input_fn返回一个tf.data.Dataset对象,用于在多个设备之间分配数据 - 每个设备处理输入批处理的一部分。
def input_fn(): x = np.random.random((1024, 10)) y = np.random.randint(2, size=(1024, 1))
x = tf.cast(x, tf.float32) dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.repeat(10) dataset = dataset.batch(32) return dataset1.2.3.4.5.6.7.8.
接下来,创建一个 tf.estimator.RunConfig 并设置 train_distribute 参数为 tf.contrib.distribute.MirroredStrategy 实例。当创建 MirroredStrategy 时,你可以指定一个设备列表 或 通过 num_gpus 参数设置 GPU 的数量。默认使用所有的 GPU。如下所示:
strategy = tf.contrib.distribute.MirroredStrategy()config = tf
.estimator.RunConfig(train_distribute=strategy)1.2.
将 Keras model 转为一个 tf.estimator.Estimator 实例。
keras_estimator = keras.estimator.model_to_estimator( keras_model=model, config=config,
model_dir='/tmp/model_dir')1.2.3.4.
最后,通过提供input_fn和steps参数来训练Estimator实例:
keras_estimator.train(input_fn=input_fn, steps=10)1.
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删