我想计算自定义错误函数中匹配标签的数量
我测试了:
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
但是我收到一个错误:
AttributeError: 'Tensor' object has no attribute '_numpy'
我也试过:
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
我不知道如何在tensorflow后端函数中使用tf.sets.intersection。
有人可以帮助我理解吗?
完整的错误
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'
我试图返回数字数组:
return count.to_numpy()
或与:
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
但得到同样的错误:
<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'
在这一行中:
model.compile(loss=model_loss, optimizer=optimizer, metrics=['accuracy',custom__error])
custom__error
函数应该返回一个 numpy 数组,而在这种情况下tf.insersection
会给你Tensor
的数组,因为传递给tf.intersection
的y_true
和y_pred
本身就是tensor
的。
因此,您首先需要将count
转换为 numpy 数组,然后再返回它。
这个答案可能会有所帮助。
编辑:count
是SparseTensor
的,因此首先用以下内容将其转换为密集张量:
dcount = tf.sparse.to_dense(count)
然后,您需要检查张量流的运行模式。 你可以使用它:
tf.executing_eagerly()
- 如果返回
True
:
Eager 模式处于活动状态。您需要在要转换的张量上调用.numpy()
。 - 如果返回
False
:
图形模式处于活动状态。您需要在要转换的张量上调用.eval()
。
如果您愿意,可以为您自动化:
if tf.executing_eagerly():
return dcount.numpy()
else
return dcount.eval()
信用:TF 2.0 "张量"对象在使用 .numpy(( 时没有属性"numpy",尽管默认情况下启用紧急执行
如果您使用的是 Tensorflow 1.x,您可能需要手动启用预先执行。查看此答案以了解详细信息。