我想手动实现Tensorflow中的分类交叉熵函数。我做到了:
def my_CE(y_true, y_pred):
log_y_pred = tf.math.log(y_pred)
element_wise = -tf.math.multiply_no_nan(x=log_y_pred, y=y_true)
return tf.reduce_mean(tf.reduce_sum(element_wise,axis=1))
通过测试,我意识到我做了正确的事情:
true = np.array([[0.0, 1.0], [1.0, 0.0]])
pred = np.array([[0.01, 0.99], [0.01, 0.99]])
print('Tensorflow CE : ',tf.keras.losses.CategoricalCrossentropy()(true, pred))
print('My CE : ',my_CE(true, pred))
*Tensorflow CE : tf.Tensor(2.307610273361206, shape=(), dtype=float64)
My CE : tf.Tensor(2.307610260920796, shape=(), dtype=float64)*
但在另一次测试中,我发现答案不同:
y_true = tf.constant([[0 , 0 , 0 , 0 , 1.0]])
y_pred = tf.constant([[0 , 0 , 0 ,0 , .3]])
print('Tensorflow CE : ',tf.keras.losses.CategoricalCrossentropy()(y_true, y_pred).numpy())
print('My CE : ',my_CE(y_true, y_pred).numpy())
Tensorflow CE : 1.192093e-07
My CE : 1.2039728
更糟糕的是,我意识到张量流函数在我看来不起作用! 也就是说,当我增加类是正确的概率时,不小于tensorflow损失函数的值
for i in np.arange(.1,1,0.1):
y_true = tf.constant([[0 , 0 , 0 , 0 , 1.0]])
y_pred = tf.constant([[0 , 0 , 0 ,0 , i]])
print('Tensorflow CE : ',tf.keras.losses.CategoricalCrossentropy()(y_true, y_pred).numpy())
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
Tensorflow CE : 1.192093e-07
根据评论
输出:
输出:
相关问题 更多 >
编程相关推荐