TLDR
I need random effects at each call of my custom layer. If I create a random mask (and also create a new tensor by shuffling the input data) in the call method instead of the init or build methods, will this create new nodes on the computation graph with every function call?
I am creating a custom CutMix-like layer to use on tabular data. This layer will 1) take a minibatch, 2) create a shuffled version of the original minibatch, 3) replace the values of the original for the shuffled at bernouilli(p). This is often referred to as SwapNoise on Kaggle.
This layer relies on a random mask (drawn bernoulli(p)) to switch out the original values for the shuffled values. In the official custom layer guide, I see new layers inside a custom layer in either the init or the build methods of the class. As my layer needs a unique random mask at each minibatch, I have placed the mask generation in the call method of the class. The code is below:
class CutMix(tf.keras.layers.Layer):
def __init__(self, noise):
super(CutMix, self).__init__()
self.noise = noise
def call(self, inputs, training=None):
if training:
shuffled = tf.stop_gradient(tf.random.shuffle(inputs))
msk = tf.keras.backend.random_bernoulli(inputs.shape, p=1 - self.noise, dtype=tf.float32)
print(msk)
return msk * inputs + (tf.ones_like(msk) - msk) * shuffled
return inputs
Does creating this mask layer (and the shuffled layer for that matter) in the class method create a new mask layer each call, thereby exploding the computational graph's size? If this is the case, how can I incorporate randomness inside a layer (such as shuffling the minibatch or creating a mask) without this bug?