Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the call method gets called at build time in Keras layers

So I'm trying to implement my own layer in Keras, using the provided example:

class MyLayer(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel', 
                                      shape=(input_shape[1], self.output_dim),
                                      initializer='uniform',
                                      trainable=True)
        super(MyLayer, self).build(input_shape)  # Be sure to call this somewhere!

    def call(self, x):
        return K.dot(x, self.kernel)

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim)

I noticed that what's in call gets called at build time when I do:

model.add(MyLayer(4, input_dim=(1))

This happens without calling fit, train, predict, etc...

Why?

like image 644
gotch4 Avatar asked Jun 29 '17 22:06

gotch4


People also ask

What is call in Keras?

The call function gets run twice at the beginning of training through the first epoch and then gets run almost at the end of the first epoch. It is never run after that.

What is model subclassing in Keras?

In Model Sub-Classing there are two most important functions __init__ and call. Basically, we will define all the trainable tf. keras layers or custom implemented layers inside the __init__ method and call those layers based on our network design inside the call method which is used to perform a forward propagation.

What is Keras sequential ()?

Sequential. The core idea of Sequential API is simply arranging the Keras layers in a sequential order and so, it is called Sequential API. Most of the ANN also has layers in sequential order and the data flows from one layer to another layer in the given order until the data finally reaches the output layer.

What is build function in Tensorflow?

build() method is typically used to instantiate the weights of the layer. See the source code for tf. keras. layers. Dense for an example, and note that the weight and bias tensors are created in that function.


1 Answers

It will be called at the time you added layer to model to check if the shape is valid or not.

If you want to see where it is called, change your code:

import sys
import traceback

class MyLayer(Layer):
    ....
    def call(self, x):
         traceback.print_stack(file=sys.stdout)
         return K.dot(x, self.kernel)
like image 193
An Phú Avatar answered Oct 07 '22 14:10

An Phú