I am trying to build my own custom keras layer following the documentation at


In the custom layers we need three functions call, build and init I am not able to understand where the function build gets called and what is its use.

I checked the source of Dense layer (https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/keras/layers/core.py#L1159-L1190) in keras which is also built on top of the Layer class of tensorflow.keras and the same thing is observed there as well. I cant find any call to the function build.

Can someone please explain me what build function is for ? I mean I understand that add_weights is called inside build and it is important but where is build being called


build is called by the __call__ function which is implemented in the parent Layer class.

From the TF docs: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer

"build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), using add_weight(). __call__() will automatically build the layer (if it has not been built yet) by calling build()."


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.