I was wondering if anyone knew how the build() function works from the tf.keras.layers.Layer class under the hood. According to the documentation:
build is called when you know the shapes of the input tensors and can do the rest of the initialization
so to me it seems like the class is behaving similar to this:
class MyDenseLayer:
def __init__(self, num_outputs):
self.num_outputs = num_outputs
def build(self, input_shape):
self.kernel = self.add_weight("kernel",
shape=[int(input_shape[-1]), self.num_outputs])
def __call__(self, input):
self.build(input.shape) ## build is called here when input shape is known
return tf.matmul(input, self.kernel)
I can't imagine build() would be called for ever __call__, but it is the only place where the input is passed in. Does anyone know how exactly this works under the hood?