tensorflow: AttributeError: 'Tensor' object has no attribute 'numpy'

I use tensorflow with Google Colaboratory I have TensorFlow 2.x selected.

I wrote a small error metrics but could not succeed to transform the result to numpy array. I got AttributeError: ‘Tensor’ object has no attribute ‘numpy’

I thought I could transform my tensor to numpy array with a .numpy() call

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.numpy()
  xcp = bp[None,:10]
  # ---- y_true
  yt = tf.nn.softmax( y_true )
  bt = tf.argsort(yt,axis=-1,direction='DESCENDING',stable=False,name=None)
  #ct = bt.numpy()
  xct = bt[None,:10]
  # ---- common
  count =  tf.sets.intersection(xcp,xct)
  dcount = tf.sparse.to_dense(count)
  return dcount.numpy()
<ipython-input-46-10646c4f8b46> in custom__error(y_true, y_pred)
     15   count =  tf.sets.intersection(xcp,xct)
     16   dcount = tf.sparse.to_dense(count)
---> 17   return dcount.numpy()
     18 
     19 

AttributeError: 'Tensor' object has no attribute 'numpy'

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 20 (7 by maintainers)

Most upvoted comments

My problem is only inside a callback function (tf backend) https://colab.research.google.com/drive/1wFKDQjlPuB0ax0g-ZSU8ge1ukzBBn3TR?usp=sharing

@jvishnuvardhan Hello You were right I do have a mixture After corrections I have no more errors Thank you