最近在学习莫凡的Pytorch教程,为了避免忘记,现对一些学习中的知识点进行记录。对本系列博客,有以下几点需要注意:
好了,废话不多说,开始今天的学习笔记记录。
本篇笔记主要对应于莫凡Pytorch中的2.3和2.4节内容。
对于激活函数,维基百科给出的定义是这样的:
“在计算网络中, 一个节点的激活函数定义了该节点在给定的输入或输入的集合下的输出。标准的计算机芯片电路可以看作是根据输入得到"开"(1)或"关"(0)输出的数字网络激活函数。这与神经网络中的线性感知机的行为类似。 一种函数(例如 ReLU 或 S 型函数),用于对上一层的所有输入求加权和,然后生成一个输出值(通常为非线性值),并将其传递给下一层。”
在神经网络中,激活函数是一个非线性函数。因为倘若是线性函数,那么由线性函数的齐次性和可加性就可以将这些线性函数全部等效为一个线性函数,从而失去深度网络模型强大的表示能力。
在Pytorch中常用的几个激活函数有:
f ( x ) = { 0 , x < 0 x , x ≥ 0 f(x) = begin{cases} 0, & x< 0 \ x, & x ge 0 end{cases} f(x)={0,x,x<0x≥0
f ( x ) = t a n h ( x ) = e x − e − x e x + e − x f(x) = tanh(x) = frac{e^{x}-e^{-x}}{e^{x}+e^{-x}} f(x)=tanh(x)=ex+e−xex−e−x
f ( x ) = σ ( x ) = 1 1 + e − x f(x)= sigma(x) = frac{1}{1+e^{-x}} f(x)=σ(x)=1+e−x1
f ( x ) = ln ( 1 + e x ) f(x) = ln(1+e^x) f(x)=ln(1+ex)
对其可视化如下:
import torch
functional as F
import matplotlib.pyplot as plt
# fake data
x = torch.linspace(-5, 5, 200) # x data (tensor), shape=(100, 1)
x_np = x.numpy()
y_relu = F.relu(x).numpy()
y_sigmoid = F.sigmoid(x).numpy()
y_tanh = F.tanh(x).numpy()
y_softplus = F.softplus(x).numpy()
plt.figure(1, figsize=(8, 6))
plt.subplot(221)
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim(0, 5)
plt.legend(loc='best')plt.subplot(222)
plt.plot(x_np, y_sigmoid, c='g', label='sigmoid')
plt.ylim(0, 1)
plt.legend(loc='best')plt.subplot(223)
plt.plot(x_np, y_tanh, c='b', label='tanh')
plt.ylim(-1, 1)
plt.legend(loc='best')plt.subplot(224)
plt.plot(x_np, y_softplus, c='k', label='softplus')
plt.ylim(0, 5)
plt.legend(loc='best')
本文发布于:2024-02-03 03:51:35,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170690349548464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |