title: What does axis=[1,2,3] mean in K.sum in keras backend?
body: <p>I'm trying to implement a custom loss function for my CNN model. I found an <a href="https://github.com/rekon/Smoke-semantic-segmentation/blob/linknet-implementation/LinkNet.ipynb" rel="noreferrer">IPython notebook</a> that has implemented a custom loss function named Dice, just as follows:</p>
  
  <pre><code>from keras import backend as K
  smooth = 1.
  
  def dice_coef(y_true, y_pred, smooth=1):
      intersection = K.sum(y_true * y_pred, axis=[1,2,3])
      union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3])
      return K.mean( (2. * intersection + smooth) / (union + smooth), axis=0)
  
  def bce_dice(y_true, y_pred):
      return binary_crossentropy(y_true, y_pred)-K.log(dice_coef(y_true, y_pred))
  
  def true_positive_rate(y_true, y_pred):
      return K.sum(K.flatten(y_true)*K.flatten(K.round(y_pred)))/K.sum(y_true)
  
  seg_model.compile(optimizer = 'adam', 
                loss = bce_dice, 
                metrics = ['binary_accuracy', dice_coef, true_positive_rate])
  </code></pre>
  
  <p>I have never used keras backend before and really get confused with the matrix calculations of keras backend. So, I created some tensors to see what's happening in the code:</p>
  
  <pre><code>val1 = np.arange(24).reshape((4, 6))
  y_true = K.variable(value=val1)
  
  val2 = np.arange(10,34).reshape((4, 6))
  y_pred = K.variable(value=val2)
  </code></pre>
  
  <p>Now I run the <code>dice_coef</code> function:</p>
  
  <pre><code>result = K.eval(dice_coef(y_true=y_true, y_pred=y_pred))
  print('result is:', result)
  </code></pre>
  
  <p>But it gives me this error:</p>
  
  <pre><code>ValueError: Invalid reduction dimension 2 for input with 2 dimensions. for 'Sum_32' (op: 'Sum') with input shapes: [4,6], [3] and with computed input tensors: input[1] = &lt;1 2 3&gt;.
  </code></pre>
  
  <p>Then I changed all of <code>[1,2,3]</code> to <code>-1</code> just like below:</p>
  
  <pre><code>def dice_coef(y_true, y_pred, smooth=1):
      intersection = K.sum(y_true * y_pred, axis=-1)
      # intersection = K.sum(y_true * y_pred, axis=[1,2,3])
      # union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3])
      union = K.sum(y_true, axis=-1) + K.sum(y_pred, axis=-1)
      return K.mean( (2. * intersection + smooth) / (union + smooth), axis=0)
  </code></pre>
  
  <p>Now it gives me a value. </p>
  
  <pre><code>result is: 14.7911625
  </code></pre>
  
  <p><em>Questions:</em></p>
  
  <ol>
  <li>What is <code>[1,2,3]</code>? </li>
  <li>Why the code works when I change <code>[1,2,3]</code> to <code>-1</code>?</li>
  <li>What does this <code>dice_coef</code> function do?</li>
  </ol>