Yutas
Yutas

Reputation: 3

How can I tell Keras the learning phase when I use train_on_batch to train a model?

I have dropout layers in my model so I want keras to figure out the training and test phases to run or ignore the dropout layers, and I found that K.set_learning_phase can do me this favor but how can I add it to training and test processes? My code is like this:

def discriminator(self):
    x_A = Input(shape=self.shape)
    x_B = Input(shape=self.shape)
    x = concatenate([x_A, x_B], axis=-1)
    self.model = Sequential()
    self.model.add(Dropout(0.5, input_shape=self.shape_double))
    self.model.add(LSTM(200, return_sequences=True, kernel_constraint=unit_norm()))
    self.model.add(Dropout(0.5))
    self.model.add(LSTM(200, return_sequences=True, kernel_constraint=unit_norm()))
    self.model.add(Dropout(0.5))
    self.model.add(Flatten())
    self.model.add(Dense(8, activation="softmax", kernel_constraint=unit_norm())

    label=self.model(x)

    return Model([x_A,x_B], label)
...
def train(self, epoch, batch_size):
    for epoch in range(epochs):
        for batch,train_A,train_B,train_label in enumerate(Load_train(batch_size)):
            Dloss = self.discriminator.train_on_batch([train_A,train_B],train_label)
            ...
def test(self,test_A,test_B,test_label):
    predicted_label_dist = self.discriminator.predict([test_A,test_B])
    ...

Any suggestions will be appreciated. Thanks.

Upvotes: 0

Views: 2460

Answers (1)

AaronDT
AaronDT

Reputation: 4060

Keras does figure out the appropriate learning phase on its own by default when you call fit or predict. Hence, your dropout will only be applied during training but not during testing. However, if you still wish to configure training phase on your own i.e. overwrite the default behaviour you can do it like this (from the keras docs):

keras.backend.set_learning_phase(value) 

Where:

value: Learning phase value, either 0 or 1 (integers).

simply add this code in your training and testing function.

Upvotes: 3

Related Questions