title: TypeError: object of type 'Tensor' has no len() when using a custom metric in Tensorflow
body: <p>I am developing a model for multi-class classification problem ( 4 classes) using Keras with Tensorflow backend. The values of <code>y_test</code> have 2D format:</p>
  
  <pre><code>0 1 0 0
  0 0 1 0
  0 0 1 0
  </code></pre>
  
  <p>This is the function that I use to calculate a balanced accuracy:</p>
  
  <pre><code>def my_metric(targ, predict):
      val_predict = predict
      val_targ = tf.math.argmax(targ, axis=1)
      return metrics.balanced_accuracy_score(val_targ, val_predict)
  </code></pre>
  
  <p>And this is the model:</p>
  
  <pre><code>hidden_neurons = 50
  timestamps = 20
  nb_features = 18
  
  model = Sequential()
  
  model.add(LSTM(
                  units=hidden_neurons,
                  return_sequences=True, 
                  input_shape=(timestamps,nb_features),
                  dropout=0.15
                  #recurrent_dropout=0.2
                )
           )
  
  model.add(TimeDistributed(Dense(units=round(timestamps/2),activation='sigmoid')))
  
  model.add(Dense(units=hidden_neurons,
                 activation='sigmoid'))
  
  
  model.add(Flatten())
  
  model.add(Dense(units=nb_classes,
                 activation='softmax'))
  
  model.compile(loss="categorical_crossentropy",
                metrics = [my_metric],
                optimizer='adadelta')
  </code></pre>
  
  <p>When I run this code, I get this error:</p>
  
  <blockquote>
    <p>--------------------------------------------------------------------------- TypeError                                 Traceback (most recent call
    last)  in ()
         30 model.compile(loss="categorical_crossentropy",
         31               metrics = [my_metric], #'accuracy',
    ---> 32               optimizer='adadelta')</p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in
    compile(self, optimizer, loss, metrics, loss_weights,
    sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
        449                 output_metrics = nested_metrics[i]
        450                 output_weighted_metrics = nested_weighted_metrics[i]
    --> 451                 handle_metrics(output_metrics)
        452                 handle_metrics(output_weighted_metrics, weights=weights)
        453 </p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in
    handle_metrics(metrics, weights)
        418                     metric_result = weighted_metric_fn(y_true, y_pred,
        419                                                        weights=weights,
    --> 420                                                        mask=masks[i])
        421 
        422                 # Append to self.metrics_names, self.metric_tensors,</p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py
    in weighted(y_true, y_pred, weights, mask)
        402         """
        403         # score_array has ndim >= 2
    --> 404         score_array = fn(y_true, y_pred)
        405         if mask is not None:
        406             # Cast the mask to floatX to avoid float64 upcasting in Theano</p>
    
    <p> in my_metric(targ, predict)
         22     val_predict = predict
         23     val_targ = tf.math.argmax(targ, axis=1)
    ---> 24     return metrics.balanced_accuracy_score(val_targ, val_predict)
         25     #return 5
         26 </p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py
    in balanced_accuracy_score(y_true, y_pred, sample_weight, adjusted)<br>
    1431     1432     """
    -> 1433     C = confusion_matrix(y_true, y_pred, sample_weight=sample_weight)    1434     with
    np.errstate(divide='ignore', invalid='ignore'):    1435<br>
    per_class = np.diag(C) / C.sum(axis=1)</p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py
    in confusion_matrix(y_true, y_pred, labels, sample_weight)
        251 
        252     """
    --> 253     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
        254     if y_type not in ("binary", "multiclass"):
        255         raise ValueError("%s is not supported" % y_type)</p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/sklearn/metrics/classification.py
    in _check_targets(y_true, y_pred)
         69     y_pred : array or indicator matrix
         70     """
    ---> 71     check_consistent_length(y_true, y_pred)
         72     type_true = type_of_target(y_true)
         73     type_pred = type_of_target(y_pred)</p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in
    check_consistent_length(*arrays)
        229     """
        230 
    --> 231     lengths = [_num_samples(X) for X in arrays if X is not None]
        232     uniques = np.unique(lengths)
        233     if len(uniques) > 1:</p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in
    (.0)
        229     """
        230 
    --> 231     lengths = [_num_samples(X) for X in arrays if X is not None]
        232     uniques = np.unique(lengths)
        233     if len(uniques) > 1:</p>
    
    <p>~/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in
    _num_samples(x)
        146             return x.shape[0]
        147         else:
    --> 148             return len(x)
        149     else:
        150         return len(x)</p>
    
    <p>TypeError: object of type 'Tensor' has no len()</p>
  </blockquote>