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

質問編集履歴

6

コードが見難かったので修正しました。

2021/03/23 03:04

投稿

kei_kei_
kei_kei_

スコア6

title CHANGED
File without changes
body CHANGED
@@ -57,6 +57,7 @@
57
57
  ```
58
58
 
59
59
  <追記>
60
+ ```python
60
61
  class MyChain(chainer.Chain):
61
62
  def init(self):
62
63
  super(MyChain, self).init()
@@ -64,28 +65,36 @@
64
65
  with self.init_scope():
65
66
  self.l1 = L.Linear(None, 3)
66
67
  self.l2 = L.Linear(None, 2)
68
+ ```
67
69
 
68
70
  おそらくこの下の箇所が問題かと思っているのですが、
71
+ ```python
69
72
  def call(self, x):
70
73
  h = F.relu(self.l1(x))
71
74
  h = self.l2(h)
72
75
  return h
76
+ ```
73
77
 
74
78
  ・書き方1
75
-
79
+ ```python
76
80
  def call(self, x):
77
81
  h1 = F.relu(self.l1(x))
78
82
  y = self.l2(h1)
79
83
  return y
84
+ ```
80
85
 
81
86
  ・書き方2
87
+ ```python
82
88
  def call(self, x):
83
89
  h1 = F.relu(self.l1(x))
84
90
  h2 = self.l2(h1)
85
91
  return h2
92
+ ```
86
93
 
87
94
  ・書き方3
95
+ ```python
88
96
  def call(self, x):
89
97
  h = F.relu(self.l1(x))
90
98
  return self.l2(h)
99
+ ```
91
100
  などの書き方をしても解消されない状態です。。

5

コード検証例追加

2021/03/23 03:04

投稿

kei_kei_
kei_kei_

スコア6

title CHANGED
File without changes
body CHANGED
@@ -54,4 +54,38 @@
54
54
  trainer.run()
55
55
 
56
56
  chainer.serializers.save_npz("result/out.model",model)
57
- ```
57
+ ```
58
+
59
+ <追記>
60
+ class MyChain(chainer.Chain):
61
+ def init(self):
62
+ super(MyChain, self).init()
63
+
64
+ with self.init_scope():
65
+ self.l1 = L.Linear(None, 3)
66
+ self.l2 = L.Linear(None, 2)
67
+
68
+ おそらくこの下の箇所が問題かと思っているのですが、
69
+ def call(self, x):
70
+ h = F.relu(self.l1(x))
71
+ h = self.l2(h)
72
+ return h
73
+
74
+ ・書き方1
75
+
76
+ def call(self, x):
77
+ h1 = F.relu(self.l1(x))
78
+ y = self.l2(h1)
79
+ return y
80
+
81
+ ・書き方2
82
+ def call(self, x):
83
+ h1 = F.relu(self.l1(x))
84
+ h2 = self.l2(h1)
85
+ return h2
86
+
87
+ ・書き方3
88
+ def call(self, x):
89
+ h = F.relu(self.l1(x))
90
+ return self.l2(h)
91
+ などの書き方をしても解消されない状態です。。

4

微修正

2021/03/23 02:38

投稿

kei_kei_
kei_kei_

スコア6

title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
  〜コード〜
7
7
  ```python
8
+
8
9
  import numpy as np
9
10
  import chainer
10
11
  import chainer.functions as F
@@ -52,4 +53,5 @@
52
53
 
53
54
  trainer.run()
54
55
 
55
- chainer.serializers.save_npz("result/out.model",model)```
56
+ chainer.serializers.save_npz("result/out.model",model)
57
+ ```

3

微修正

2021/03/23 00:51

投稿

kei_kei_
kei_kei_

スコア6

title CHANGED
File without changes
body CHANGED
@@ -20,6 +20,7 @@
20
20
  with self.init_scope():
21
21
  self.l1 = L.Linear(None, 3)
22
22
  self.l2 = L.Linear(None, 2)
23
+
23
24
  def __call__(self, x):
24
25
  h = F.relu(self.l1(x))
25
26
  h = self.l2(h)

2

微修正

2021/03/23 00:50

投稿

kei_kei_
kei_kei_

スコア6

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,6 @@
5
5
 
6
6
  〜コード〜
7
7
  ```python
8
-
9
8
  import numpy as np
10
9
  import chainer
11
10
  import chainer.functions as F
@@ -14,7 +13,6 @@
14
13
  from chainer import training
15
14
  from chainer.training import extensions
16
15
 
17
- #NNの設定
18
16
  class MyChain(chainer.Chain):
19
17
  def __init__(self):
20
18
  super(MyChain, self).__init__()
@@ -27,41 +25,30 @@
27
25
  h = self.l2(h)
28
26
  return h
29
27
 
30
- #データの設定
31
28
  trainx = np.array(([0,0],[0,1],[1,0],[1,1]),dtype=np.float32)
32
29
  trainy = np.array([0,1,1,0],dtype=np.int32)
33
30
  train = chainer.datasets.TupleDataset(trainx,trainy)
34
31
  test = chainer.datasets.TupleDataset(trainx,trainy)
35
32
 
36
- #chainerの設定
37
- #ニューラルネットの登録
38
33
  model = L.Classifier(MyChain(),lossfun=F.softmax_cross_entropy)
39
34
  optimizer = chainer.optimizers.Adam()
40
35
  optimizer.setup(model)
41
36
 
42
- #イテレータの定義
43
37
  batchsize = 4
44
38
  train_iter = chainer.iterators.SerialIterator(train,batchsize) #学習用
45
39
  test_iter = chainer.iterators.SerialIterator(test,batchsize,repeat=False,shuffle=False) #評価用
46
40
 
47
- #アップデータの登録
48
41
  updater = training.StandardUpdater(train_iter,optimizer)
49
42
 
50
- #トレーナーの登録
51
43
  epoch = 500
52
44
  trainer = training.Trainer(updater,(epoch,'epoch'))
53
45
 
54
- #学習状況の表示や保存
55
46
  trainer.extend(extensions.LogReport()) #ログ
56
47
  trainer.extend(extensions.Evaluator(test_iter,model)) #エポック数の表示
57
48
  trainer.extend(extensions.PrintReport(['epoch','main/loss','validation/main/loss','main/accuracy','validation/main/accuracy','elapsed_time'])) #計算状態の表示
58
49
  trainer.extend(extensions.PlotReport(['main/loss','validation/main/loss'],'epoch',file_name='loss.png')) #誤差グラフ
59
50
  trainer.extend(extensions.PlotReport(['main/accuracy','validation/main/accuracy'],'epoch',file_name='accuracy.png')) #精度グラフ
60
51
 
61
- #学習開始
62
52
  trainer.run()
63
53
 
64
- #モデルの保存
65
- chainer.serializers.save_npz("result/out.model",model)
54
+ chainer.serializers.save_npz("result/out.model",model)```
66
-
67
- ```

1

コードが見難かったので修正しました。

2021/03/23 00:47

投稿

kei_kei_
kei_kei_

スコア6

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,8 @@
4
4
 
5
5
 
6
6
  〜コード〜
7
- #上の関数をひとまとめにする
7
+ ```python
8
+
8
9
  import numpy as np
9
10
  import chainer
10
11
  import chainer.functions as F
@@ -61,4 +62,6 @@
61
62
  trainer.run()
62
63
 
63
64
  #モデルの保存
64
- chainer.serializers.save_npz("result/out.model",model)
65
+ chainer.serializers.save_npz("result/out.model",model)
66
+
67
+ ```