1+ {
2+ "nbformat" : 4 ,
3+ "nbformat_minor" : 0 ,
4+ "metadata" : {
5+ "colab" : {
6+ "name" : " optimizers_lazyadam.ipynb" ,
7+ "version" : " 0.3.2" ,
8+ "provenance" : [],
9+ "collapsed_sections" : []
10+ },
11+ "kernelspec" : {
12+ "name" : " python3" ,
13+ "display_name" : " Python 3"
14+ },
15+ "accelerator" : " GPU"
16+ },
17+ "cells" : [
18+ {
19+ "metadata" : {
20+ "colab_type" : " text" ,
21+ "id" : " Tce3stUlHN0L"
22+ },
23+ "cell_type" : " markdown" ,
24+ "source" : [
25+ " ##### Copyright 2019 The TensorFlow Authors.\n " ,
26+ " \n "
27+ ]
28+ },
29+ {
30+ "metadata" : {
31+ "colab_type" : " code" ,
32+ "id" : " tuOe1ymfHZPu" ,
33+ "cellView" : " form" ,
34+ "colab" : {}
35+ },
36+ "cell_type" : " code" ,
37+ "source" : [
38+ " #@title Licensed under the Apache License, Version 2.0\n " ,
39+ " # you may not use this file except in compliance with the License.\n " ,
40+ " # You may obtain a copy of the License at\n " ,
41+ " #\n " ,
42+ " # https://www.apache.org/licenses/LICENSE-2.0\n " ,
43+ " #\n " ,
44+ " # Unless required by applicable law or agreed to in writing, software\n " ,
45+ " # distributed under the License is distributed on an \" AS IS\" BASIS,\n " ,
46+ " # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n " ,
47+ " # See the License for the specific language governing permissions and\n " ,
48+ " # limitations under the License."
49+ ],
50+ "execution_count" : 0 ,
51+ "outputs" : []
52+ },
53+ {
54+ "metadata" : {
55+ "colab_type" : " text" ,
56+ "id" : " MfBg1C5NB3X0"
57+ },
58+ "cell_type" : " markdown" ,
59+ "source" : [
60+ " # TensorFlow Addons Optimizers: LazyAdam\n " ,
61+ " \n " ,
62+ " <table class=\" tfo-notebook-buttons\" align=\" left\" >\n " ,
63+ " <td>\n " ,
64+ " <a target=\" _blank\" href=\" https://colab.research.google.com/github/tensorflow/addons/blob/master/tensorflow_addons/examples/notebooks/optimizers_lazyadam.ipynb\" ><img src=\" https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n " ,
65+ " </td>\n " ,
66+ " <td>\n " ,
67+ " <a target=\" _blank\" href=\" https://github.com/tensorflow/addons/blob/master/tensorflow_addons/examples/notebooks/optimizers_lazyadam.ipynb\" ><img src=\" https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n " ,
68+ " </td>\n " ,
69+ " </table>"
70+ ]
71+ },
72+ {
73+ "metadata" : {
74+ "colab_type" : " text" ,
75+ "id" : " xHxb-dlhMIzW"
76+ },
77+ "cell_type" : " markdown" ,
78+ "source" : [
79+ " ## Overview\n " ,
80+ " \n " ,
81+ " This notebook will demonstrate how to use the lazy adam optimizer from the Addons package.\n "
82+ ]
83+ },
84+ {
85+ "metadata" : {
86+ "colab_type" : " text" ,
87+ "id" : " MUXex9ctTuDB"
88+ },
89+ "cell_type" : " markdown" ,
90+ "source" : [
91+ " ## Setup"
92+ ]
93+ },
94+ {
95+ "metadata" : {
96+ "colab_type" : " code" ,
97+ "id" : " IqR2PQG4ZaZ0" ,
98+ "colab" : {}
99+ },
100+ "cell_type" : " code" ,
101+ "source" : [
102+ " !pip install tensorflow-gpu==2.0.0.a0\n " ,
103+ " !pip install tensorflow-addons\n " ,
104+ " from __future__ import absolute_import, division, print_function, unicode_literals\n " ,
105+ " \n " ,
106+ " import tensorflow as tf\n " ,
107+ " import tensorflow_addons as tfa\n " ,
108+ " import tensorflow_datasets as tfds\n " ,
109+ " import numpy as np\n " ,
110+ " from matplotlib import pyplot as plt"
111+ ],
112+ "execution_count" : 0 ,
113+ "outputs" : []
114+ },
115+ {
116+ "metadata" : {
117+ "id" : " ys65MwOLKnXq" ,
118+ "colab_type" : " code" ,
119+ "colab" : {}
120+ },
121+ "cell_type" : " code" ,
122+ "source" : [
123+ " # Hyperparameters\n " ,
124+ " batch_size=64\n " ,
125+ " epochs=10"
126+ ],
127+ "execution_count" : 0 ,
128+ "outputs" : []
129+ },
130+ {
131+ "metadata" : {
132+ "id" : " KR01t9v_fxbT" ,
133+ "colab_type" : " text"
134+ },
135+ "cell_type" : " markdown" ,
136+ "source" : [
137+ " # Build the Model"
138+ ]
139+ },
140+ {
141+ "metadata" : {
142+ "id" : " djpoAvfWNyL5" ,
143+ "colab_type" : " code" ,
144+ "colab" : {}
145+ },
146+ "cell_type" : " code" ,
147+ "source" : [
148+ " model = tf.keras.Sequential()\n " ,
149+ " model.add(tf.keras.layers.Dense(64, input_shape=(784,), activation='relu', name='dense_1'))\n " ,
150+ " model.add(tf.keras.layers.Dense(64, activation='relu', name='dense_2'))\n " ,
151+ " model.add(tf.keras.layers.Dense(10, activation='softmax', name='predictions'))"
152+ ],
153+ "execution_count" : 0 ,
154+ "outputs" : []
155+ },
156+ {
157+ "metadata" : {
158+ "id" : " 0_D7CZqkv_Hj" ,
159+ "colab_type" : " text"
160+ },
161+ "cell_type" : " markdown" ,
162+ "source" : [
163+ " # Prep the Data"
164+ ]
165+ },
166+ {
167+ "metadata" : {
168+ "id" : " U0bS3SyowBoB" ,
169+ "colab_type" : " code" ,
170+ "colab" : {}
171+ },
172+ "cell_type" : " code" ,
173+ "source" : [
174+ " # Load MNIST dataset as NumPy arrays\n " ,
175+ " dataset = {}\n " ,
176+ " num_validation = 10000\n " ,
177+ " (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()\n " ,
178+ " \n " ,
179+ " # Preprocess the data\n " ,
180+ " x_train = x_train.reshape(-1, 784).astype('float32') / 255\n " ,
181+ " x_test = x_test.reshape(-1, 784).astype('float32') / 255"
182+ ],
183+ "execution_count" : 0 ,
184+ "outputs" : []
185+ },
186+ {
187+ "metadata" : {
188+ "id" : " HYE-BxhOzFQp" ,
189+ "colab_type" : " text"
190+ },
191+ "cell_type" : " markdown" ,
192+ "source" : [
193+ " # Train and Evaluate\n " ,
194+ " \n " ,
195+ " Simply replace typical keras optimizers with the new tfa optimizer "
196+ ]
197+ },
198+ {
199+ "metadata" : {
200+ "id" : " NxfYhtiSzHf-" ,
201+ "colab_type" : " code" ,
202+ "colab" : {}
203+ },
204+ "cell_type" : " code" ,
205+ "source" : [
206+ " # Compile the model\n " ,
207+ " model.compile(\n " ,
208+ " optimizer=tfa.optimizers.LazyAdam(0.001), # Utilize TFA optimizer\n " ,
209+ " loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n " ,
210+ " metrics=['accuracy'])\n " ,
211+ " \n " ,
212+ " # Train the network\n " ,
213+ " history = model.fit(\n " ,
214+ " x_train,\n " ,
215+ " y_train,\n " ,
216+ " batch_size=batch_size,\n " ,
217+ " epochs=epochs)\n "
218+ ],
219+ "execution_count" : 0 ,
220+ "outputs" : []
221+ },
222+ {
223+ "metadata" : {
224+ "id" : " 1Y--0tK69SXf" ,
225+ "colab_type" : " code" ,
226+ "colab" : {
227+ "base_uri" : " https://localhost:8080/" ,
228+ "height" : 67
229+ },
230+ "outputId" : " 4a33a1c1-da98-4da9-b226-ee9af7b903d3"
231+ },
232+ "cell_type" : " code" ,
233+ "source" : [
234+ " # Evaluate the network\n " ,
235+ " print('Evaluate on test data:')\n " ,
236+ " results = model.evaluate(x_test, y_test, batch_size=128)\n " ,
237+ " print('Test loss = {0}, Test acc: {1}'.format(results[0], results[1]))"
238+ ],
239+ "execution_count" : 19 ,
240+ "outputs" : [
241+ {
242+ "output_type" : " stream" ,
243+ "text" : [
244+ " Evaluate on test data:\n " ,
245+ " 10000/10000 [==============================] - 0s 28us/sample - loss: 0.1149 - accuracy: 0.9762\n " ,
246+ " Test loss = 0.11493133022264228, Test acc: 0.9761999845504761\n "
247+ ],
248+ "name" : " stdout"
249+ }
250+ ]
251+ }
252+ ]
253+ }
0 commit comments