I want to count the number of matching label in a custom error function
I tested :
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
but I got an error:
AttributeError: 'Tensor' object has no attribute '_numpy'
I also tried :
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
I don't know how to use tf.sets.intersection inside tensorflow backend function.
Can someone help me understand ?
The complete error
AttributeError Traceback (most recent call last)
<ipython-input-319-eed924909205> in <module>()
4 print(optimizer.learning_rate.numpy()) # or print(optimizer.lr.numpy())
5 model_loss=custom__loss(100,100,1,0.01)
----> 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 <listcomp>(.0)
3798 return nest.pack_sequence_as(
3799 self._outputs_structure,
-> 3800 [x._numpy() for x in outputs], # pylint: disable=protected-access
3801 expand_composites=True)
3802
AttributeError: 'Tensor' object has no attribute '_numpy'
I tried to return numpy array :
return count.to_numpy()
or with:
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
but got the same error:
<ipython-input-114-59d27aab97e1> 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)
----> 5 cp = bp.to_numpy()
6 xcp = cp[None,:10]
7 # ---- y_true
AttributeError: 'Tensor' object has no attribute 'to_numpy'
<ipython-input-91-16ca4acce397> in custom__error(y_true, y_pred)
12 # ---- common
13 count = tf.sets.intersection(xcp,xct)
---> 14 return count.numpy()
15
16
AttributeError: 'SparseTensor' object has no attribute 'numpy'