teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

補足追記

2021/04/15 08:26

投稿

mikan_professor
mikan_professor

スコア28

title CHANGED
File without changes
body CHANGED
@@ -224,4 +224,46 @@
224
224
  RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
225
225
  ```
226
226
 
227
- modelはGPUに乗せており、printでも確認できました。このエラーの理由も教えていただきたいです。
227
+ modelはGPUに乗せており、printでも確認できました。このエラーの理由も教えていただきたいです。
228
+
229
+
230
+ -----------補足追加----------------
231
+
232
+ 以下が全体のコードです。
233
+ ModuleListからリストに変え、ZFnetを使ったものです。
234
+
235
+ ```Python
236
+ import 同様
237
+
238
+
239
+ class AlexNet(nn.Module):
240
+
241
+ //最初のコードと同様。文字数制限にて省略
242
+ //self.layersをModuleListからListに変更
243
+
244
+
245
+ self.layers = [
246
+ self.layer1,
247
+ self.layer2,
248
+ self.layer3,
249
+ self.layer4,
250
+ self.layer5,
251
+ self.layer6,
252
+ self.layer7,
253
+ self.layer8,
254
+ ]
255
+
256
+ def forward(self, x):
257
+ 同様
258
+
259
+
260
+ class ZFNet(AlexNet):
261
+ 同様
262
+
263
+
264
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
265
+ net = ZFNet()
266
+ net = net.to(device)
267
+
268
+ torchsummary.summary(net, input_size=(3, 224, 224))
269
+ ```

3

エラー詳細追記

2021/04/15 08:26

投稿

mikan_professor
mikan_professor

スコア28

title CHANGED
File without changes
body CHANGED
@@ -202,6 +202,25 @@
202
202
  ```
203
203
 
204
204
  ```bash
205
+ Traceback (most recent call last):
206
+ File "/home/〇〇/workspace/machineLearning/popularModel/alexNet.py", line 824, in <module>
207
+ summary(net, input_size=(3, 224, 224))
208
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torchsummary/torchsummary.py", line 72, in summary
209
+ model(*x)
210
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
211
+ result = self.forward(*input, **kwargs)
212
+ File "/home/〇〇/workspace/machineLearning/popularModel/alexNet.py", line 161, in forward
213
+ x = layer(x)
214
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
215
+ result = self.forward(*input, **kwargs)
216
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/container.py", line 119, in forward
217
+ input = module(input)
218
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
219
+ result = self.forward(*input, **kwargs)
220
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 399, in forward
221
+ return self._conv_forward(input, self.weight, self.bias)
222
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 395, in _conv_forward
223
+ return F.conv2d(input, weight, bias, self.stride,
205
224
  RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
206
225
  ```
207
226
 

2

補足

2021/04/08 10:31

投稿

mikan_professor
mikan_professor

スコア28

title CHANGED
File without changes
body CHANGED
@@ -113,7 +113,7 @@
113
113
  torchsummary.summary(net, input_size=(3, 224, 224))
114
114
  ```
115
115
 
116
- ```zsh
116
+ ```bash
117
117
  ----------------------------------------------------------------
118
118
  Layer (type) Output Shape Param #
119
119
  ================================================================
@@ -175,4 +175,34 @@
175
175
  Params size (MB): 472.91
176
176
  Estimated Total Size (MB): 491.39
177
177
  ----------------------------------------------------------------
178
- ```
178
+ ```
179
+
180
+
181
+
182
+ =================補足====================
183
+ nn.ModuleListを通常のリストとした場合は正しく動いたのですが、以下のように新しいモデルを継承により作った場合に、エラーが発生しました。
184
+
185
+ ```Python
186
+ class ZFNet(AlexNet):
187
+ def __init__(self, conv_channels_list=[96, 256, 384, 384, 256]):
188
+ super(ZFNet, self).__init__()
189
+ self.layer1 = nn.Sequential(
190
+ nn.Conv2d(
191
+ in_channels=3,
192
+ out_channels=conv_channels_list[0],
193
+ kernel_size=7,
194
+ stride=2,
195
+ padding=2
196
+ ),
197
+ nn.MaxPool2d(kernel_size=3, stride=2),
198
+ nn.LocalResponseNorm(size=5, k=2),
199
+ nn.ReLU(inplace=True)
200
+ )
201
+
202
+ ```
203
+
204
+ ```bash
205
+ RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
206
+ ```
207
+
208
+ modelはGPUに乗せており、printでも確認できました。このエラーの理由も教えていただきたいです。

1

誤字の訂正

2021/04/07 23:41

投稿

mikan_professor
mikan_professor

スコア28

title CHANGED
File without changes
body CHANGED
@@ -88,7 +88,7 @@
88
88
  nn.Softmax(dim=1)
89
89
  )
90
90
 
91
- self.conv_layers = nn.ModuleList([
91
+ self.layers = nn.ModuleList([
92
92
  self.layer1,
93
93
  self.layer2,
94
94
  self.layer3,
@@ -100,7 +100,7 @@
100
100
  ])
101
101
 
102
102
  def forward(self, x):
103
- for layer in self.conv_layers:
103
+ for layer in self.layers:
104
104
  x = layer(x)
105
105
  return x
106
106