Possibilities to change the training behavor between epochs

Lets say i have a model where i want to change the training behavor in the call-method. I know how to differ it between training and validation but what if i want to have multiple training modes? I tried it with a callback but it didnt work:

import tensorflow as tf
from keras import layers
import numpy as np

class model(tf.keras.Model):

    def __init__(self):
        super(model, self).__init__()

        self.training_mode = True

        self.layer = layers.Dense(15)
        self.check_layer = layers.Dense(16, trainable=False)

    def call(self, inputs, training=None, mask=None):
        if training:
            if self.training_mode:
                tf.print("First_training_mode")
                return tf.reduce_mean(self.layer(inputs), axis=-1)
            else:
                tf.print("Second_training_mode")
                return tf.reduce_sum(self.layer(inputs), axis=-1)

        else:
            tf.print("Valmode")
            return tf.reduce_mean(self.check_layer(inputs), axis=-1)

class callback(tf.keras.callbacks.Callback):

    def __init__(self):
        super(callback, self).__init__()

    def on_epoch_end(self, epoch, logs=None):
        if epoch % 2 == 0:
            self.model.training_mode = True
        else:
            self.model.training_mode = False

input = np.random.randn(10, 5)
target = np.random.randn(10, 1)

model = model()
model.compile("adam", loss="mse")

model.fit(input, target, validation_data=(input, target), callbacks=(callback()), epochs=3)

is there an other way?

Hi @Samuel_K ,

Below is a rough code snippet for the question and you can modify according to your needs. Please check and let me know if it helps you.


class model(tf.keras.Model):
    def __init__(self):
        super(model, self).__init__()
    def train_mode_1(self, inputs, epoch_num, training=None):
        if epoch_num  % 2 !=0:
            # define the behavior for odd-numbered epochs
            # define your first training routine here
        return outputs

    def train_mode_2(self, inputs, epoch_num, training=None):
        if epoch_num % 2 == 0:
            # define the behavior for even-numbered epochs
            # define your second training routine here
        return outputs

    def call(self, inputs, mode=1, epoch_num=0, training=None):
        if mode == 1:
            outputs = self.train_mode_1(inputs, epoch_num=epoch_num, training=training)
        elif mode == 2:
            outputs = self.train_mode_2(inputs, epoch_num=epoch_num, training=training)
        else:
            raise ValueError("Invalid mode specified")
        return outputs

Thanks.

1 Like

cool idea! But how do you change the mode when training with model.fit