質問編集履歴

2

モデルをクラスにしたところ新たな問題が出ました

2020/10/21 06:28

投稿

vurie
vurie

スコア0

test CHANGED
File without changes
test CHANGED
@@ -1,18 +1,16 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 分類を行う人工知能を作成たく、kerasを使っ構築をしていますが、現在以下ようなエラーが生しています
3
+ 分類を行う人工知能の出力時系列データとして扱う人工知能モデル
4
4
 
5
5
 
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
8
 
9
- batch_tの方dense_3_に投げてエラーが図れているように思うのでが、原因がわかりません
9
+ モデル連結し学習を行おうとすると下記のようなエラーが出ま
10
-
11
- どなたか、ご助力お願いいたします
12
10
 
13
11
  ```
14
12
 
15
- ValueError: Error when checking target: expected dense_3_ to have 2 dimensions, but got array with shape (40, 200, 10)
13
+ ValueError: Graph disconnected: cannot obtain value for tensor Tensor("inputs_2_:0", shape=(None, 4, 10), dtype=float32) at layer "inputs_2_". The following previous layers were accessed without issue: []
16
14
 
17
15
  ```
18
16
 
@@ -22,11 +20,183 @@
22
20
 
23
21
 
24
22
 
23
+ ```constmodel
24
+
25
+ from keras.layers import Input, Dense, LSTM, Concatenate, Activation
26
+
27
+ from keras.models import Model
28
+
29
+
30
+
31
+
32
+
33
+ class Mymodel(Model):
34
+
35
+ def __init__(self, hidden_finger_size, output_finger_size, output_hand_size, hidden_LSTM_size):
36
+
37
+ self.hidden_fing = hidden_finger_size
38
+
39
+ self.output_fing = output_finger_size
40
+
41
+ self.output_hand = output_hand_size
42
+
43
+ self.hidden_lstm = hidden_LSTM_size
44
+
45
+
46
+
47
+ def static_model(self, inputs):
48
+
49
+ #input
50
+
51
+ inputs[0] = Input(shape=(12, ), name='inputs_0')
52
+
53
+ inputs[1] = Input(shape=(15, ), name='inputs_1')
54
+
55
+ inputs[2] = Input(shape=(15, ), name='inputs_2')
56
+
57
+ inputs[3] = Input(shape=(15, ), name='inputs_3')
58
+
59
+ inputs[4] = Input(shape=(15, ), name='inputs_4')
60
+
61
+
62
+
63
+ #dense_1
64
+
65
+ dense_1_0 = Dense(self.hidden_fing, name='dense_1_0')(inputs[0])
66
+
67
+ dense_1_1 = Dense(self.hidden_fing, name='dense_1_1')(inputs[1])
68
+
69
+ dense_1_2 = Dense(self.hidden_fing, name='dense_1_2')(inputs[2])
70
+
71
+ dense_1_3 = Dense(self.hidden_fing, name='dense_1_3')(inputs[3])
72
+
73
+ dense_1_4 = Dense(self.hidden_fing, name='dense_1_4')(inputs[4])
74
+
75
+
76
+
77
+ #activate_1
78
+
79
+ activation_1_0 = Activation(activation='sigmoid', name='activation_1_0')(dense_1_0)
80
+
81
+ activation_1_1 = Activation(activation='sigmoid', name='activation_1_1')(dense_1_1)
82
+
83
+ activation_1_2 = Activation(activation='sigmoid', name='activation_1_2')(dense_1_2)
84
+
85
+ activation_1_3 = Activation(activation='sigmoid', name='activation_1_3')(dense_1_3)
86
+
87
+ activation_1_4 = Activation(activation='sigmoid', name='activation_1_4')(dense_1_4)
88
+
89
+
90
+
91
+ #dense_2
92
+
93
+ dense_2_0 = Dense(self.output_fing, name='dense_2_0')(activation_1_0)
94
+
95
+ dense_2_1 = Dense(self.output_fing, name='dense_2_1')(activation_1_1)
96
+
97
+ dense_2_2 = Dense(self.output_fing, name='dense_2_2')(activation_1_2)
98
+
99
+ dense_2_3 = Dense(self.output_fing, name='dense_2_3')(activation_1_3)
100
+
101
+ dense_2_4 = Dense(self.output_fing, name='dense_2_4')(activation_1_4)
102
+
103
+
104
+
105
+ #activate_2
106
+
107
+ activation_2_0 = Activation(activation='sigmoid', name='activation_2_0')(dense_2_0)
108
+
109
+ activation_2_1 = Activation(activation='sigmoid', name='activation_2_1')(dense_2_1)
110
+
111
+ activation_2_2 = Activation(activation='sigmoid', name='activation_2_2')(dense_2_2)
112
+
113
+ activation_2_3 = Activation(activation='sigmoid', name='activation_2_3')(dense_2_3)
114
+
115
+ activation_2_4 = Activation(activation='sigmoid', name='activation_2_4')(dense_2_4)
116
+
117
+
118
+
119
+ #concatenate
120
+
121
+ concatenate = Concatenate()([activation_2_0, activation_2_1, activation_2_2, activation_2_3, activation_2_4])
122
+
123
+
124
+
125
+ #dense_3
126
+
127
+ dense_3_ = Dense(self.output_hand, name='dense_3_')(concatenate)
128
+
129
+ activation_3_ = Activation(activation='sigmoid', name='activation_3_')(dense_3_)
130
+
131
+
132
+
133
+ return activation_3_
134
+
135
+
136
+
137
+ def dynamic_model(self, inputs):
138
+
139
+ inputs_2_ = Input(shape=(4, 10, ), name='inputs_2_')
140
+
141
+
142
+
143
+ lstm_0 = LSTM(self.hidden_lstm, activation='sigmoid', name='lstm_0')(inputs_2_)
144
+
145
+ dense_4_ = Dense(self.output_hand, name='dense_4_')(lstm_0)
146
+
147
+ activation_4_ = Activation(activation='sigmoid', name='activation_4_')(dense_4_)
148
+
149
+
150
+
151
+ return activation_4_
152
+
153
+
154
+
155
+ ```
156
+
157
+
158
+
25
- ```python
159
+ ```train
160
+
161
+ import sys, time, csv, os
162
+
163
+
164
+
165
+ my_path = ".."
166
+
167
+ sys.path.append(my_path)
168
+
169
+
170
+
171
+ from my_dataset.load_hand import load_hand_data
172
+
173
+ from Myfunction import Myfunction
174
+
175
+ from const_model import Mymodel
176
+
177
+
178
+
179
+ from keras.layers import Input, Dense, LSTM, Flatten, Concatenate, Activation
180
+
181
+ from keras.models import Model
182
+
183
+ from keras.utils import plot_model
184
+
185
+
186
+
187
+ import matplotlib.pyplot as plt
188
+
189
+ import numpy as np
190
+
191
+ import tensorflow as tf
192
+
193
+
194
+
195
+
26
196
 
27
197
  I = [12, 15, 15, 15, 15]
28
198
 
29
- epochs = 100
199
+ epochs = 1
30
200
 
31
201
  batch_size = 40
32
202
 
@@ -38,7 +208,7 @@
38
208
 
39
209
  hidden_finger_size = 10 #指の中間層サイズ
40
210
 
41
- output_finger_size = 8 #指の出力層サイズ
211
+ output_finger_size = 5 #指の出力層サイズ
42
212
 
43
213
  output_hand_size = 10 #手の出力層サイズ
44
214
 
@@ -46,161 +216,75 @@
46
216
 
47
217
 
48
218
 
219
+ mymodel = Mymodel(hidden_finger_size, output_finger_size, output_hand_size, hidden_LSTM_size)
220
+
49
221
  func = Myfunction(batch_size)
50
222
 
51
223
 
52
224
 
225
+
226
+
53
227
  train_x, train_t, test_x, test_t = load_hand_data(["a.txt", "i.txt", "u.txt", "e.txt", "ku.txt", "se.txt", "so.txt", "ma.txt", "ru.txt", "ya.txt"],
54
228
 
55
229
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
56
230
 
57
-
58
-
59
- #reshape_train_data_to_batch_data
231
+ #reshape_train_data_to_batch
60
-
232
+
61
- batch_x, batch_t = func.get_batch(train_x, train_t)
233
+ tr_batch_x, tr_batch_t = func.get_batch(train_x, train_t)
62
-
234
+
63
- #batch_x = (40, 200, 72)
235
+ te_batch_x, te_batch_t = func.get_test_batch(test_x, test_t)
64
-
65
- #batch_t = (40, 200, 10)
66
236
 
67
237
 
68
238
 
69
239
  #train_data
70
240
 
71
- h_1_0 = batch_x[:, :, :I[0]] #親指
241
+ h_1_0 = tr_batch_x[:, :I[0]] #親指
72
-
242
+
73
- h_1_1 = batch_x[:, :, I[0]:I[0]+I[1]] #人差し指
243
+ h_1_1 = tr_batch_x[:, I[0]:I[0]+I[1]] #人差し指
74
-
244
+
75
- h_1_2 = batch_x[:, :, I[0]+I[1]:I[0]+I[1]+I[2]] #中指
245
+ h_1_2 = tr_batch_x[:, I[0]+I[1]:I[0]+I[1]+I[2]] #中指
76
-
246
+
77
- h_1_3 = batch_x[:, :, I[0]+I[1]+I[2]:I[0]+I[1]+I[2]+I[3]] #薬指
247
+ h_1_3 = tr_batch_x[:, I[0]+I[1]+I[2]:I[0]+I[1]+I[2]+I[3]] #薬指
78
-
248
+
79
- h_1_4 = batch_x[:, :, I[0]+I[1]+I[2]+I[3]:] #小指
249
+ h_1_4 = tr_batch_x[:, I[0]+I[1]+I[2]+I[3]:] #小指
80
-
81
-
82
250
 
83
251
  #test_data
84
252
 
85
- h_2_0 = test_x[:, :I[0]] #親指
253
+ h_2_0 = te_batch_x[:, :I[0]] #親指
86
-
254
+
87
- h_2_1 = test_x[:, I[0]:I[0]+I[1]] #人差し指
255
+ h_2_1 = te_batch_x[:, I[0]:I[0]+I[1]] #人差し指
88
-
256
+
89
- h_2_2 = test_x[:, I[0]+I[1]:I[0]+I[1]+I[2]] #中指
257
+ h_2_2 = te_batch_x[:, I[0]+I[1]:I[0]+I[1]+I[2]] #中指
90
-
258
+
91
- h_2_3 = test_x[:, I[0]+I[1]+I[2]:I[0]+I[1]+I[2]+I[3]] #薬指
259
+ h_2_3 = te_batch_x[:, I[0]+I[1]+I[2]:I[0]+I[1]+I[2]+I[3]] #薬指
92
-
260
+
93
- h_2_4 = test_x[:, I[0]+I[1]+I[2]+I[3]:] #小指
261
+ h_2_4 = te_batch_x[:, I[0]+I[1]+I[2]+I[3]:] #小指
94
-
95
- ################################################################
262
+
96
-
97
- #
263
+
98
-
99
- #
264
+
100
-
101
- #モデル:静的モデル
265
+
102
-
103
- #
104
-
105
- #
106
-
107
- ################################################################
108
266
 
109
267
  #input
110
268
 
111
- inputs_0 = Input(shape=(func.batch_frame, 12), name='inputs_0')
269
+ inputs_0 = Input(shape=(12, ), name='inputs_0')
112
-
270
+
113
- inputs_1 = Input(shape=(func.batch_frame, 15), name='inputs_1')
271
+ inputs_1 = Input(shape=(15, ), name='inputs_1')
114
-
272
+
115
- inputs_2 = Input(shape=(func.batch_frame, 15), name='inputs_2')
273
+ inputs_2 = Input(shape=(15, ), name='inputs_2')
116
-
274
+
117
- inputs_3 = Input(shape=(func.batch_frame, 15), name='inputs_3')
275
+ inputs_3 = Input(shape=(15, ), name='inputs_3')
118
-
276
+
119
- inputs_4 = Input(shape=(func.batch_frame, 15), name='inputs_4')
277
+ inputs_4 = Input(shape=(15, ), name='inputs_4')
120
-
121
-
122
-
123
- #dense_1
278
+
124
-
125
- dense_1_0 = Dense(hidden_finger_size, name='dense_1_0')(inputs_0)
126
-
127
- dense_1_1 = Dense(hidden_finger_size, name='dense_1_1')(inputs_1)
128
-
129
- dense_1_2 = Dense(hidden_finger_size, name='dense_1_2')(inputs_2)
130
-
131
- dense_1_3 = Dense(hidden_finger_size, name='dense_1_3')(inputs_3)
132
-
133
- dense_1_4 = Dense(hidden_finger_size, name='dense_1_4')(inputs_4)
134
-
135
-
136
-
137
- #activate_1
138
-
139
- activation_1_0 = Activation(activation='sigmoid', name='activation_1_0')(dense_1_0)
140
-
141
- activation_1_1 = Activation(activation='sigmoid', name='activation_1_1')(dense_1_1)
142
-
143
- activation_1_2 = Activation(activation='sigmoid', name='activation_1_2')(dense_1_2)
144
-
145
- activation_1_3 = Activation(activation='sigmoid', name='activation_1_3')(dense_1_3)
146
-
147
- activation_1_4 = Activation(activation='sigmoid', name='activation_1_4')(dense_1_4)
148
-
149
-
150
-
151
- #dense_2
152
-
153
- dense_2_0 = Dense(output_finger_size, name='dense_2_0')(activation_1_0)
154
-
155
- dense_2_1 = Dense(output_finger_size, name='dense_2_1')(activation_1_1)
156
-
157
- dense_2_2 = Dense(output_finger_size, name='dense_2_2')(activation_1_2)
158
-
159
- dense_2_3 = Dense(output_finger_size, name='dense_2_3')(activation_1_3)
160
-
161
- dense_2_4 = Dense(output_finger_size, name='dense_2_4')(activation_1_4)
162
-
163
-
164
-
165
- #activate_2
166
-
167
- activation_2_0 = Activation(activation='sigmoid', name='activation_2_0')(dense_2_0)
168
-
169
- activation_2_1 = Activation(activation='sigmoid', name='activation_2_1')(dense_2_1)
170
-
171
- activation_2_2 = Activation(activation='sigmoid', name='activation_2_2')(dense_2_2)
172
-
173
- activation_2_3 = Activation(activation='sigmoid', name='activation_2_3')(dense_2_3)
174
-
175
- activation_2_4 = Activation(activation='sigmoid', name='activation_2_4')(dense_2_4)
176
-
177
-
178
-
179
- #flatten
180
-
181
- flatten_0 = Flatten()(activation_2_0)
182
-
183
- flatten_1 = Flatten()(activation_2_1)
184
-
185
- flatten_2 = Flatten()(activation_2_2)
186
-
187
- flatten_3 = Flatten()(activation_2_3)
188
-
189
- flatten_4 = Flatten()(activation_2_4)
190
-
191
-
192
-
193
-
194
-
195
- #concatenate
196
-
197
- concatenate = Concatenate()([flatten_0, flatten_1, flatten_2, flatten_3, flatten_4])
279
+ inputs = [inputs_0, inputs_1, inputs_2, inputs_3, inputs_4]
198
-
199
-
200
-
280
+
281
+
282
+
283
+
284
+
201
- #dense_3
285
+ static_outputs = mymodel.static_model(inputs)
202
-
286
+
203
- dense_3_ = Dense(output_hand_size, activation='sigmoid', name='dense_3_')(concatenate)
287
+ dynamic_outputs = mymodel.dynamic_model(static_outputs)
204
288
 
205
289
 
206
290
 
@@ -208,11 +292,7 @@
208
292
 
209
293
  #train
210
294
 
211
- model = Model(inputs=[inputs_0, inputs_1, inputs_2, inputs_3, inputs_4], outputs=dense_3_)
295
+ model = Model(inputs=inputs, outputs=dynamic_outputs)
212
-
213
- model.summary()
214
-
215
-
216
296
 
217
297
  model.compile(optimizer='adam',
218
298
 
@@ -220,7 +300,7 @@
220
300
 
221
301
  metrics=['accuracy'])
222
302
 
223
- history = model.fit([h_1_0, h_1_1, h_1_2, h_1_3, h_1_4], batch_t,
303
+ history = model.fit([h_1_0, h_1_1, h_1_2, h_1_3, h_1_4], tr_batch_t,
224
304
 
225
305
  batch_size=batch_size,
226
306
 
@@ -228,17 +308,21 @@
228
308
 
229
309
  verbose=1,
230
310
 
231
- validation_data=([h_2_0, h_2_1, h_2_2, h_2_3, h_2_4], test_t))
311
+ validation_data=([h_2_0, h_2_1, h_2_2, h_2_3, h_2_4], te_batch_t))
312
+
313
+
314
+
315
+
232
316
 
233
317
  ```
234
318
 
235
-
236
-
237
319
  ### 試したこと
238
320
 
321
+ 前回の投稿から、モデルをクラス化しました
322
+
239
- 同様なエーメッセージにいろいろ調べてみましたが、解決まで至りませんでした
323
+ ス化した一めのモデルの方は学習まで持っ行けのですが、二つ目のモデルの方を繋げて学習を行おうとするとエラーが
324
+
240
-
325
+ 出ました
241
-
242
326
 
243
327
  ### 補足情報(FW/ツールのバージョンなど)
244
328
 

1

エラー部分について追記

2020/10/21 06:28

投稿

vurie
vurie

スコア0

test CHANGED
File without changes
test CHANGED
@@ -1,12 +1,14 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 分類を行う人工知能を作成したく、kerasを使って構築をしています
3
+ 分類を行う人工知能を作成したく、kerasを使って構築をしていますが、現在以下のようなエラーが発生しています
4
4
 
5
5
 
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
8
 
9
-
9
+ batch_tの方をdense_3_に投げてエラーが図れているように思うのですが、原因がわかりません
10
+
11
+ どなたか、ご助力お願いいたします
10
12
 
11
13
  ```
12
14