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

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

如何在 pytorch nn.module 中設置圖層的值?

如何在 pytorch nn.module 中設置圖層的值?

狐的傳說 2022-10-18 16:06:52
我有一個模型,我正在嘗試使用它。我正在解決這些錯誤,但現在我認為它已經歸結為我層中的值。我收到此錯誤:RuntimeError: Given groups=1, weight of size 24 1 3 3, expected input[512, 50, 50, 3] to have 1 channels, but got 50 channels instead我的參數是:LR = 5e-2N_EPOCHS = 30BATCH_SIZE = 512DROPOUT = 0.5我的圖像信息是:depth=24channels=3original height = 1600original width = 1200resized to 50x50這是我的數據的大?。篢rain shape (743, 50, 50, 3) (743, 7)Test shape (186, 50, 50, 3) (186, 7)Train pixels 0 255 188.12228712427097 61.49539262385051Test pixels 0 255 189.35559211469533 60.688278787628775我在這里試圖了解每一層的期望值,但是當我在這里輸入它所說的內容時,https://towardsdatascience.com/pytorch-layer-dimensions-what-sizes-should-they-be-and-為什么-4265a41e01fd,它給了我關于錯誤通道和內核的錯誤。我發現 torch_summary 讓我對輸出有更多的了解,但它只會提出更多的問題。這是我的 torch_summary 代碼:from torchvision import modelsfrom torchsummary import summaryimport torchimport torch.nn as nnclass CNN(nn.Module):    def __init__(self):        super(CNN, self).__init__()        self.conv1 = nn.Conv2d(1,24, kernel_size=5)  # output (n_examples, 16, 26, 26)        self.convnorm1 = nn.BatchNorm2d(24) # channels from prev layer        self.pool1 = nn.MaxPool2d((2, 2))  # output (n_examples, 16, 13, 13)        self.conv2 = nn.Conv2d(24,48,kernel_size=5)  # output (n_examples, 32, 11, 11)        self.convnorm2 = nn.BatchNorm2d(48) # 2*channels?        self.pool2 = nn.AvgPool2d((2, 2))  # output (n_examples, 32, 5, 5)        self.linear1 = nn.Linear(400,120)  # input will be flattened to (n_examples, 32 * 5 * 5)        self.linear1_bn = nn.BatchNorm1d(400) # features?        self.drop = nn.Dropout(DROPOUT)        self.linear2 = nn.Linear(400, 10)        self.act = torch.relu
查看完整描述

1 回答

?
慕姐4208626

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

看來您輸入x張量軸的順序錯誤。
正如您在輸入中看到的,必須是doc Conv2d(N, C, H, W)

N是批量大小,C表示通道數,H是以像素為單位的輸入平面的高度,以像素為單位W的寬度。

因此,為了正確使用torch.permute前傳中的交換軸。

...

def forward(self, x):

    x = x.permute(0, 3, 1, 2)

    ...

    ...

    return self.linear2(x)

...

示例permute:


t = torch.rand(512, 50, 50, 3)

t.size()

torch.Size([512, 50, 50, 3])


t = t.permute(0, 3, 1, 2)

t.size()

torch.Size([512, 3, 50, 50])


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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