在使用resnet152模型训练fashion_mnist时报错
具体代码如下
model = torchvision.models.resnet152(pretrained=True)
fashion_mnist = keras.datasets.fashion_mnist
data,label = fashion_mnist.load_data()
for param in model.parameters():
param.requires_gradq = False
model.fc = torch.nn.Linear(2048, 28)
loss_fn = torch.nn.MSELoss(reduction='sum')
opt = torch.optim.SGD(model.fc.parameters(), lr=0.001)
然而在下面的代码,也就是训练模型的时候遇到报错
for i in range(200):
y = model(data)
loss = loss_fn(label, y)
opt.zero_grad()
loss.backward()
opt.step()
print(loss)
报错内容 :
TypeError Traceback (most recent call last)
<ipython-input-20-042e5c6d56e7> in <module>
1 for i in range(200):
----> 2 y = model(data)
3 loss = loss_fn(label, y)
4 opt.zero_grad()
5 loss.backward()
~/yes/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []
~/yes/lib/python3.8/site-packages/torchvision/models/resnet.py in forward(self, x)
247
248 def forward(self, x: Tensor) -> Tensor:
--> 249 return self._forward_impl(x)
250
251
~/yes/lib/python3.8/site-packages/torchvision/models/resnet.py in _forward_impl(self, x)
230 def _forward_impl(self, x: Tensor) -> Tensor:
231 # See note [TorchScript super()]
--> 232 x = self.conv1(x)
233 x = self.bn1(x)
234 x = self.relu(x)
~/yes/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []
~/yes/lib/python3.8/site-packages/torch/nn/modules/conv.py in forward(self, input)
444
445 def forward(self, input: Tensor) -> Tensor:
--> 446 return self._conv_forward(input, self.weight, self.bias)
447
448 class Conv3d(_ConvNd):
~/yes/lib/python3.8/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
440 weight, bias, self.stride,
441 _pair(0), self.dilation, self.groups)
--> 442 return F.conv2d(input, weight, bias, self.stride,
443 self.padding, self.dilation, self.groups)
444
TypeError: conv2d() received an invalid combination of arguments - got (tuple, Parameter, NoneType, tuple, tuple, tuple, int), but expected one of:
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!tuple!, !Parameter!, !NoneType!, !tuple!, !tuple!, !tuple!, int)
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!tuple!, !Parameter!, !NoneType!, !tuple!, !tuple!, !tuple!, int)
我尝试将data和label转torch.tensor
data = torch.tensor(data)
可是依旧报错
TypeError Traceback (most recent call last)
<ipython-input-32-42ca4109f755> in <module>
----> 1 data = torch.tensor(data)
TypeError: not a sequence