I was looking at the tensorflow tutorial about making a custom layer and found a code which is something like:
# A layer that creates an activity regularization loss
class ActivityRegularizationLayer(keras.layers.Layer):
def __init__(self, rate=1e-2):
super(ActivityRegularizationLayer, self).__init__()
self.rate = rate
def call(self, inputs):
self.add_loss(self.rate * tf.reduce_sum(inputs))
return inputs
and then they have used the nested Layers as
class OuterLayer(keras.layers.Layer):
def __init__(self):
super(OuterLayer, self).__init__()
self.activity_reg = ActivityRegularizationLayer(1e-2)
def call(self, inputs):
return self.activity_reg(inputs)
I know the outer layer will address the weights, biases and losses of the inner layer too but what I fail to understand is that why is it not a custom loss function but a layer? Is this supposed to be collecting the loss for the specific layer only? If yes, I don't see a way out about what is it doing and how is it?