경고: 이 프로젝트는 더 이상 사용되지 않습니다 . TensorFlow Addons는 개발을 중단했습니다. 이 프로젝트는 2024년 5월까지 최소한의 유지 관리 릴리스만 제공합니다. 여기 또는 github 에서 전체 발표를 참조하세요.
TensorFlow 애드온 옵티마이저: LazyAdam
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
개요
이 노트북은 애드온 패키지에서 게으른 adam 옵티마이저를 사용하는 방법을 보여줍니다.
LazyAdam
LazyAdam은 희소 업데이트를 보다 효율적으로 처리하는 Adam 옵티마이저의 변형입니다. 원래 Adam 알고리즘은 각 훈련 가능한 변수에 대해 두 개의 이동 평균 누산기(moving-average accumulator)를 유지합니다. 누산기는 모든 단계에서 업데이트됩니다. 이 클래스는 희소 변수에 대한 그래디언트 업데이트의 지연 처리를 제공합니다. 모든 인덱스에 대한 누산기를 업데이트하지 않고 현재 배치에 나타나는 희소 변수 인덱스에 대한 이동 평균 누산기만 업데이트합니다. 원래 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
훈련 및 평가하기
일반적인 keras 옵티마이저를 새로운 tfa 옵티마이저로 간단히 교체합니다.
# 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.3173 - accuracy: 0.9084
Epoch 2/10
938/938 [==============================] - 2s 2ms/step - loss: 0.1410 - accuracy: 0.9577
Epoch 3/10
938/938 [==============================] - 2s 2ms/step - loss: 0.1009 - accuracy: 0.9696
Epoch 4/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0814 - accuracy: 0.9750
Epoch 5/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0663 - accuracy: 0.9790
Epoch 6/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0539 - accuracy: 0.9828
Epoch 7/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0467 - accuracy: 0.9851
Epoch 8/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0402 - accuracy: 0.9873
Epoch 9/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0348 - accuracy: 0.9883
Epoch 10/10
938/938 [==============================] - 2s 2ms/step - loss: 0.0297 - accuracy: 0.9903
# 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.0851 - accuracy: 0.9754
Test loss = 0.08514752238988876, Test acc: 0.9753999710083008
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2022-01-24(UTC)
[null,null,["최종 업데이트: 2022-01-24(UTC)"],[],[],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```"]]