pytorch中LN(LayerNorm)以及Relu和其變種的輸出

主要就是了解一下pytorch中的使用layernorm這種歸一化之後的數據變化,以及數據使用relu,prelu,leakyrelu之後的變化。

import torch
import torch.nn as nn
import torch.nn.functional as F
class model(nn.Module):
    def __init__(self):
        super(model, self).__init__()
        self.LN=nn.LayerNorm(10,eps=0,elementwise_affine=True)
        self.PRelu=nn.PReLU(init=0.25)
        self.Relu=nn.ReLU()
        self.LeakyReLU=nn.LeakyReLU(negative_slope=0.01,inplace=False)
    def forward(self,input ):
        out=self.LN(input)
        print("LN:",out)
        out1=self.PRelu(out)
        print("PRelu:",out1)
        out2=self.Relu(out)
        print("Relu:",out2)
        out3=self.LeakyReLU(out)
        print("LeakyRelu:",out3)
        return out
tensor=torch.tensor([-0.9,0.1,0,-0.1,0.9,-0.4,0.9,-0.5,0.8,0.1])
net=model()
print(tensor)
net(tensor)

輸出:

tensor([-0.9000,  0.1000,  0.0000, -0.1000,  0.9000, -0.4000,  0.9000, -0.5000,
         0.8000,  0.1000])
LN: tensor([-1.6906,  0.0171, -0.1537, -0.3245,  1.3833, -0.8368,  1.3833, -1.0076,
         1.2125,  0.0171], grad_fn=<NativeLayerNormBackward>)
Relu: tensor([0.0000, 0.0171, 0.0000, 0.0000, 1.3833, 0.0000, 1.3833, 0.0000, 1.2125,
        0.0171], grad_fn=<ReluBackward0>)
PRelu: tensor([-0.4227,  0.0171, -0.0384, -0.0811,  1.3833, -0.2092,  1.3833, -0.2519,
         1.2125,  0.0171], grad_fn=<PreluBackward>)
LeakyRelu: tensor([-0.0169,  0.0171, -0.0015, -0.0032,  1.3833, -0.0084,  1.3833, -0.0101,
         1.2125,  0.0171], grad_fn=<LeakyReluBackward0>)

從上面可以看出,這個LayerNorm的歸一化,並不是將數據限定在0-1之間,也沒有進行一個類似於高斯分佈一樣的分數,只是將其進行了一個處理,對應的數值得到了一些變化,相同數值的變化也是相同的。

Relu的則是單純將小於0的數變成了0,減少了梯度消失的可能性

PRelu是一定程度上的保留了負值,根據init給的值。

LeakyRelu也是一定程度上保留負值,不過比較小,應該是根據negative_slope給的值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章