torch.nn.functioanl及torch.nn(未完待续。。。)

torch.nn 接口的了解

torch.nn是专门为神经网络设计的模块化接口。nn构建于autograd之上,可以用来定义和运行神经网络。
nn.Module是nn中十分重要的类,包含网络各层的定义及forward方法。
定义自已的网络:
def init(self):
需要继承nn.Module类,并实现forward方法。super(model,self).init
一般把网络中具有可学习参数的层放在构造函数__init__()中,
不具有可学习参数的层(如ReLU)可放在构造函数中,也可不放在构造函数中(而在forward中使用nn.functional来代替)

只要在nn.Module的子类中定义了forward函数,backward函数就会被自动实现(利用Autograd)。
在forward函数中可以使用任何Variable支持的函数,毕竟在整个pytorch构建的图中,是Variable在流动。还可以使用
if,for,print,log等python语法.

注:Pytorch基于nn.Module构建的模型中,只支持mini-batch的Variable输入方式,
比如,只有一张输入图片,也需要变成 N x C x H x W 的形式:

input_image = torch.FloatTensor(1, 28, 28)
input_image = Variable(input_image)
input_image = input_image.unsqueeze(0)   # 1 x 1 x 28 x 28

torch.nn.functional包的内置函数

Convolution 函数

Pooling函数

非线性激活函数

Pytorch内的非线性激活函数多达十九种,主要是 relu,softmax,sigmoid,tanh及其相关变形。

非线性激活函数 函数表达式 适用场景
threshold(input, threshold, value) { x ,  if  x > threshold value ,  otherwise  \begin{cases} x, &\text{ if } x > \text{threshold} \\ \text{value}, &\text{ otherwise } \end{cases} 图像灰度处理
relu(input) m a x ( 0 , x ) max(0,x)
hardtanh(input, min_val=-1.0, max_val=1.0) { 1  if  x > 1 1  if  x < 1 x  otherwise  \begin{cases}1 & \text{ if } x > 1 \\ -1 & \text{ if } x < -1 \\x & \text{ otherwise } \\\end{cases}
relu6(input) min ( max ( 0 , x ) , 6 ) \min(\max(0,x), 6)
elu(input, alpha=1.0) max ( 0 , x ) + min ( 0 , α ( e x 1 ) \max(0,x) + \min(0, \alpha * (e^x - 1)
leaky_relu(input, negative_slope=0.01) max ( 0 , x ) + negative_slope min ( 0 , x ) \max(0, x) + \text{negative\_slope} * \min(0, x)
prelu(input, weight) max ( 0 , x ) + weight min ( 0 , x ) \max(0,x) + \text{weight} * \min(0,x)
rrelu(input, lower=0.125, upper=0.33, training=False) { x if  x 0 a x  otherwise  \begin{cases}x & \text{if } x \geq 0 \\ax & \text{ otherwise }\end{cases}
logsigmoid(input) l o g ( 1 1 + exp ( x ) ) log \left(\dfrac{1}{1 + \exp(-x)}\right)
hardshrink(input, lambd=0.5) { x ,  if  x > λ x ,  if  x < λ 0 ,  otherwise  \begin{cases} x, & \text{ if } x > \lambda \\ x, & \text{ if } x < -\lambda \\0, & \text{ otherwise } \end{cases}
tanhshrink(input) x Tanh ( x ) x - \text{Tanh}(x)
softsign(input) x 1 + a b s ( x ) \dfrac{x}{1+abs(x)}
softplus(input, beta=1, threshold=20) l o g ( 1 + e x ) log(1+e^x)
softmin(input) Softmax ( x ) \text{Softmax}(-x)
softmax(input) e x p ( x i ) j e x p ( x j ) \dfrac{exp(x_i)}{\sum_j exp(x_j)}
softshrink(input, lambd=0.5) { x λ ,  if  x > λ x + λ ,  if  x < λ 0 ,  otherwise  \begin{cases} x - \lambda, & \text{ if } x > \lambda \\ x + \lambda, & \text{ if } x < -\lambda \\ 0, & \text{ otherwise } \end{cases}
tanh(input) exp ( x ) exp ( x ) exp ( x ) + exp ( x ) \dfrac{\exp(x) - \exp(-x)}{\exp(x) + \exp(-x)}
sigmoid(input) 1 1 + e x \dfrac{1}{1+e^{-x}}

这里绘制出sigmoid,tanh,softmax,relu的函数图像
在这里插入图片描述

Normalization函数

线性函数

Dropout函数

距离函数

损失函数

vision functions

torch.nn包的内置函数

loss function

  • torch.nn.BCEWithLogitsLoss()
    封装好的对数似然损失函数
    ( x , y ) = L = { l 1 , , l N } , l n = w n [ y n log σ ( x n ) + ( 1 y n ) log ( 1 σ ( x n ) ) ] \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_n \left[ y_n \cdot \log \sigma(x_n) + (1 - y_n) \cdot \log (1 - \sigma(x_n)) \right]

参考资料

1)https://blog.csdn.net/Jorah/article/details/89159484

猜你喜欢

转载自blog.csdn.net/qq_39446239/article/details/89176442