Softmax公式
softmax函数可以把多个值归一化到(0,1)区间中,以实现分类等问题
代码实现
import tensorflow as tf
def softmax(logits, axis=-1):
return tf.exp(logits)/tf.reduce_sum(tf.exp(logits), axis, keepdims=True)
测试
X = tf.random.normal(shape=(2, 5))
X_prob = softmax(X)
tf.reduce_sum(X_prob, axis=1)
print(X)
print(X_prob)
输出:
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[ 0.1738882 , 0.7230672 , -0.8248408 , -0.8263163 , 1.5385914 ],
[-1.3104331 , 0.91867334, 1.5094105 , -0.17404696, -0.16576393]],
dtype=float32)>
<tf.Tensor: shape=(2, 5), dtype=float32, numpy=
array([[0.13545468, 0.23458456, 0.04989437, 0.0498208 , 0.5302456 ],
[0.03000959, 0.27883592, 0.50338775, 0.09349456, 0.0942722 ]],
dtype=float32)>
交叉熵损失函数
由于在 Tensorflow 涉及运算类型转换的问题,使用cast函数对张量进行类型转换
def cross_entropy(y_hat, y):
y = tf.cast(tf.reshape(y, shape=[-1, 1]),dtype=tf.int32)
y = tf.one_hot(y, depth=y_hat.shape[-1])
y = tf.cast(tf.reshape(y, shape=[-1, y_hat.shape[-1]]),dtype=tf.int32)
return -tf.math.log(tf.boolean_mask(y_hat, y)+1e-8)