[英]ValueError: Target size (torch.Size([1, 1])) must be the same as input size (torch.Size([1, 2]))
[英]ValueError: Target size (torch.Size([2])) must be the same as input size (torch.Size([504, 2]))
我有一個用於分類的 CNN 網絡(2 個標簽)。 它由多個在列表中鏈接在一起的 conv3d 層組成,然后在我的前向 function 中的 for 循環中執行。 model如下:
(0) : Sequential(
(0): Dropout3d(p=0.2, inplace=False)
(1): Conv3d(1, 8, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
(2): BatchNorm3d(8, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ELU(alpha=1.0)
(4): Conv3d(8, 16, kernel_size=(3, 3, 3), stride=(1, 1, 1))
(5): BatchNorm3d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(6): ELU(alpha=1.0)
)
(1) : Sequential(
(0): Dropout3d(p=0.1, inplace=False)
(1): Conv3d(16, 24, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
(2): BatchNorm3d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): ELU(alpha=1.0)
(4): Conv3d(24, 32, kernel_size=(3, 3, 3), stride=(1, 1, 1))
(5): BatchNorm3d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(6): ELU(alpha=1.0)
)
(2) : Sequential(
(0): Conv3d(32, 80, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
(1): BatchNorm3d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ELU(alpha=1.0)
(3): Conv3d(80, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1))
(4): BatchNorm3d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ELU(alpha=1.0)
)
(3) : Sequential(
(0): Conv3d(128, 72, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))
(1): BatchNorm3d(72, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ELU(alpha=1.0)
(3): Conv3d(72, 16, kernel_size=(3, 3, 3), stride=(1, 1, 1))
(4): BatchNorm3d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ELU(alpha=1.0)
)
(finallayer) : Conv3d(16, 2, kernel_size=(4, 5, 4), stride=(1, 1, 1))
在每個順序塊 [0-4] 之后,我們使用以下方法進行最大池化:
max_pool3d(x, kernel_size=2, stride=2)
然后對finallayer的output進行整形:
x = x.reshape(-1, 2)
每個順序和最大池化層調用后的維度如下所示:
Input dimension before calling forward: torch.Size([2, 1, 182, 218, 182])
#Here, 2 is batch size and 1 is channel.
Output dimension of (0) convolution layer: [2, 16, 180, 216, 180]
#printing as list(x.shape)
Output dimension of pool (0) layer : [2, 16, 90, 108, 90]
Output dimension of (1) convolution layer : [2, 32, 88, 106, 88]
Output dimension of pool (1) layer : [2, 32, 44, 53, 44]
Output dimension of (2) convolution layer : [2, 128, 42, 51, 42]
Output dimension of pool (2) layer : [2, 128, 21, 25, 21]
Output dimension of (3) convolution layer : [2, 16, 19, 23, 19]
Output dimension of pool (3) layer : [2, 16, 9, 11, 9]
Dimension before calling finallayer : [2, 16, 9, 11, 9]
Dimension after finallayer but before reshape : [2, 2, 6, 7, 6]
Dimension post reshape : [504, 2]
在調用內部調用criterion
的binary_cross_entropy_with_logits
的標准時。 我得到這個尺寸不匹配。
ValueError: Target size (torch.Size([2])) must be the same as input size (torch.Size([504, 2]))
我已經研究過他們提到使用torch.unsqueeze()
的類似問題,但是我不確定我是否可以使用unsqueeze
創建 504 行。
此外,我在每次調用后交叉檢查了每個 output 層通道號並對其進行了驗證。 任何幫助表示贊賞。
我認為您缺少將Batch x Features
轉換為Batch x Classes
的 全連接層
將此添加到您的 model 構造函數(假設您的批量大小為 = 1)
self.fc = nn.Linear(1008, 2)
這到前傳的結尾:-
x = x.reshape(batch_size, -1)
x = self.fc(x)
確保按批次尺寸縮放 1008。 理想情況下,您應該能夠在最后計算 conv output 的維度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.