pytorch之torch.nn.Identity()的作用及解釋
torch.nn.Identity()的作用及解釋
class Identity(Module):
r"""A placeholder identity operator that is argument-insensitive.
Args:
args: any argument (unused)
kwargs: any keyword argument (unused)
Examples::
>>> m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
>>> input = torch.randn(128, 20)
>>> output = m(input)
>>> print(output.size())
torch.Size([128, 20])
"""
def __init__(self, *args, **kwargs):
super(Identity, self).__init__()
def forward(self, input: Tensor) -> Tensor:
return input通過閱讀源碼可以看到,identity模塊不改變輸入。
直接return input
一種編碼技巧吧,比如我們要加深網(wǎng)絡(luò),有些層是不改變輸入數(shù)據(jù)的維度的,
在增減網(wǎng)絡(luò)的過程中我們就可以用identity占個位置,這樣網(wǎng)絡(luò)整體層數(shù)永遠不變,
看起來可能舒服一些,
可能理解的不到位。。。。
Pytorch-torch.nn.identity()方法

identity模塊不改變輸入,直接return input
一種編碼技巧吧,比如我們要加深網(wǎng)絡(luò),有些層是不改變輸入數(shù)據(jù)的維度的,在增減網(wǎng)絡(luò)的過程中我們就可以用identity占個位置,這樣網(wǎng)絡(luò)整體層數(shù)永遠不變,
應(yīng)用:
例如此時:如果此時我們使用了se_layer,那么就SELayer(dim),否則就輸入什么就輸出什么(什么都不做)
m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False) input = torch.randn(128, 20) output = m(input) print(output.size()) >> torch.Size([128, 20])
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python詳解argparse參數(shù)模塊之命令行參數(shù)
這篇文章主要介紹了Python詳解argparse參數(shù)模塊之命令行參數(shù),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考下面文章詳解2022-07-07
python+matplotlib實現(xiàn)禮盒柱狀圖實例代碼
這篇文章主要介紹了python+matplotlib實現(xiàn)禮盒柱狀圖實例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
centos6.5安裝python3.7.1之后無法使用pip的解決方案
今天小編就為大家分享一篇關(guān)于centos6.5安裝python3.7.1之后無法使用pip的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02

