0

I want to write my own keras layer with taking as input a tensor with shape (nb_batch, input_dim) and producing a tensor with shape (nb_batch, context_size, output_dim) . I have write a demo below:

class MyLayer(Layer):
def __init__(self, output_dim, context_size, init="uniform", **kwargs):
    self.output_dim = output_dim
    self.context_size = context_size
    self.init = initializations.get(init)
    super(MyLayer, self).__init__(**kwargs)

def build(self, input_shape):
    input_dim = input_shape[1]
    self.W_vec = self.init(
        (self.context_size, input_dim, self.output_dim),
        name="W_vec")

    self.trainable_weights = [self.W_vec]
    super(MyLayer, self).build()  # be sure you call this somewhere!

def call(self, x, mask=None):
    return K.dot(x, self.W_vec)
    # return K.dot(x, self.W)

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

when I ran it , got a error "TypeError: build() takes exactly 2 arguments (1 given)"enter code here

Hungry fool
  • 27
  • 1
  • 6

1 Answers1

0

Looks like build needs input shape argument

super(MyLayer, self).build(input_shape) # be sure you call this somewhere!

naveen sr
  • 43
  • 1
  • 4