Discussions>How to use number of iteration in loss functions with Tensorflow?>

How to use number of iteration in loss functions with Tensorflow?

I'm working with Tensorflow 1.13 and have created a custom loss function. I need kind of like iteration number in my loss function and can't figure out how to do it with Tensorflow. Is there any way to make it?

3 votesJW275.00
1 Answers

You will need to create a variable for storing that value. Tensorflow minimizer function has an argument called global_step, which provides the iterations number during training process (it's not necessary to be the epoch).

global_step: Optional Variable to increment by one after the variables have been updated.

So here is how you can do that

# ...
global_step = tf.Variable(0, name='global_step', trainable=False)
loss = custom_loss(global_step)
optimizer = tf.train.RMSPropOptimizer(learning_rate=1e-4)
minimizer = optimizer.minimize(loss, global_step=global_step)
# global_step will contain number of iterations during training

For more details, you can take a look at the optimizer.minimize() function. Also, there is a global function for it as well tf.train.global_step().

Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?