質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
PyTorch

PyTorchは、オープンソースのPython向けの機械学習ライブラリ。Facebookの人工知能研究グループが開発を主導しています。強力なGPUサポートを備えたテンソル計算、テープベースの自動微分による柔軟なニューラルネットワークの記述が可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

3305閲覧

Pytorchによる機械学習について

glacier_gg

総合スコア2

PyTorch

PyTorchは、オープンソースのPython向けの機械学習ライブラリ。Facebookの人工知能研究グループが開発を主導しています。強力なGPUサポートを備えたテンソル計算、テープベースの自動微分による柔軟なニューラルネットワークの記述が可能です。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/07/20 01:11

編集2022/07/22 09:20

演習問題で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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/07/20 14:36

TypeError: conv2d() received an invalid combination of arguments - got (略) (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups) --> tuple of ints padding (略) (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups) --> str padding, (モジュールを使う側としては困ってしまいますが)文章が長くてグデグデなデバグ通知メッセージなので確証はないですが、どうもPaddingのフォーマットが違う線があるように思います。
glacier_gg

2022/07/21 00:09

すみませんどのように修正すればよろしいでしょうか paddingは行おうと考えていますが、どう修正しても実行できないのです
guest

回答2

0

ベストアンサー

今見返しました。
コメントの続きです。たぶん解決できるので解答欄に書きます。

起きていること

--> 453 return F.conv2d(input, weight, bias, self.stride,
454 self.padding, self.dilation, self.groups)
ここで、

「 str padding」のフォーマットが欲しいのに「tuple of ints padding」が来ているよ、というエラーですね。(※逆かもしれません)
つまり、padding=Trueがおかしい です。

解決策(手元でうごかしてないですが…)

公式のconv2dのリファレンスをまず参照ください。

CLASS torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
(中略)
padding controls the amount of padding applied to the input. It can be either a string {‘valid’, ‘same’} or a tuple of ints giving the amount of implicit padding applied on both sides.

padding=0とあるので、padding=integerにしないといけないと思います。あとは詳細説明にあるようにpadding="valid"padding="same"にするかです(参考:Kerasでの説明ですが分かりやすいと思います【Python】 KerasのConv2Dの引数paddingについて)。

投稿2022/07/21 21:42

編集2022/07/21 21:43
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

glacier_gg

2022/07/22 00:20

解決できました。ありがとうございます!
guest

0

preds = model(x)の前に
print(x.shape)などを追加して、入力次元とモデルの入力次元があっているか確認してください

投稿2022/07/21 09:28

East_san

総合スコア407

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問