Tensorflow(二十) —— 激活函数及其梯度

Tensorflow(二十) —— 激活函数及其梯度

1. sigmoid / logistic

"""
该函数的致命缺陷:梯度长时间得不到更新
"""
x = tf.Variable(tf.linspace(-10.,10.,100))

with tf.GradientTape() as tape:
    y = tf.nn.sigmoid(x)
[dy_dx] = tape.gradient(y,[x])

plt.figure()
plt.plot(x.numpy(),y.numpy())
plt.title("sigmoid")

plt.figure()
plt.plot(x.numpy(),dy_dx.numpy())
plt.title("derivative for sigmoid")
"""
梯度适合[-3,3]
"""

在这里插入图片描述
在这里插入图片描述

2. Tanh 在RNN中用得多

x = tf.linspace(-10.,10.,100)
with tf.GradientTape() as tape:
        tape.watch([x])
        y = tf.nn.tanh(x)
[dy_dx] = tape.gradient(y,[x])

fig = plt.figure(figsize=[20,24])
ax1 = fig.add_subplot(211)
ax1.plot(x.numpy(),y.numpy())
plt.title("tanh")

ax2 = fig.add_subplot(212)
ax2.plot(x.numpy(),dy_dx.numpy())
plt.title("derivative for tanh")

在这里插入图片描述

3. relu函数

# ****************** relu函数
"""
rectified linear unit
tf.nn.relu()
tf.nn.leaky_relu() x<0 时 可取很小的k y=kx
"""
x = tf.linspace(-10.,10.,100)

with tf.GradientTape(persistent = True) as tape:
    tape.watch([x])
    y1 = tf.nn.relu(x)
    y2 = tf.nn.leaky_relu(x)
[dy1_dx] = tape.gradient(y1,[x])
[dy2_dx] = tape.gradient(y2,[x])

fig = plt.figure(figsize=[40,24])
ax1 = fig.add_subplot(221)
ax1.plot(x.numpy(),y1.numpy())

ax2 = fig.add_subplot(222)
ax2.plot(x.numpy(),y2.numpy())

ax3 = fig.add_subplot(223)
ax3.plot(x.numpy(),dy1_dx.numpy())

ax4 = fig.add_subplot(224)
ax4.plot(x.numpy(),dy2_dx.numpy())

在这里插入图片描述

本文为参考龙龙老师的“深度学习与TensorFlow 2入门实战“课程书写的学习笔记

by CyrusMay 2022 04 17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值