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?
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With