0

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.

Yutas
  • 3
  • 4

1 Answers1

3

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.

AaronDT
  • 3,940
  • 8
  • 31
  • 71
  • 1
    If I set the learning phase to be `0` meaning test. Then I call `fit`, does Keras override the 0, because if I call `fit` of course I'm trying to train the network. And what happens if it is the other way around, if I set the learning phase to `1` and then call predict? Or does the learning phase only affect the `call` methods? And is thus independent of `fit` and `predict`? – CMCDragonkai Jul 14 '20 at 05:49