【发布时间】:2021-06-03 13:35:15
【问题描述】:
不确定我是否在这里做错了什么,但无论出于何种原因,当我按照 tf 2.4.1 here987654321@ 的示例进行操作时
我没有得到相同的结果,实际上,我得到了示例中结果的否定。
这就是我正在做的事情
import tensorflow as tf
from tensorflow.keras.losses import cosine_similarity
y_true = [[0., 1.], [1., 1.], [1., 1.]]
y_pred = [[1., 0.], [1., 1.], [-1., -1.]]
loss = cosine_similarity(y_true, y_pred, axis=1)
loss_numpy = loss.eval(session=tf.Session())
print(loss_numpy) # array([ 0. , 0.99999994, -0.99999994], dtype=float32)
# expected output array([-0. , -0.99999994, 0.99999994], dtype=float32)
我假设在 tf 2+ 中不是这种情况,但这是我必须处理的已知问题,还是发生了其他事情?
附言
我已经通过定义解决了这个问题
cosine_proximity_loss = lambda y_true, y_pred: -1. * cosine_similarity(y_true, y_pred)
并在我的模型中使用它。如果有人知道我不应该这样做,任何建议将不胜感激!
【问题讨论】:
标签: python python-3.x tensorflow machine-learning keras