質問編集履歴

4

補足追記

2021/04/15 08:26

投稿

mikan_professor
mikan_professor

スコア28

test CHANGED
File without changes
test CHANGED
@@ -451,3 +451,87 @@
451
451
 
452
452
 
453
453
  modelはGPUに乗せており、printでも確認できました。このエラーの理由も教えていただきたいです。
454
+
455
+
456
+
457
+
458
+
459
+ -----------補足追加----------------
460
+
461
+
462
+
463
+ 以下が全体のコードです。
464
+
465
+ ModuleListからリストに変え、ZFnetを使ったものです。
466
+
467
+
468
+
469
+ ```Python
470
+
471
+ import 同様
472
+
473
+
474
+
475
+
476
+
477
+ class AlexNet(nn.Module):
478
+
479
+
480
+
481
+ //最初のコードと同様。文字数制限にて省略
482
+
483
+ //self.layersをModuleListからListに変更
484
+
485
+
486
+
487
+
488
+
489
+ self.layers = [
490
+
491
+ self.layer1,
492
+
493
+ self.layer2,
494
+
495
+ self.layer3,
496
+
497
+ self.layer4,
498
+
499
+ self.layer5,
500
+
501
+ self.layer6,
502
+
503
+ self.layer7,
504
+
505
+ self.layer8,
506
+
507
+ ]
508
+
509
+
510
+
511
+ def forward(self, x):
512
+
513
+ 同様
514
+
515
+
516
+
517
+
518
+
519
+ class ZFNet(AlexNet):
520
+
521
+ 同様
522
+
523
+
524
+
525
+
526
+
527
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
528
+
529
+ net = ZFNet()
530
+
531
+ net = net.to(device)
532
+
533
+
534
+
535
+ torchsummary.summary(net, input_size=(3, 224, 224))
536
+
537
+ ```

3

エラー詳細追記

2021/04/15 08:26

投稿

mikan_professor
mikan_professor

スコア28

test CHANGED
File without changes
test CHANGED
@@ -406,6 +406,44 @@
406
406
 
407
407
  ```bash
408
408
 
409
+ Traceback (most recent call last):
410
+
411
+ File "/home/〇〇/workspace/machineLearning/popularModel/alexNet.py", line 824, in <module>
412
+
413
+ summary(net, input_size=(3, 224, 224))
414
+
415
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torchsummary/torchsummary.py", line 72, in summary
416
+
417
+ model(*x)
418
+
419
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
420
+
421
+ result = self.forward(*input, **kwargs)
422
+
423
+ File "/home/〇〇/workspace/machineLearning/popularModel/alexNet.py", line 161, in forward
424
+
425
+ x = layer(x)
426
+
427
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
428
+
429
+ result = self.forward(*input, **kwargs)
430
+
431
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/container.py", line 119, in forward
432
+
433
+ input = module(input)
434
+
435
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
436
+
437
+ result = self.forward(*input, **kwargs)
438
+
439
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 399, in forward
440
+
441
+ return self._conv_forward(input, self.weight, self.bias)
442
+
443
+ File "/home/〇〇/.local/share/virtualenvs/machineLearning-WvLHzLrB/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 395, in _conv_forward
444
+
445
+ return F.conv2d(input, weight, bias, self.stride,
446
+
409
447
  RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
410
448
 
411
449
  ```

2

補足

2021/04/08 10:31

投稿

mikan_professor
mikan_professor

スコア28

test CHANGED
File without changes
test CHANGED
@@ -228,7 +228,7 @@
228
228
 
229
229
 
230
230
 
231
- ```zsh
231
+ ```bash
232
232
 
233
233
  ----------------------------------------------------------------
234
234
 
@@ -353,3 +353,63 @@
353
353
  ----------------------------------------------------------------
354
354
 
355
355
  ```
356
+
357
+
358
+
359
+
360
+
361
+
362
+
363
+ =================補足====================
364
+
365
+ nn.ModuleListを通常のリストとした場合は正しく動いたのですが、以下のように新しいモデルを継承により作った場合に、エラーが発生しました。
366
+
367
+
368
+
369
+ ```Python
370
+
371
+ class ZFNet(AlexNet):
372
+
373
+ def __init__(self, conv_channels_list=[96, 256, 384, 384, 256]):
374
+
375
+ super(ZFNet, self).__init__()
376
+
377
+ self.layer1 = nn.Sequential(
378
+
379
+ nn.Conv2d(
380
+
381
+ in_channels=3,
382
+
383
+ out_channels=conv_channels_list[0],
384
+
385
+ kernel_size=7,
386
+
387
+ stride=2,
388
+
389
+ padding=2
390
+
391
+ ),
392
+
393
+ nn.MaxPool2d(kernel_size=3, stride=2),
394
+
395
+ nn.LocalResponseNorm(size=5, k=2),
396
+
397
+ nn.ReLU(inplace=True)
398
+
399
+ )
400
+
401
+
402
+
403
+ ```
404
+
405
+
406
+
407
+ ```bash
408
+
409
+ RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
410
+
411
+ ```
412
+
413
+
414
+
415
+ modelはGPUに乗せており、printでも確認できました。このエラーの理由も教えていただきたいです。

1

誤字の訂正

2021/04/07 23:41

投稿

mikan_professor
mikan_professor

スコア28

test CHANGED
File without changes
test CHANGED
@@ -178,7 +178,7 @@
178
178
 
179
179
 
180
180
 
181
- self.conv_layers = nn.ModuleList([
181
+ self.layers = nn.ModuleList([
182
182
 
183
183
  self.layer1,
184
184
 
@@ -202,7 +202,7 @@
202
202
 
203
203
  def forward(self, x):
204
204
 
205
- for layer in self.conv_layers:
205
+ for layer in self.layers:
206
206
 
207
207
  x = layer(x)
208
208