title: Keras custom layer with trainable weights
body: <p>I need to create custom layer in Keras (1.1) that has trainable weights (the same shape as input). And I try to init the weights by random values. 
  There is my 'mylayer.py' file:</p>
  
  <pre><code>from keras import backend as K
  from keras.engine.topology import Layer
  import numpy as np
  from numpy import random
  
  class MyLayer(Layer):
  
  def __init__(self,**kwargs):
      super(MyLayer, self).__init__(**kwargs)
  
  def build(self, input_shape):
      # Create a trainable weight variable for this layer.
      self.W_init = np.random(input_shape)
      self.W = K.variable(self.W_init, name="W")
      self.trainable_weights = [self.W]
      super(MyLayer, self).build(input_shape)  # Be sure to call this somewhere!
  
  def call(self, x):
      num, n, m = x.shape
      res=np.empty(num,1,1)
      for i in range(num):
          res[i,0,0]=K.dot(x[i,:,:], self.W[i,:,:])
      return res
  
  def compute_output_shape(self, input_shape):
      return (input_shape[0], 1,1)
  </code></pre>
  
  <p>But when I try to use it:</p>
  
  <pre><code>    from mylayer import *
  def get_my_model_2():
      model = Sequential([
          Lambda(norm_input, input_shape=(1,28,28)),
          Convolution2D(32,3,3, activation='relu'),
          BatchNormalization(axis=1),
          Convolution2D(32,3,3, activation='relu'),
          MaxPooling2D(),
          BatchNormalization(axis=1),
          Convolution2D(64,3,3, activation='relu'),
          BatchNormalization(axis=1),
          Convolution2D(64,3,3, activation='relu'),
          MaxPooling2D(),
          MyLayer(input_shape=(64,4,4)), # MY LAYER
          Flatten(),
          BatchNormalization(),
          Dense(512, activation='relu'),
          BatchNormalization(),
          Dropout(0.5),
          Dense(10, activation='softmax')
          ])
      model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
      return model
  
  model=get_my_model_2()
  </code></pre>
  
  <p>I have the error "'module' object is not callable":</p>
  
  <pre><code>&gt; 
  &gt; /home/universal/anaconda3/envs/practicecourse2/mylayer.py in
  &gt; build(self, input_shape)
  &gt;      15                    #                   initializer='uniform',
  &gt;      16                    #                   trainable=True)
  &gt; ---&gt; 17         self.W_init = np.random(input_shape)
  &gt;      18         self.W = K.variable(self.W_init, name="W")
  &gt;      19         self.trainable_weights = [self.W]
  &gt; 
  &gt; TypeError: 'module' object is not callable
  </code></pre>
  
  <p>What's wrong?</p>
  
  <p>Thank you in advance</p>
  
  <p>Added:</p>
  
  <p>After fixing the "random" problem I have  another error:</p>
  
  <pre><code>---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-31-8e5ded840273&gt; in &lt;module&gt;()
  ----&gt; 1 model=get_my_model_2()
        2 model.summary()
  
  &lt;ipython-input-30-09ee1207017c&gt; in get_my_model_2()
       20         BatchNormalization(),
       21         Dropout(0.5),
  ---&gt; 22         Dense(10, activation='softmax')
       23         ])
       24     model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
  
  /home/universal/anaconda3/envs/practicecourse2/lib/python2.7/site-packages/keras/models.pyc in __init__(self, layers, name)
      253 
      254         for layer in layers:
  --&gt; 255             self.add(layer)
      256 
      257     def add(self, layer):
  
  /home/universal/anaconda3/envs/practicecourse2/lib/python2.7/site-packages/keras/models.pyc in add(self, layer)
      310                  output_shapes=[self.outputs[0]._keras_shape])
      311         else:
  --&gt; 312             output_tensor = layer(self.outputs[0])
      313             if type(output_tensor) is list:
      314                 raise Exception('All layers in a Sequential model '
  
  /home/universal/anaconda3/envs/practicecourse2/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, x, mask)
      512         if inbound_layers:
      513             # this will call layer.build() if necessary
  --&gt; 514             self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
      515             input_added = True
      516 
  
  /home/universal/anaconda3/envs/practicecourse2/lib/python2.7/site-packages/keras/engine/topology.pyc in add_inbound_node(self, inbound_layers, node_indices, tensor_indices)
      570         # creating the node automatically updates self.inbound_nodes
      571         # as well as outbound_nodes on inbound layers.
  --&gt; 572         Node.create_node(self, inbound_layers, node_indices, tensor_indices)
      573 
      574     def get_output_shape_for(self, input_shape):
  
  /home/universal/anaconda3/envs/practicecourse2/lib/python2.7/site-packages/keras/engine/topology.pyc in create_node(cls, outbound_layer, inbound_layers, node_indices, tensor_indices)
      147 
      148         if len(input_tensors) == 1:
  --&gt; 149             output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
      150             output_masks = to_list(outbound_layer.compute_mask(input_tensors[0], input_masks[0]))
      151             # TODO: try to auto-infer shape if exception is raised by get_output_shape_for
  
  TypeError: call() got an unexpected keyword argument 'mask'
  </code></pre>
  
  <p>It seems to me that there is still sth wrong with my custom layer</p>