Pytorch CNN:RuntimeError:给定的组=1,大小的权重[16,16,3],预期输入[500,119357]有16个通道,但得到了1个通道
运行上面的代码,出现以下错误:Pytorch CNN:RuntimeError:给定的组=1,大小的权重[16,16,3],预期输入[500,119357]有16个通道,但得到了1个通道,pytorch,cnn,Pytorch,Cnn,运行上面的代码,出现以下错误: class ConvolutionalNetwork(nn.Module): def __init__(self, in_features, trial): super().__init__() self.in_features = in_features self.trial = trial # this computes num features outputted from the tw
class ConvolutionalNetwork(nn.Module):
def __init__(self, in_features, trial):
super().__init__()
self.in_features = in_features
self.trial = trial
# this computes num features outputted from the two conv layers
c1 = int(((self.in_features - 2)) / 64) # this is to account for the loss due to conversion to int type
c2 = int((c1 - 2) / 64)
self.n_conv = int(c2 * 16)
# self.n_conv = int((( ( (self.in_features - 2)/4 ) - 2 )/4 ) * 16)
self.conv1 = nn.Conv1d(16, 16, 3, 1)
self.conv1_bn = nn.BatchNorm1d(16)
self.conv2 = nn.Conv1d(16, 16, 3, 1)
self.conv2_bn = nn.BatchNorm1d(16)
# self.dp = nn.Dropout(trial.suggest_uniform('dropout_rate',0,1.0))
self.dp = nn.Dropout(0.5)
self.fc3 = nn.Linear(self.n_conv, 2)
def forward(self, x):
# shape x for conv 1d op
x = x.view(-1, 1, self.in_features)
x = self.conv1(x)
x = F.tanh(x)
x = F.max_pool1d(x, 64, 64)
x = self.conv2(x)
x = F.tanh(x)
x = F.max_pool1d(x, 64, 64)
x = x.view(-1, self.n_conv)
x = self.dp(x)
x = self.fc3(x)
x = F.log_softmax(x, dim=1)
return x
有人能就此提出建议吗?它表示输入存在差异,但上面的代码在较早时运行良好,不确定在我重新排列代码后发生了什么。好的,在输入“前进”方法后,您正在重塑输入数组,因此它只有一个通道:
RuntimeError: Given groups=1, weight of size [16, 16, 3], expected input[500, 1, 19357] to have 16 channels, but got 1 channels instead.
同时,在模型构造函数中,您指定conv1有16个通道作为输入:
x = x.view(-1, 1, self.in_features)
因此,期望16个通道但收到1个通道的错误
这里有两件事需要注意:
- 如果您习惯于tensorflow,可能您认为通道是最后一个维度,但在pytorch中,通道位于第一个维度。请看下面的图片。在重新调整数据形状时,请考虑这一点
- Conv1d不知道输入的长度(我告诉您这是为了以防万一in_特性代表长度)
我无法为您提供具体的解决方案,因为我不确定您正在尝试做什么。好的,在进入“前进”方法后,您正在重塑您的输入阵列,使其只有一个通道:
RuntimeError: Given groups=1, weight of size [16, 16, 3], expected input[500, 1, 19357] to have 16 channels, but got 1 channels instead.
同时,在模型构造函数中,您指定conv1有16个通道作为输入:
x = x.view(-1, 1, self.in_features)
因此,期望16个通道但收到1个通道的错误
这里有两件事需要注意:
- 如果您习惯于tensorflow,可能您认为通道是最后一个维度,但在pytorch中,通道位于第一个维度。请看下面的图片。在重新调整数据形状时,请考虑这一点
- Conv1d不知道输入的长度(我告诉您这是为了以防万一in_特性代表长度)