TensorFlow Keras 官方教程
TensorFlow版本:1.10.0 > Guide >教程地址:https://tensorflow.google.cn/guide/keras
Keras 簡介
Keras是一個用于構建和訓練深度學習模型的高級API。 它用于快速原型設計,高級研究和生產,具有三個主要優點:
- 用戶友好
Keras具有針對常見用例優化的簡單,一致的界面。 它為用戶錯誤提供清晰且可操作的反饋。 - 模塊化和可組合
Keras模型是通過將可配置的構建塊連接在一起而制定的,幾乎沒有限制。 - 易于擴展
編寫自定義構建塊以表達研究的新想法。 創建新圖層,損失函數并開發最先進的模型。
1. 導入 tf.keras
tf.keras是 Keras API 在TensorFlow 里的實現。這是一個高級API,用于構建和訓練模型,同時兼容 TensorFlow 的絕大部分功能,比如,eager execution, tf.data模塊及 Estimators。 tf.keras使得 TensorFlow 更容易使用,且保持 TF 的靈活性和性能。
首先需要在您的代碼開始時導入tf.keras:
tf.keras可以運行任何與Keras兼容的代碼,但請記住:
- 最新TensorFlow版本中的tf.keras版本可能與PyPI的最新keras版本不同。 檢查tf.keras.version。
- 保存模型的權重時,tf.keras默認為 checkpoint 格式。 通過save_format ='h5'使用HDF5。
2. 構建一個簡單的模型
2.1 Sequential model
在Keras中,您可以組裝圖層來構建模型。 模型(通常)是圖層圖。 最常見的模型類型是一堆層:tf.keras.Sequential 模型。
構建一個簡單的全連接網絡(即多層感知器):
2.2 Configure the layers
在 tf.keras.layers 中有很多層,下面是一些通用的構造函數的參數:
- activation:設置層的激活函數。 此參數由內置函數的名稱或可調用對象指定。 默認情況下,不應用任何激活。
- kernel_initializer 和 bias_initializer:設置層創建時,權重和偏差的初始化方法。指定方法:名稱 或 可調用對象。默認為"Glorot uniform" initializer。
- kernel_regularizer 和 bias_regularizer:設置層的權重、偏差的正則化方法。比如:L1 或 L2 正則。默認為空。
以下實例化tf.keras。 layers.Dense圖層使用構造函數參數:
3. Train and evaluate
3.1 Set up training
構建模型后,通過調用compile方法配置其訓練過程:
model.compile(optimizer=tf.train.AdamOptimizer(0.001),loss='categorical_crossentropy',metrics=['accuracy'])tf.keras.Model.compile有三個重要參數:
- optimizer:訓練過程的優化方法。此參數通過 tf.train 模塊的優化方法的實例來指定,比如:AdamOptimizer, RMSPropOptimizer, GradientDescentOptimizer。
- loss:訓練過程中使用的損失函數(通過最小化損失函數來訓練模型)。 常見的選擇包括:均方誤差(mse),categorical_crossentropy和binary_crossentropy。 損失函數由名稱或通過從tf.keras.losses模塊傳遞可調用對象來指定。
- metrics:訓練過程中,監測的指標(Used to monitor training)。
指定方法:名稱 或 可調用對象 from the tf.keras.metrics 模塊。
以下顯示了配置培訓模型的幾個示例:
3.2 Input NumPy data
對于小的數據集,可以直接使用 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)tf.keras.Model.fit 有三個重要的參數:
- epochs:訓練多少輪。(小批量)
- batch_size:當傳遞NumPy數據時,模型將數據分成較小的批次,并在訓練期間迭代這些批次。 此整數指定每個批次的大小。 請注意,如果樣本總數不能被批量大小整除,則最后一批可能會更小。
- validation_data:在對模型進行原型設計時,您希望輕松監控其在某些驗證數據上的性能。 傳遞這個參數 - 輸入和標簽的元組 - 允許模型在每個epoch的末尾以傳遞數據的推理模式顯示損失和度量。
這是使用validation_data的示例:
3.3 Input tf.data datasets
使用數據集API可擴展到大型數據集或多設備培訓。 將tf.data.Dataset實例傳遞給fit方法:
使用 Datasets API 可擴展到大型數據集或多設備訓練。 給 fit 方法傳遞一個 tf.data.Dataset 實例:
這里,fit方法使用steps_per_epoch參數 - 這是模型在移動到下一個epoch之前運行的訓練步數。 由于數據集生成批量數據,因此此代碼段不需要batch_size。
Dataset API 也可以用于驗證:
3.4 Evaluate and predict
tf.keras.Model.evaluate 和 tf.keras.Model.predict 方法能夠使用 NumPy 數據 和 tf.data.Dataset 數據。
要評估所提供數據的推理模式損失和指標:
并且作為NumPy數組,預測所提供數據的推斷中最后一層的輸出:
model.predict(x, batch_size=32)model.predict(dataset, steps=30)4. Build advanced models
4.1 Functional API
tf.keras.Sequential 模型只適用于多層簡單堆疊網絡,不能表示復雜模型。使用 Keras functional API 可以構建有復雜拓撲結構的模型。比如:
- 多輸入模型(Multi-input models)
- 多輸出模型(Multi-output models)
- 有共享層的模型(Models with shared layers (the same layer called several times))
- 具有非順序數據流的模型(Models with non-sequential data flows (e.g. residual connections))
使用函數式API構建模型的工作方式如下:
以下示例使用函數式API構建一個簡單,全連接的網絡:
4.2 Model subclassing
通過繼承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_classesreturn 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)4.3 自定義 layers
通過繼承tf.keras.layers.Layer并實現以下方法來創建自定義層:
- build:創建圖層的權重。 使用add_weight方法添加權重。
- call:定義前向傳播過程。
- compute_output_shape:指定在給定輸入形狀的情況下如何計算圖層的輸出形狀。
- 或者,可以通過實現get_config方法和from_config類方法來序列化層。
這里有一個自定義 layer 的例子,該 layer 將輸入和一個矩陣進行相乘:
5. 回調(Callbacks)
回調是傳遞給模型的對象,用于在訓練期間自定義和擴展其行為。 您可以編寫自己的自定義回調,或使用包含以下內置的tf.keras.callbacks:
- tf.keras.callbacks.ModelCheckpoint:定期保存checkpoints。
- tf.keras.callbacks.LearningRateScheduler:動態改變學習速率。
- tf.keras.callbacks.EarlyStopping:當驗證集上的性能不再提高時,終止訓練。
- tf.keras.callbacks.TensorBoard:使用TensorBoard 監測模型的行為。
要使用tf.keras.callbacks.Callback,請將其傳遞給模型的fit方法:
6. Save and restore
6.1 Weights only
使用 tf.keras.Model.save_weights 來保存和加載模型的 weights:
# Save weights to a TensorFlow Checkpoint file model.save_weights('./my_model')# Restore the model's state, # this requires a model with the same architecture. model.load_weights('my_model')默認情況下,這會以 TensorFlow checkpoint 格式保存模型的 weights。weights 也可以保存為 HDF5 格式(Keras 默認的保存格式):
# Save weights to a HDF5 file model.save_weights('my_model.h5', save_format='h5')# Restore the model's state model.load_weights('my_model.h5')6.2 Configuration only
一個模型的 configuration 可以被保存,序列化過程中不包含任何 weights。保存的 configuration 可以用來重新創建、初始化出相同的模型,即使沒有模型原始的定義代碼。Keras 支持 JSON,YAML 序列化格式:
# Serialize a model to JSON format json_string = model.to_json()# Recreate the model (freshly initialized) fresh_model = keras.models.model_from_json(json_string)# Serializes a model to YAML format yaml_string = model.to_yaml()# Recreate the model fresh_model = keras.models.model_from_yaml(yaml_string)注意:子類模型不可序列化,因為它們的體系結構由調用方法體中的Python代碼定義。
6.3 Entire model
整個模型可以保存到包含權重值,模型配置甚至優化器配置的文件中。 這允許您檢查模型并稍后從完全相同的狀態恢復訓練 - 無需訪問原始代碼。
# Create a trivial model model = 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 file model.save('my_model.h5')# Recreate the exact same model, including weights and optimizer. model = keras.models.load_model('my_model.h5')7. Eager execution
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。
8. Distribution
8.1 Estimators
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)注意: 可以通過開啟 eager execution 來調試 Estimator input functions、檢查數據。
8.2 Multiple GPUs
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。
首先,定義一個簡單的模型:
定義輸入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 dataset接下來,創建一個 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)將 Keras model 轉為一個 tf.estimator.Estimator 實例。
keras_estimator = keras.estimator.model_to_estimator(keras_model=model,config=config,model_dir='/tmp/model_dir')最后,通過提供input_fn和steps參數來訓練Estimator實例:
keras_estimator.train(input_fn=input_fn, steps=10)總結
以上是生活随笔為你收集整理的TensorFlow Keras 官方教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow版的“Hello W
- 下一篇: TensorFlow 完整的Tensor