How to change the input variable of a neural network in a callback?

Viewed 42

I have an autoencoder and on some epochs I would like to a dummy matrix to my input instead of training data.

I assume that I need to do it in a callback but I have trouble writing the callback, for example imagine I would like to set the input to zero in a random time instance:

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

 def on_epoch_begin(self, epoch, logs=None):

     set_zero_input1 = np.random.choice([0, 1])
     self.model.layers[0] = set_zero_input1*self.model.layers[0]
  
   
input_zeroing = MyCustomCallback_zeroing()

and then I call it:

history= model.fit([x_train], [y_train_n], batch_size=10, epochs=300, validation_split=0.2, shuffle=True, callbacks=[input_zeroing])

I am getting this error:

callback.on_epoch_begin(epoch, logs)
 File "<input>", line 10, in on_epoch_begin
TypeError: unsupported operand type(s) for *: 'int' and 'InputLayer'

How should I pass this matrix to model.layers[0]?

Plus I also would like to pass set_zero_input1 to a loss function, does return set_zero_input1 the make sense?

0 Answers
Related