Keras - add_weight() method not adding to total model parameters

I am creating a custom Keras layer FConv2D(), and adding a weight in its build() function using the add_weight() method as suggested in official Keras tutorial for creating custom layers.

def build(self, input_shape):
      shape = tf.TensorShape(input_shape).as_list()
      h = shape[1]
      w = shape[2]
      in_channels = shape[3]
      self.kernel = self.add_weight(
            shape=(h,w,in_channels,self.num_outputs),
            initializer=random_normal,
            trainable=True,
        )
      super(FConv2D, self).build(input_shape)

But when I print the summary of a single-layer model containing just this layer, the number of parameters in this layer is shown to be 0.

Model: model_4
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         [(None, 7, 7, 2)]         0         
_________________________________________________________________
f_conv2d_4 (FConv2D)         (None, 7, 7, 64)          0         
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0

I tried the same method in the tutorial for custom layers on the official website, but their add_weight method seems to work right -

class SimpleDense(tf.keras.layers.Layer):

  def __init__(self, units=32):
      super(SimpleDense, self).__init__()
      self.units = units

  def build(self, input_shape):  # Create the state of the layer (weights)
    self.w = self.add_weight(shape=(input_shape[-1], self.units),
                               initializer='random_normal',
                               trainable=True)
    self.b = self.add_weight(shape=(self.units,),
                               initializer='random_normal',
                               trainable=True)

  def call(self, inputs):  # Defines the computation from inputs to outputs
      return tf.matmul(inputs, self.w) + self.b

input = tf.keras.layers.Input(shape = (1000,1))
output = SimpleDense(100)(input)
model = tf.keras.Model(inputs = [input], outputs = [output])
model.summary()

Model: model_1
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 1000, 1)]         0         
_________________________________________________________________
simple_dense_3 (SimpleDense) (None, 1000, 100)         200       
=================================================================
Total params: 200
Trainable params: 200
Non-trainable params: 0

Could anyone please tell why the weights added in the custom layer are not showing in the model parameters?

Topic keras tensorflow deep-learning

Category Data Science


I bumped into the same problem, the cause of which may be that you used def __call__ in your customized layer. Like the official sample did, try using def call instead.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.