Warning: This project is deprecated. TensorFlow Addons has stopped development,
The project will only be providing minimal maintenance releases until May 2024. See the full
announcement here or on
github.
TensorFlow Addons 优化器:LazyAdam
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
概述
此笔记本将演示如何使用 Addons 包中的 Lazy Adam 优化器。
LazyAdam
LazyAdam 是 Adam 优化器的一种变体,可以更高效地处理稀疏更新。原始的 Adam 算法为每个可训练变量维护两个移动平均累加器,这些累加器在每一步都会更新。此类为稀疏变量提供了更加懒惰的梯度更新处理。它仅更新当前批次中出现的稀疏变量索引的移动平均累加器,而不是更新所有索引的累加器。与原始的 Adam 优化器相比,它可以大幅提高某些应用的模型训练吞吐量。但是,它的语义与原始的 Adam 算法略有不同,这可能会产生不同的实验结果。
设置
pip install -q -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)
Epoch 1/10
938/938 [==============================] - 2s 2ms/step - loss: 0.3188 - accuracy: 0.9079
Epoch 2/10
938/938 [==============================] - 2s 2ms/step - loss: 0.1316 - accuracy: 0.9607
Epoch 3/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0970 - accuracy: 0.9706
Epoch 4/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0783 - accuracy: 0.9760
Epoch 5/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0622 - accuracy: 0.9805
Epoch 6/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0516 - accuracy: 0.9843
Epoch 7/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0431 - accuracy: 0.9863
Epoch 8/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0392 - accuracy: 0.9873
Epoch 9/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0332 - accuracy: 0.9893
Epoch 10/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0283 - accuracy: 0.9909
# 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]))
Evaluate on test data:
79/79 - 0s - loss: 0.0843 - accuracy: 0.9765
Test loss = 0.08429064601659775, Test acc: 0.9764999747276306
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2022-01-24。
[null,null,["最后更新时间 (UTC):2022-01-24。"],[],[],null,["# TensorFlow Addons Optimizers: LazyAdam\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|\n| [View on TensorFlow.org](https://www.tensorflow.org/addons/tutorials/optimizers_lazyadam) | [Run in Google Colab](https://colab.research.google.com/github/tensorflow/addons/blob/master/docs/tutorials/optimizers_lazyadam.ipynb) | [View source on GitHub](https://github.com/tensorflow/addons/blob/master/docs/tutorials/optimizers_lazyadam.ipynb) | [Download notebook](https://storage.googleapis.com/tensorflow_docs/addons/docs/tutorials/optimizers_lazyadam.ipynb) |\n\nOverview\n--------\n\nThis notebook will demonstrate how to use the lazy adam optimizer from the Addons package.\n\nLazyAdam\n--------\n\n\u003e LazyAdam is a variant of the Adam optimizer that handles sparse updates more efficiently.\n\u003e The original Adam algorithm maintains two moving-average accumulators for\n\u003e each trainable variable; the accumulators are updated at every step.\n\u003e This class provides lazier handling of gradient updates for sparse\n\u003e variables. It only updates moving-average accumulators for sparse variable\n\u003e indices that appear in the current batch, rather than updating the\n\u003e accumulators for all indices. Compared with the original Adam optimizer,\n\u003e it can provide large improvements in model training throughput for some\n\u003e applications. However, it provides slightly different semantics than the\n\u003e original Adam algorithm, and may lead to different empirical results.\n\nSetup\n-----\n\n pip install -U tensorflow-addons\n\n import tensorflow as tf\n import tensorflow_addons as tfa\n\n # Hyperparameters\n batch_size=64\n epochs=10\n\nBuild the Model\n---------------\n\n model = tf.keras.Sequential([\n tf.keras.layers.Dense(64, input_shape=(784,), activation='relu', name='dense_1'),\n tf.keras.layers.Dense(64, activation='relu', name='dense_2'),\n tf.keras.layers.Dense(10, activation='softmax', name='predictions'),\n ])\n\nPrepare the Data\n----------------\n\n # Load MNIST dataset as NumPy arrays\n dataset = {}\n num_validation = 10000\n (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n\n # Preprocess the data\n x_train = x_train.reshape(-1, 784).astype('float32') / 255\n x_test = x_test.reshape(-1, 784).astype('float32') / 255\n\nTrain and Evaluate\n------------------\n\nSimply replace typical keras optimizers with the new tfa optimizer \n\n # Compile the model\n model.compile(\n optimizer=tfa.optimizers.LazyAdam(0.001), # Utilize TFA optimizer\n loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n metrics=['accuracy'])\n\n # Train the network\n history = model.fit(\n x_train,\n y_train,\n batch_size=batch_size,\n epochs=epochs)\n\n```\nEpoch 1/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.3141 - accuracy: 0.9086\nEpoch 2/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.1447 - accuracy: 0.9574\nEpoch 3/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.1064 - accuracy: 0.9681\nEpoch 4/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.0835 - accuracy: 0.9751\nEpoch 5/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.0665 - accuracy: 0.9798\nEpoch 6/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.0566 - accuracy: 0.9827\nEpoch 7/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.0472 - accuracy: 0.9852\nEpoch 8/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.0412 - accuracy: 0.9869\nEpoch 9/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.0353 - accuracy: 0.9890\nEpoch 10/10\n938/938 [==============================] - 2s 2ms/step - loss: 0.0320 - accuracy: 0.9901\n``` \n\n # Evaluate the network\n print('Evaluate on test data:')\n results = model.evaluate(x_test, y_test, batch_size=128, verbose = 2)\n print('Test loss = {0}, Test acc: {1}'.format(results[0], results[1]))\n\n```\nEvaluate on test data:\n79/79 - 0s - loss: 0.0990 - accuracy: 0.9741 - 236ms/epoch - 3ms/step\nTest loss = 0.09902079403400421, Test acc: 0.9740999937057495\n```"]]