亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 nn.Identity 進行殘差學習背后的想法是什么?

使用 nn.Identity 進行殘差學習背后的想法是什么?

弒天下 2023-10-11 10:00:55
所以,我已經閱讀了大約一半的原始 ResNet 論文,并且正在嘗試找出如何為表格數據制作我的版本。我讀過一些關于 PyTorch 如何工作的博客文章,并且我看到大量使用nn.Identity(). 現在,論文還經常使用恒等映射這個術語。然而,它只是指以元素方式將層堆棧的輸入添加到同一堆棧的輸出。如果輸入和輸出維度不同,那么本文討論了用零填充輸入或使用矩陣W_s將輸入投影到不同的維度。這是我在博客文章中找到的殘差塊的抽象:class ResidualBlock(nn.Module):    def __init__(self, in_channels, out_channels, activation='relu'):        super().__init__()        self.in_channels, self.out_channels, self.activation = in_channels, out_channels, activation        self.blocks = nn.Identity()        self.shortcut = nn.Identity()           def forward(self, x):        residual = x        if self.should_apply_shortcut: residual = self.shortcut(x)        x = self.blocks(x)        x += residual        return x        @property    def should_apply_shortcut(self):        return self.in_channels != self.out_channels    block1 = ResidualBlock(4, 4)以及我自己對虛擬張量的應用:x = tensor([1, 1, 2, 2])block1 = ResidualBlock(4, 4)block2 = ResidualBlock(4, 6)x = block1(x)print(x)x = block2(x)print(x)>>> tensor([2, 2, 4, 4])>>> tensor([4, 4, 8, 8])所以在最后,x = nn.Identity(x)除了模仿原始論文中的數學術語之外,我不確定它的用途是什么。但我確信情況并非如此,而且它有一些我還沒有看到的隱藏用途。會是什么呢?編輯這是實施殘差學習的另一個例子,這次是在 Keras 中。它按照我上面的建議進行操作,只保留輸入的副本以添加到輸出中:def residual_block(x: Tensor, downsample: bool, filters: int,                                        kernel_size: int = 3) -> Tensor:    y = Conv2D(kernel_size=kernel_size,               strides= (1 if not downsample else 2),               filters=filters,               padding="same")(x)    y = relu_bn(y)    y = Conv2D(kernel_size=kernel_size,               strides=1,               filters=filters,               padding="same")(y)    if downsample:        x = Conv2D(kernel_size=1,                   strides=2,                   filters=filters,                   padding="same")(x)    out = Add()([x, y])    out = relu_bn(out)    return out
查看完整描述

1 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

ResNet 實現

我能想到的最簡單的投影通用版本是這樣的:

class Residual(torch.nn.Module):

? ? def __init__(self, module: torch.nn.Module, projection: torch.nn.Module = None):

? ? ? ? super().__init__()

? ? ? ? self.module = module

? ? ? ? self.projection = projection


? ? def forward(self, inputs):

? ? ? ? output = self.module(inputs)

? ? ? ? if self.projection is not None:

? ? ? ? ? ? inputs = self.projection(inputs)

? ? ? ? return output + inputs

您可以傳遞module兩個堆疊卷積之類的東西,并添加1x1卷積(帶有填充或步幅或其他東西)作為投影模塊。


對于tabular數據,您可以將其用作module(假設您的輸入具有50功能):


torch.nn.Sequential(

? ? torch.nn.Linear(50, 50),

? ? torch.nn.ReLU(),

? ? torch.nn.Linear(50, 50),

? ? torch.nn.ReLU(),

? ? torch.nn.Linear(50, 50),

)

基本上,您所要做的就是將input某個模塊添加到其輸出中,僅此而已。


理由如下nn.Identity

構建神經網絡(然后讀取它們)可能會更容易,例如批量歸一化(取自上述 PR):


batch_norm = nn.BatchNorm2d

if dont_use_batch_norm:

? ? batch_norm = Identity

現在您可以nn.Sequential輕松地使用它:


nn.Sequential(

? ? ...

? ? batch_norm(N, momentum=0.05),

? ? ...

)

當打印網絡時,它總是具有相同數量的子模塊(帶有BatchNorm或Identity),這也使整個過程在我看來更加流暢。


這里提到的另一個用例可能是刪除現有神經網絡的部分內容:


net = tv.models.alexnet(pretrained=True)

# Assume net has two parts

# features and classifier

net.classifier = Identity()

net.features(input)現在,您可以運行而不是運行net(input),這對其他人來說也更容易閱讀。


查看完整回答
反對 回復 2023-10-11
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號