title: Keras custom loss function using outputs of each neuron
body: <p>I am trying to custom a loss function using the outputs of each neuron of the last layer. And the function may not be linear. Here is what I am working on:</p>
  <pre><code>## some previous layers##
  ## my last dense layer##
  dense1 = Dense(4, activation=&quot;relu&quot;, name=&quot;dense_layer1&quot;)(previous layer)
  
  
  dense11 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;dense11&quot;)(dense1)
  dense12 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;dense12&quot;)(dense1)
  dense13 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;dense13&quot;)(dense1)
  dense14 = Dense(1, activation = &quot;sigmoid&quot;, name = &quot;dense14&quot;)(dense1)
  
  
  ## custom loss function ##
  
  def custom_layer(tensor):
      return tensor[1]*2+tensor[2]+tensor[3]/(tensor[4]*2)  #some nonlinear function like this
  
  
  lambda_layer = Lambda(custom_layer, name=&quot;lambda_layer&quot;)([dense11,dense12,dense13,dense14])
  
  model = Model(inputs=Input, outputs=lambda_layer)  # &quot;Input&quot; are in previous layers, not shown here
  
  model.compile(loss='mse', optimizer='adam')
  model.fit(X_train, Y_train, epochs=2, batch_size=512, verbose=1)
  
  
  </code></pre>
  <p>My Y_train is n*1 (n is the sample size).</p>
  <p>So I am basically applying a nonlinear transformation of those final four neurons' output, which is equivalent as to construct a new loss function. After the transformation, the y hat should also be a n*1 vector.</p>
  <p>But the code keeps not working. I think it is due to the lambda_layer or the custom_layer function. I also tried to define a new loss function (then there would be no &quot;lambda_layer&quot;), but it didn't work either. I have no idea what's wrong with it. (headache!)</p>
  <p>Any ideas or suggestions are appreciated!! Thanks a lot! (I'm using Python3.7 with Tensorflow version 2.0.0)</p>
  <hr />
  <p>Solved, thanks!</p>