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?