Set Training to False for Validation Data Used in Keras Fit Generator Function

Viewed 1570

How do I set training to False for the validation_data used in Keras fit_generator? I have Dropout layers in my model and I want training to be True during training, and False during validation and testing.

2 Answers

Keras automatically sets learning_phase to False when doing validation. There is nothing extra that you need to do.

Dropout nodes automatically check whether they are in training mode.

https://github.com/keras-team/keras/blob/master/keras/layers/core.py#L126

If you want to verify that Keras automatically changes the learning mode flag you can execute the code bellow. It adds a Lambda layer that adds a print tensor to the graph that outputs a different message in each case.

from tensorflow import keras
from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K

def inspect(x):
  xp = K.in_train_phase(K.print_tensor(x, message='train x:'),
                        K.print_tensor(x, message='test x:'))
  return xp

def make_model():
  inp = Input(shape=(4,))
  h1 = Dense(2)(inp)
  h1p = Lambda(inspect)(h1)
  out = Dense(1)(h1p)
  model = Model(inp, out)
  model.compile('adam', 'mse')
  return model

model = make_model()
model.summary()


import numpy as np

X_train = np.random.rand(1, 4)
Y_train = np.random.rand(1, 1)
X_test = np.random.rand(1, 4)
Y_test = np.random.rand(1, 1)

model.fit(X_train, Y_train, validation_data=(X_test, Y_test))

I added an is_training argument to my data generator function. If it's True, I set the Keras learning phase to 1, else to 0 (see docs at keras.io/backend/):

if is_training:
    K.set_learning_phase( 1 )
else:
    K.set_learning_phase( 0 )

So, for my training data generator, I use is_training = True, and for my validation data generator, I use is_training = False.

Related