From 0c4178397782b99a77f1b71125d1dcda74b42aeb Mon Sep 17 00:00:00 2001 From: aweers <32593524+aweers@users.noreply.github.com> Date: Thu, 25 Apr 2019 17:22:12 +0200 Subject: [PATCH 1/4] Add example for LR scheduler A short and simple example, especially for new users. It helps for a general understanding on how to use this callback. Open for feedback and suggestions. ;) --- tensorflow/python/keras/callbacks.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tensorflow/python/keras/callbacks.py b/tensorflow/python/keras/callbacks.py index 8d33bb22978..7db9fae5f3b 100644 --- a/tensorflow/python/keras/callbacks.py +++ b/tensorflow/python/keras/callbacks.py @@ -1168,6 +1168,23 @@ class LearningRateScheduler(Callback): (integer, indexed from 0) and returns a new learning rate as output (float). verbose: int. 0: quiet, 1: update messages. + + ```python + # Firstly, let's create a function which + # maps a given epoch to a learning rate. + # This function would keep the learning rate + # constant at 0.001 for the first ten epochs and + # let it decrease exponentially after that. + def scheduler(epoch): + if epoch < 10: + return 0.001 + else: + return 0.001 * tf.math.exp(0.1 * (10 - epoch)) + # Next, we need to set up the callback and train the model. + callback = tf.keras.callbacks.LearningRateScheduler(scheduler) + model.fit(data, labels, epochs=100, callbacks=[callback], + validation_data=(val_data, val_labels)) + ``` """ def __init__(self, schedule, verbose=0): From 1662bdc253e86036b2df1890dc8d807ea077d571 Mon Sep 17 00:00:00 2001 From: aweers <32593524+aweers@users.noreply.github.com> Date: Fri, 26 Apr 2019 19:26:02 +0200 Subject: [PATCH 2/4] Rephrase a commentand delete one Suggestions from @rchao --- tensorflow/python/keras/callbacks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow/python/keras/callbacks.py b/tensorflow/python/keras/callbacks.py index 7db9fae5f3b..6b01f314505 100644 --- a/tensorflow/python/keras/callbacks.py +++ b/tensorflow/python/keras/callbacks.py @@ -1172,15 +1172,15 @@ class LearningRateScheduler(Callback): ```python # Firstly, let's create a function which # maps a given epoch to a learning rate. - # This function would keep the learning rate - # constant at 0.001 for the first ten epochs and - # let it decrease exponentially after that. + # This function keeps the learning rate + # at 0.001 for the first ten epochs and + # decreases it exponentially after that. def scheduler(epoch): if epoch < 10: return 0.001 else: return 0.001 * tf.math.exp(0.1 * (10 - epoch)) - # Next, we need to set up the callback and train the model. + callback = tf.keras.callbacks.LearningRateScheduler(scheduler) model.fit(data, labels, epochs=100, callbacks=[callback], validation_data=(val_data, val_labels)) From 8deca6b4c84d225be38b81105fa0cdc47c22223b Mon Sep 17 00:00:00 2001 From: aweers <32593524+aweers@users.noreply.github.com> Date: Sun, 28 Apr 2019 18:27:55 +0200 Subject: [PATCH 3/4] Removed unneccessary sentence --- tensorflow/python/keras/callbacks.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tensorflow/python/keras/callbacks.py b/tensorflow/python/keras/callbacks.py index 6b01f314505..29535918fa0 100644 --- a/tensorflow/python/keras/callbacks.py +++ b/tensorflow/python/keras/callbacks.py @@ -1170,8 +1170,6 @@ class LearningRateScheduler(Callback): verbose: int. 0: quiet, 1: update messages. ```python - # Firstly, let's create a function which - # maps a given epoch to a learning rate. # This function keeps the learning rate # at 0.001 for the first ten epochs and # decreases it exponentially after that. From cd1481f6eefb1328ea4d7996c35657578500026c Mon Sep 17 00:00:00 2001 From: aweers <32593524+aweers@users.noreply.github.com> Date: Fri, 3 May 2019 16:38:55 +0200 Subject: [PATCH 4/4] Lines fit line limit now --- tensorflow/python/keras/callbacks.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tensorflow/python/keras/callbacks.py b/tensorflow/python/keras/callbacks.py index 29535918fa0..e1e80a6b99a 100644 --- a/tensorflow/python/keras/callbacks.py +++ b/tensorflow/python/keras/callbacks.py @@ -1170,9 +1170,8 @@ class LearningRateScheduler(Callback): verbose: int. 0: quiet, 1: update messages. ```python - # This function keeps the learning rate - # at 0.001 for the first ten epochs and - # decreases it exponentially after that. + # This function keeps the learning rate at 0.001 for the first ten epochs + # and decreases it exponentially after that. def scheduler(epoch): if epoch < 10: return 0.001