1 回答
TA贡献1803条经验 获得超3个赞
使用tf- 兼容的操作,通过tf和keras.backend:
import tensorflow as tf
import keras.backend as K
from keras.losses import binary_crossentropy
def custom_loss(y_true, y_pred):
indices = K.squeeze(tf.where(K.sum(y_true, axis=1) > 0))
y_true_sparse = K.cast(K.gather(y_true, indices), dtype='float32')
y_pred_sparse = K.cast(K.gather(y_pred, indices), dtype='float32')
return binary_crossentropy(y_true_sparse, y_pred_sparse) # returns a tensor
我不确定您的问题的确切维度规格,但损失必须评估为单个值 - 上面没有,因为您正在传递多维预测和标签。要减少暗淡,请用 eg 包裹上面的 return K.mean。例子:
y_true = np.random.randint(0,2,(10,2))
y_pred = np.abs(np.random.randn(10,2))
y_pred /= np.max(y_pred) # scale between 0 and 1
print(K.get_value(custom_loss(y_true, y_pred))) # get_value evaluates returned tensor
print(K.get_value(K.mean(custom_loss(y_true, y_pred))
>> [1.1489482 1.2705883 0.76229745 5.101402 3.1309896] # sparse; 5 / 10 results
>> 2.28284 # single value, as required
(最后,请注意,这种稀疏性会通过从总标签/预测计数中排除全零列来偏置损失;如果不希望,您可以通过K.sumand K.shapeor进行平均K.size)
添加回答
举报