tensorflow实现softmax回归函数

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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值