title: AttributeError: 'Tensor' object has no attribute '_numpy' when using custom metrics function
body: <p>I want to count the number of matching label in a custom error function</p>
  
  <p>I tested : </p>
  
  <pre><code>def custom__error(y_true, y_pred):
    # ---- y_pred
    yp = tf.nn.softmax( y_pred)
    bp = tf.argsort(yp,axis=-1,direction='DESCENDING',stable=False,name=None)
    cp = tf.keras.backend.eval(bp)
    # ---- y_true
    yt = tf.nn.softmax( y_true )
    bt = tf.argsort(yt,axis=-1,direction='DESCENDING',stable=False,name=None)
    ct = tf.keras.backend.eval(bt)
    # ---- common
    count =  tf.sets.intersection(cp[None,:10],ct[None,:10])
    return count
  </code></pre>
  
  <p>but I got an error:</p>
  
  <pre><code>AttributeError: 'Tensor' object has no attribute '_numpy'
  </code></pre>
  
  <p>I also tried :</p>
  
  <pre><code> def custom__error(y_true, y_pred):
    # ---- y_pred
    yp = tf.nn.softmax( y_pred)
    bp = tf.argsort(yp,axis=-1,direction='DESCENDING',stable=False,name=None)
    cp = bp.to_numpy()
    xcp = cp[None,:10]
    # ---- y_true
    yt = tf.nn.softmax( y_true )
    bt = tf.argsort(yt,axis=-1,direction='DESCENDING',stable=False,name=None)
    ct = bt.to_numpy()
    xct = ct[None,:10]
    # ---- common
    count =  tf.sets.intersection(tf.convert_to_tensor(xcp),tf.convert_to_tensor(xct))
    return count
  </code></pre>
  
  <p>I don't know how to use <strong>tf.sets.intersection</strong> inside tensorflow <strong>backend</strong> function.</p>
  
  <p>Can someone help me understand ?</p>
  
  <hr>
  
  <p>The complete error</p>
  
  <pre><code>AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-319-eed924909205&gt; in &lt;module&gt;()
        4 print(optimizer.learning_rate.numpy())  # or print(optimizer.lr.numpy())
        5 model_loss=custom__loss(100,100,1,0.01)
  ----&gt; 6 model.compile(loss=model_loss, optimizer=optimizer, metrics=['accuracy',custom__error])
  
  11 frames
  /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in &lt;listcomp&gt;(.0)
     3798     return nest.pack_sequence_as(
     3799         self._outputs_structure,
  -&gt; 3800         [x._numpy() for x in outputs],  # pylint: disable=protected-access
     3801         expand_composites=True)
     3802 
  
  AttributeError: 'Tensor' object has no attribute '_numpy'
  </code></pre>
  
  <hr>
  
  <p>I tried to return numpy array :</p>
  
  <pre><code>return count.to_numpy()
  </code></pre>
  
  <p>or with:</p>
  
  <pre><code>def custom__error(y_true, y_pred):
    # ---- y_pred
    yp = tf.nn.softmax( y_pred)
    bp = tf.argsort(yp,axis=-1,direction='DESCENDING',stable=False,name=None)
    cp = bp.to_numpy()
    xcp = cp[None,:10]
    # ---- y_true
    yt = tf.nn.softmax( y_true )
    bt = tf.argsort(yt,axis=-1,direction='DESCENDING',stable=False,name=None)
    ct = bt.to_numpy()
    xct = ct[None,:10]
    # ---- common
    count =  tf.sets.intersection(xcp,xct)
    return count
  </code></pre>
  
  <p>but got the same error:</p>
  
  <pre><code>&lt;ipython-input-114-59d27aab97e1&gt; in custom__error(y_true, y_pred)
        3   yp = tf.nn.softmax( y_pred)
        4   bp = tf.argsort(yp,axis=-1,direction='DESCENDING',stable=False,name=None)
  ----&gt; 5   cp = bp.to_numpy()
        6   xcp = cp[None,:10]
        7   # ---- y_true
  
  AttributeError: 'Tensor' object has no attribute 'to_numpy'
  </code></pre>
  
  <hr>
  
  <pre><code>&lt;ipython-input-91-16ca4acce397&gt; in custom__error(y_true, y_pred)
       12   # ---- common
       13   count =  tf.sets.intersection(xcp,xct)
  ---&gt; 14   return count.numpy()
       15 
       16 
  
  AttributeError: 'SparseTensor' object has no attribute 'numpy'
  </code></pre>