演習問題でPytorchによるCIFAR-10のCNNによる学習を行っていました。エラー構文を見ると conv2d()の中身が違うとのことですが入力チャネルと出力チャネル、フィルターサイズが必要な引数なのになぜこのようなエラー構文がでるのかわかりません。原因と解決策を教えてほしいです
import os from torchvision import datasets import torchvision.transforms as transforms from torch.utils.data import DataLoader root = './data' transform_train = transforms.Compose( [transforms.RandomHorizontalFlip(0.2), transforms.RandomRotation(15), transforms.ColorJitter(brightness=0.3, saturation=0.3), transforms.ToTensor(), transforms.Normalize((0.5), (0.5)) ]) transform_val = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5), (0.5)) ]) f_mnist_train = datasets.CIFAR10( root=root, download=True, train=True, transform=transform_train) f_mnist_test = datasets.CIFAR10( root=root, download=True, train=False, transform=transform_val) batch_size = 64 train_dataloader = DataLoader(f_mnist_train, batch_size=batch_size, shuffle=True) test_dataloader = DataLoader(f_mnist_test, batch_size=batch_size, shuffle=False) import torch.nn as nn class CNN(nn.Module): def __init__(self): super().__init__() # 第1層: 畳み込み層1 self.conv1 = nn.Conv2d(in_channels=3, # 入力チャネル数 out_channels=32, # 出力チャネル数 kernel_size=3, # フィルターサイズ padding=True, # パディングを行う padding_mode='zeros') # 正規化 self.bn1 = torch.nn.BatchNorm2d(32) # 第2層: 畳み込み層2 self.conv2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=True, padding_mode='zeros') self.bn2 = torch.nn.BatchNorm2d(32) # 第3層: プーリング層1 self.pool1 = nn.MaxPool2d(2, 2) self.dropout1 = nn.Dropout2d(0.2) # 第4層: 畳み込み層3 self.conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=True, padding_mode='zeros') self.bn3 = torch.nn.BatchNorm2d(64) # 第5層: 畳み込み層4 self.conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=True, padding_mode='zeros') self.bn4 = torch.nn.BatchNorm2d(64) # 第6層: プーリング層2 self.pool2 = nn.MaxPool2d(2, 2) self.dropout2 = nn.Dropout2d(0.3) # 第7層: 畳み込み層5 self.conv5 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=True, padding_mode='zeros') self.bn5 = torch.nn.BatchNorm2d(128) # 第8層: 畳み込み層6 self.conv6 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=True, padding_mode='zeros') self.bn6 = torch.nn.BatchNorm2d(128) # 第9層: プーリング層3 self.pool3 = nn.MaxPool2d(2, 2) self.dropout3 = nn.Dropout2d(0.4) # 第10層: 全結合層1 self.fc1 = nn.Linear(128 * 4 * 4, 128) self.dropout4 = nn.Dropout2d(0.4) # 第11層: 出力層 self.fc2 = nn.Linear(128, 10) def forward(self, x): x = torch.relu(self.conv1(x)) x = self.bn1(x) x = torch.relu(self.conv2(x)) x = self.bn2(x) x = self.pool1(x) x = self.dropout1(x) x = torch.relu(self.conv3(x)) x = self.bn3(x) x = torch.relu(self.conv4(x)) x = self.bn4(x) x = self.pool2(x) x = self.dropout2(x) x = torch.relu(self.conv5(x)) x = self.bn5(x) x = torch.relu(self.conv6(x)) x = self.bn6(x) x = self.pool3(x) x = self.dropout3(x) x = x.view(-1, 128 * 4 * 4) # フラット化 x = torch.relu(self.fc1(x)) x = self.dropout4(x) x = self.fc2(x) return x import torch device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = CNN().to(device) import torch.optim criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam( model.parameters(), lr=0.001, weight_decay=0.0001) model.train() preds = model(x) loss = criterion(preds, t) optimizer.zero_grad() loss.backward() optimizer.step() return loss, preds def test_step(x, t): model.eval() preds = model(x) loss = criterion(preds, t) return loss, preds from sklearn.metrics import accuracy_score epochs = 120 history = {'loss':[],'accuracy':[], 'test_loss':[], 'test_accuracy':[]} scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( optimizer, mode='max', factor=0.1, patience=5, threshold=0.0001, verbose=True ) for epoch in range(epochs): train_loss = 0. train_acc = 0. test_loss = 0. test_acc = 0. for (x, t) in train_dataloader: x, t = x.to(device), t.to(device) loss, preds = train_step(x, t) train_loss += loss.item() train_acc += accuracy_score( t.tolist(), preds.argmax(dim=-1).tolist() ) for (x, t) in test_dataloader: x, t = x.to(device), t.to(device) loss, preds = test_step(x, t) test_loss += loss.item() test_acc += accuracy_score( t.tolist(), preds.argmax(dim=-1).tolist() ) avg_train_loss = train_loss / len(train_dataloader) avg_train_acc = train_acc / len(train_dataloader) avg_test_loss = test_loss / len(test_dataloader) avg_test_acc = test_acc / len(test_dataloader) history['loss'].append(avg_train_loss) history['accuracy'].append(avg_train_acc) history['test_loss'].append(avg_test_loss) history['test_accuracy'].append(avg_test_acc) if (epoch + 1) % 1 == 0: print( 'epoch({}) train_loss: {:.4} train_acc: {:.4} val_loss: {:.4} val_acc: {:.4}'.format( epoch+1, avg_train_loss, # 訓練データの損失を出力 avg_train_acc, # 訓練データの精度を出力 avg_test_loss, # テストデータの損失を出力 avg_test_acc # テストデータの精度を出力 )) # スケジューラー scheduler.step(avg_test_acc
```ここに言語を入力 TypeError Traceback (most recent call last) File <timed exec>:32, in <module> Input In [13], in train_step(x, t) 238 '''バックプロパゲーションによるパラメーター更新を行う 239 240 Parameters: x: 訓練データ (...) 244 MLPの出力と正解ラベルのクロスエントロピー誤差 245 ''' 246 model.train() # モデルを訓練(学習)モードにする --> 247 preds = model(x) # モデルの出力を取得 248 loss = criterion(preds, t) # 出力と正解ラベルの誤差から損失を取得 249 optimizer.zero_grad() # 勾配を0で初期化(累積してしまうため) in Module._call_impl(self, *input, **kwargs) 1126 # If we don't have any hooks, we want to skip the rest of the logic in 1127 # this function, and just call forward. 1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1129 or _global_forward_hooks or _global_forward_pre_hooks): -> 1130 return forward_call(*input, **kwargs) 1131 # Do not call functions when jit is used 1132 full_backward_hooks, non_full_backward_hooks = [], [] Input In [13], in CNN.forward(self, x) 174 def forward(self, x): 175 '''MLPの順伝播処理を行う 176 177 Parameters: (...) 181 出力層からの出力値 182 ''' --> 183 x = torch.relu(self.conv1(x)) 184 x = self.bn1(x) 185 x = torch.relu(self.conv2(x)) in Module._call_impl(self, *input, **kwargs) 1126 # If we don't have any hooks, we want to skip the rest of the logic in 1127 # this function, and just call forward. 1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks 1129 or _global_forward_hooks or _global_forward_pre_hooks): -> 1130 return forward_call(*input, **kwargs) 1131 # Do not call functions when jit is used 1132 full_backward_hooks, non_full_backward_hooks = [], [] in Conv2d.forward(self, input) 456 def forward(self, input: Tensor) -> Tensor: --> 457 return self._conv_forward(input, self.weight, self.bias) in Conv2d._conv_forward(self, input, weight, bias) 449 if self.padding_mode != 'zeros': 450 return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode), 451 weight, bias, self.stride, 452 _pair(0), self.dilation, self.groups) --> 453 return F.conv2d(input, weight, bias, self.stride, 454 self.padding, self.dilation, self.groups) TypeError: conv2d() received an invalid combination of arguments - got (Tensor, Parameter, Parameter, 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: (Tensor, Parameter, Parameter, 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: (Tensor, Parameter, Parameter, tuple, tuple, tuple, int)
回答2件
あなたの回答
tips
プレビュー