Search code examples
keraskeras-2

What does self.iterations refer to in Keras' Optimizer class


I've been trying to understand Keras' Optimizer class, and realize that there's a variable I don't quite understand - self.iterations. Does this refer to:

  1. The number of individual samples for which updates have been performed?
  2. The number of individual batches for which updates have been performed? (This is what I believe)
  3. The number of total epochs (i.e. complete passes thru a training set) for which updates have been performed?

Solution

  • It's 2.

    The entire keras function is iterated once per batch.

    One way to test is to get a small array of data and train for one epoch:

    #get 3 batches of size 32 from the data
    small_X = X_train[:3*32]
    small_Y = Y_train[:3*32]
    
    #print the initial value of iterations
    print(keras.backend.eval(model.optimizer.iterations))
    
    #train for 1 epoch with batch size 32
    model.fit(small_X, small_Y, epochs=1, batch_size=32, verbose=0)
    
    #see the new value of iterations
    print(keras.backend.eval(model.optimizer.iterations))