Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/zh-cn/addons/tutorials/optimizers_lazyadam.ipynb
25118 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

概述

此笔记本将演示如何使用 Addons 包中的 Lazy Adam 优化器。

LazyAdam

LazyAdam 是 Adam 优化器的一种变体,可以更高效地处理稀疏更新。原始的 Adam 算法为每个可训练变量维护两个移动平均累加器,这些累加器在每一步都会更新。此类为稀疏变量提供了更加懒惰的梯度更新处理。它仅更新当前批次中出现的稀疏变量索引的移动平均累加器,而不是更新所有索引的累加器。与原始的 Adam 优化器相比,它可以大幅提高某些应用的模型训练吞吐量。但是,它的语义与原始的 Adam 算法略有不同,这可能会产生不同的实验结果。

设置

!pip install -U tensorflow-addons
import tensorflow as tf import tensorflow_addons as tfa
# Hyperparameters batch_size=64 epochs=10

构建模型

model = tf.keras.Sequential([ tf.keras.layers.Dense(64, input_shape=(784,), activation='relu', name='dense_1'), tf.keras.layers.Dense(64, activation='relu', name='dense_2'), tf.keras.layers.Dense(10, activation='softmax', name='predictions'), ])

准备数据

# Load MNIST dataset as NumPy arrays dataset = {} num_validation = 10000 (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # Preprocess the data x_train = x_train.reshape(-1, 784).astype('float32') / 255 x_test = x_test.reshape(-1, 784).astype('float32') / 255

训练和评估

只需用新的 TFA 优化器替换典型的 Keras 优化器

# Compile the model model.compile( optimizer=tfa.optimizers.LazyAdam(0.001), # Utilize TFA optimizer loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) # Train the network history = model.fit( x_train, y_train, batch_size=batch_size, epochs=epochs)
# Evaluate the network print('Evaluate on test data:') results = model.evaluate(x_test, y_test, batch_size=128, verbose = 2) print('Test loss = {0}, Test acc: {1}'.format(results[0], results[1]))