title: How can I make a trainable parameter in keras?
body: <p>thanks for looking my question.</p>
  
  <p>For example.</p>
  
  <p>The final output is the sum of two matrix A and B,like this:</p>
  
  <pre><code>output = keras.layers.add([A, B])
  </code></pre>
  
  <p>Now,I  want to build a new parameter x to change the output.</p>
  
  <p><strong>I want to make newoutput = A<em>x+B</em>(1-x)</strong></p>
  
  <p>and x is a <strong>trainable parameter</strong> in my network.</p>
  
  <p>what should I do?
  please help me ~ thanks very much!</p>
  
  <p><strong>edit(part of code ):</strong></p>
  
  <pre><code>conv1 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(input)
  drop1 = Dropout(0.5)(conv1)
  pool1 = MaxPooling2D(pool_size=(2, 2))(drop1)
  
  conv2 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)
  conv2 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)
  drop2 = Dropout(0.5)(conv2)
  
  up1 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop2))
  
  #the line I want to change:
  merge = add([drop2,up1])
  #this layer is simply add drop2 and up1 layer.now I want to add a trainable parameter x to adjust the weight of thoese two layers.
  </code></pre>
  
  <p>I tried to use the codes,but still occured some questions:</p>
  
  <p>1.how can I use my own layer?</p>
  
  <pre><code>merge = Mylayer()(drop2,up1)
  </code></pre>
  
  <p>or otherway?</p>
  
  <p>2.what is the meaning of out_dim?
  those parameters are all 3-dim matrix.what is the mening of out_dim?</p>
  
  <p>thank you...T.T</p>
  
  <p><strong>edit2(solved)</strong></p>
  
  <pre><code>from keras import backend as K
  from keras.engine.topology import Layer
  import numpy as np
  
  from keras.layers import add
  
  class MyLayer(Layer):
  
  def __init__(self, **kwargs):
      super(MyLayer, self).__init__(**kwargs)
  
  def build(self, input_shape):
  
      self._x = K.variable(0.5)
      self.trainable_weights = [self._x]
  
      super(MyLayer, self).build(input_shape)  # Be sure to call this at the end
  
  def call(self, x):
      A, B = x
      result = add([self._x*A ,(1-self._x)*B])
      return result
  
  def compute_output_shape(self, input_shape):
      return input_shape[0]
  </code></pre>