'RuntimeError: mat1 and mat2 shapes cannot be multiplied', how do I solve it?
'RuntimeError: mat1 and mat2 shapes cannot be multiplied', how do I solve it?
我正在尝试实现一个 ResNet1D,它应该将 window ECG 信号作为输入,包含一个心跳,在我的例子中有 950 个样本,我想预测长度QRS 间期。
这是网络实现的代码:
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv1d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm1d(planes)
self.conv2 = nn.Conv1d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm1d(planes)
self.conv3 = nn.Conv1d(planes, self.expansion*planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm1d(self.expansion*planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(
nn.Conv1d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm1d(self.expansion*planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, num_blocks, num_classes=3):
super(ResNet, self).__init__()
self.in_planes = 64
self.avg1 = nn.AvgPool1d(1024, stride=2)
self.conv1 = nn.Conv1d(1, 128, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm1d(128)
self.layer1 = self._make_layer(block, 128, num_blocks[0], stride=1)
self.layer2 = self._make_layer(block, 256, num_blocks[1], stride=2)
self.layer3 = self._make_layer(block, 512, num_blocks[2], stride=2)
self.layer4 = self._make_layer(block, 1024, num_blocks[3], stride=2)
self.linear1 = nn.Linear(19968*block.expansion, 1024)
self.linear2 = nn.Linear(1024, num_classes)
def _make_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1]*(num_blocks-1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
def forward(self, x):
out = self.avg1(x)
out = F.rel(self.bn1(self.conv1(out)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = F.avg_pool1d(out, 16)
out = out.view(out.size(0), -1)
out = self.linear1(out)
out = self.linear2(out)
return out
def ResNet50():
return ResNet(Bottleneck, [3, 4, 6, 3], num_classes=1)
我提供给网络的输入是一个数据加载器,批量大小 = 32,通道数 = 1,样本长度 = 950。
训练网络时出现此错误:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x3584 and 19968x1024)
我知道错误出在线性层中,但我不明白我应该如何更改尺寸才能使其正常工作。你能给我解释一下吗?
如果第一个矩阵的列数与第二个矩阵的行数匹配,则矩阵是可乘的。例如,MxN * NxK。所以这里你有错误的形状。您始终可以自己计算每一层输出的大小,以确保您的形状是否正确。
所以在这里我认为如果你改变这个应该可以工作
self.linear1 = nn.Linear(19968*block.expansion, 1024)
至此
self.linear1 = nn.Linear(3584*block.expansion, 1024)
我正在尝试实现一个 ResNet1D,它应该将 window ECG 信号作为输入,包含一个心跳,在我的例子中有 950 个样本,我想预测长度QRS 间期。
这是网络实现的代码:
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv1d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm1d(planes)
self.conv2 = nn.Conv1d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm1d(planes)
self.conv3 = nn.Conv1d(planes, self.expansion*planes, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm1d(self.expansion*planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(
nn.Conv1d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm1d(self.expansion*planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
out += self.shortcut(x)
out = F.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, num_blocks, num_classes=3):
super(ResNet, self).__init__()
self.in_planes = 64
self.avg1 = nn.AvgPool1d(1024, stride=2)
self.conv1 = nn.Conv1d(1, 128, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm1d(128)
self.layer1 = self._make_layer(block, 128, num_blocks[0], stride=1)
self.layer2 = self._make_layer(block, 256, num_blocks[1], stride=2)
self.layer3 = self._make_layer(block, 512, num_blocks[2], stride=2)
self.layer4 = self._make_layer(block, 1024, num_blocks[3], stride=2)
self.linear1 = nn.Linear(19968*block.expansion, 1024)
self.linear2 = nn.Linear(1024, num_classes)
def _make_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1]*(num_blocks-1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
def forward(self, x):
out = self.avg1(x)
out = F.rel(self.bn1(self.conv1(out)))
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = F.avg_pool1d(out, 16)
out = out.view(out.size(0), -1)
out = self.linear1(out)
out = self.linear2(out)
return out
def ResNet50():
return ResNet(Bottleneck, [3, 4, 6, 3], num_classes=1)
我提供给网络的输入是一个数据加载器,批量大小 = 32,通道数 = 1,样本长度 = 950。 训练网络时出现此错误:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x3584 and 19968x1024)
我知道错误出在线性层中,但我不明白我应该如何更改尺寸才能使其正常工作。你能给我解释一下吗?
如果第一个矩阵的列数与第二个矩阵的行数匹配,则矩阵是可乘的。例如,MxN * NxK。所以这里你有错误的形状。您始终可以自己计算每一层输出的大小,以确保您的形状是否正确。 所以在这里我认为如果你改变这个应该可以工作
self.linear1 = nn.Linear(19968*block.expansion, 1024)
至此
self.linear1 = nn.Linear(3584*block.expansion, 1024)