title: Keras: how to get tensor dimensions inside custom loss?
body: <p>I'm trying to write my custom loss function: I want to apply <code>categorical_crossentropy</code> to the parts of input vector and then sum.</p>
  
  <p>Assume y_true, y_pred are 1D vectors.</p>
  
  <p>Code:</p>
  
  <pre><code>def custom_loss(y_true, y_pred):
  
      loss_sum= 0.0
      for i in range(0,y_true.shape[0],dictionary_dims):
          loss_sum+= keras.backend.categorical_crossentropy(y_true[i*dictionary_dims:(i+1)*dictionary_dims], y_pred[i*dictionary_dims:(i+1)*dictionary_dims])
  
      return loss_sum
  </code></pre>
  
  <p>But I get an error:</p>
  
  <pre><code>    for i in range(0,y_true.shape[0],dictionary_dims):
  TypeError: __index__ returned non-int (type NoneType)
  </code></pre>
  
  <p>So how to access shape of input tensors to get subset of tensor?</p>
  
  <p><strong>Update:</strong>
  Also tried to write loss via tensorflow directly:</p>
  
  <pre><code>def custom_loss_tf(y_true, y_pred):
  
      print('tf.shape(y_true)',tf.shape(y_true)) #
      print('type(tf.shape(y_true))',type(tf.shape(y_true))) #
  
      sys.exit()
  
      loss_sum= 0.0
      for i in range(0,y_true.shape[0],dictionary_dims):
          loss_sum+= keras.backend.categorical_crossentropy(y_true[i*dictionary_dims:(i+1)*dictionary_dims], y_pred[i*dictionary_dims:(i+1)*dictionary_dims])
  
      return loss_sum
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>tf.shape(y_true) Tensor("Shape:0", shape=(2,), dtype=int32)
  type(tf.shape(y_true)) &lt;class 'tensorflow.python.framework.ops.Tensor'&gt;
  </code></pre>
  
  <p>Not sure what is <code>shape=(2,)</code> mean, but this is not what I'm expecting, because <code>model.summary()</code> shows that last layer is <code>(None, 26)</code>:</p>
  
  <pre><code>_________________________________________________________________
  Layer (type)                 Output Shape              Param #
  =================================================================
  input_1 (InputLayer)         (None, 80, 120, 3)        0
  _________________________________________________________________
  conv2d_1 (Conv2D)            (None, 80, 120, 32)       896
  _________________________________________________________________
  max_pooling2d_1 (MaxPooling2 (None, 40, 60, 32)        0
  _________________________________________________________________
  activation_1 (Activation)    (None, 40, 60, 32)        0
  _________________________________________________________________
  conv2d_2 (Conv2D)            (None, 40, 60, 32)        9248
  _________________________________________________________________
  max_pooling2d_2 (MaxPooling2 (None, 20, 30, 32)        0
  _________________________________________________________________
  activation_2 (Activation)    (None, 20, 30, 32)        0
  _________________________________________________________________
  conv2d_3 (Conv2D)            (None, 20, 30, 64)        18496
  _________________________________________________________________
  max_pooling2d_3 (MaxPooling2 (None, 10, 15, 64)        0
  _________________________________________________________________
  activation_3 (Activation)    (None, 10, 15, 64)        0
  _________________________________________________________________
  conv2d_4 (Conv2D)            (None, 10, 15, 64)        36928
  _________________________________________________________________
  max_pooling2d_4 (MaxPooling2 (None, 5, 7, 64)          0
  _________________________________________________________________
  activation_4 (Activation)    (None, 5, 7, 64)          0
  _________________________________________________________________
  flatten_1 (Flatten)          (None, 2240)              0
  _________________________________________________________________
  head (Dense)                 (None, 26)                58266
  =================================================================
  </code></pre>