SparseCategoricalCrossentropy的from_logits参数探究

 

先看官方解释:

也就是说,

from_logits = False 表示输入进来的y_pred已符合某种分布, 系统只会帮你把概率归一化。比如把[ 0.2 , 0.6 ]变成[0.25, 0.75]

from_logits = True 表示是原始数据,系统会帮你做softmax后再进行计算

 

 

实验代码:

import tensorflow as tf
import math

y_true = [1, 1]
y_pred = [[0.05, 0.15, 0], [0.1, 0.8, 0.1]]
res1 = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=False,
        reduction=tf.keras.losses.Reduction.SUM
        )(y_true,y_pred).numpy()

print('res1',res1) # res1 = 0.5108261   equas to  -(math.log(0.75)+math.log(0.8) )
 
###############################################

res2 = tf.keras.losses.SparseCategoricalCrossentropy(
        from_logits=True,
        reduction=tf.keras.losses.Reduction.SUM
        )(y_true,y_pred).numpy()

softmax = tf.nn.softmax(y_pred,axis=1)
print('softmax ',softmax.numpy())  # [[0.32718226 0.36159232 0.3112254 ]
                                   #  [0.24914338 0.50171316 0.24914338]]

print('res2',res2) # res2 = 1.7069646    equas to  -(math.log(0.36159232)+math.log(0.50171316) )

 

你可能感兴趣的:(TensorFlow)