torch.nn.PReLU
原型
CLASS torch.nn.PReLU(num_parameters=1, init=0.25, device=None, dtype=None)
参数
- num_parameters ([int]) – 需要学习的
a
a
a 的数量,尽管作为输入,只有两个值是合法的,
1或者 输入的通道数,默认为1。 - init ([float]) –
a
a
a 的初始值,默认为
0.25。
定义
PReLU ( x ) = max ( 0 , x ) + a ∗ min ( 0 , x ) \text{PReLU}(x)= \max(0, x) + a * \min(0, x) PReLU(x)=max(0,x)+a∗min(0,x)
or
PReLU ( x ) = { x , if x ≥ 0 a x , otherwise \text{PReLU}(x) = \begin{cases} x, & \text{if} x \geq 0 \\ ax, & \text{otherwise} \end{cases} PReLU(x)={x,ax,ifx≥0otherwise
图

代码
import torch
import torch.nn as nn
m = nn.PReLU()
input = torch.randn(4)
output = m(input)
print("input: ", input) # input: tensor([ 0.1061, -2.0532, 1.4081, -0.1516])
print("output: ", output) # output: tensor([ 0.1061, -0.5133, 1.4081, -0.0379], grad_fn=<PreluBackward>)
PReLU是PyTorch中的一个激活层,全称为预激活线性单元。它结合了线性项和ReLU的非线性项,其公式为PReLU(x)=max(0,x)+a*min(0,x)。参数a可以学习得到,初始值通常设置为0.25。在处理负值时,PReLU能提供更平滑的梯度,防止梯度消失问题。示例代码展示了如何创建并应用PReLU层。
9009

被折叠的 条评论
为什么被折叠?



