Search code examples
pythontensorflowkeraskeras-layertf.keras

Keras Layer Build Error: build() takes 1 Positional Argument but two were given


I have the following error in this simple layer:

class MyLayer(Layer):

def __init__(self):
    super(MyLayer, self).__init__()

def build(self):
    # Create a trainable weight variable for this layer.
    self.kernel = self.add_weight(name='kernel', 
                                  shape=(1)
                                  trainable=True)
    super(MyLayer, self).build() 

def call(self, x):
    return x/self.kernel

When I use it as:

m = MyLayer()
t = m (input)

Error: build() takes one positional argument but two were given.


Solution

  • Each layer in a Keras layer requires a input_shape argument. Add it to your build() method.