Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get reduced learning rate of SGD optimizer in TensorFlow 2.0?

I want to reduce learning rate in SGD optimizer of tensorflow2.0, I used this line of code: tf.keras.optimizers.SGD(learning_rate, decay=lr_decay, momentum=0.9) But I don't know if my learning rate has dropped, how can I get my current learning rate?

like image 545
saleizl Avatar asked Nov 07 '19 00:11

saleizl


People also ask

How do I find the best learning rate in Tensorflow?

How to determine a good learning rate. You can identify a learning rate by looking at the TensorBoard graph of loss against training step. You want find the section where loss is decreasing fastest, and use the learning rate that was being used at that training step.

What is the default learning rate in Tensorflow?

The learning rate. Defaults to 0.01. float hyperparameter >= 0 that accelerates gradient descent in the relevant direction and dampens oscillations.

How many optimizer classes are there in TensorFlow?

Tensorflow predominantly supports 9 optimizer classes including its base class (Optimizer). The stochastic Gradient Descent (SGD) optimization method executes a parameter update for every training example.

What is learning rate in TensorFlow?

What Is Learning Rate In Tensorflow? hyperparameter that controls the change in model weights as the model weights are updated based on an estimated error. When configuring your neural network, it may be more important to consider learning rate. What Will Happen If The Learning Rate Is Set Too Low Or Too High?

What is AdaGrad optimizer in machine learning?

AdaGrad stands for Adaptive Gradient Algorithm. AdaGrad optimizer modifies the learning rate particularly with individual features .i.e. some weights in the dataset may have separate learning rates than others.

What is the default learning rate for TF keras optimizers?

Syntax: tf. keras. optimizers. Adam (leaarning_rate=0. 001, beta_1=0. 9, beta_2=0. 999, epsilon=1e-07, amsgrad=False, name='Adam', **kwargs) Parameters: learning_rate: rate at which algorithm updates the parameter. Tensor or float type of value. Default value is 0. 001 beta_1: Exponential decay rate for 1st moment.


Video Answer


1 Answers

print(model.optimizer._decayed_lr('float32').numpy())

will do. _decayed_lr() computes decayed learning rate as a function of iterations and decay. Full example below.


from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
import numpy as np

ipt = Input((12,))
out = Dense(12)(ipt)
model = Model(ipt, out)
model.compile(SGD(1e-4, decay=1e-2), loss='mse')

x = y = np.random.randn(32, 12)  # dummy data
for iteration in range(10):
    model.train_on_batch(x, y)
    print("lr at iteration {}: {}".format(
            iteration + 1, model.optimizer._decayed_lr('float32').numpy()))
# OUTPUTS
lr at iteration 1: 9.900989971356466e-05
lr at iteration 2: 9.803921420825645e-05
lr at iteration 3: 9.708738070912659e-05
lr at iteration 4: 9.61538462433964e-05
lr at iteration 5: 9.523809421807528e-05
lr at iteration 6: 9.433962259208784e-05
lr at iteration 7: 9.345793660031632e-05
lr at iteration 8: 9.259258513338864e-05
lr at iteration 9: 9.174311708193272e-05
lr at iteration 10: 9.09090886125341e-05
like image 72
OverLordGoldDragon Avatar answered Oct 18 '22 10:10

OverLordGoldDragon