質問編集履歴

2

コードブロックの使用

2019/05/21 08:59

投稿

tubusio
tubusio

スコア11

test CHANGED
File without changes
test CHANGED
@@ -2,316 +2,318 @@
2
2
 
3
3
 
4
4
 
5
+
6
+
7
+ pythonで機械学習のプログラムを作るうえで
8
+
9
+ 一変数ではなく、多変数のデータを読み込んで実行したい
10
+
11
+
12
+
13
+ 一変数でのプログラムは作って実行もできたので、そこに何かを付け足せばいいのか
14
+
15
+ または新しく作り直さなければならないのかを知りたく、またそうするにはどうすればいいかを教えてほしいです
16
+
17
+
18
+
19
+
20
+
21
+ ### 該当のソースコード
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+ ```python
30
+
31
+ import tensorflow as tf
32
+
33
+ import numpy as np
34
+
35
+ import random
36
+
37
+
38
+
39
+
40
+
41
+ num_of_input_nodes = 1
42
+
43
+ num_of_hidden_nodes = 80
44
+
45
+ num_of_output_nodes = 1
46
+
47
+ length_of_sequences = 10 ← 学習・識別時の時系列データ数(area.txtとarea2.txtの列数)
48
+
49
+ num_of_training_epochs = 5000 ← 学習回数
50
+
51
+ size_of_mini_batch = 10
52
+
53
+ num_of_prediction_epochs = 107 ← 識別ファイル(area2.txtとseikai2.txtの行数)
54
+
55
+ learning_rate = 0.01
56
+
57
+ forget_bias = 0.8
58
+
59
+ num_of_sample = 109 ← 学習ファイル(area.txtとseikai.txtの行数)
60
+
61
+
62
+
63
+
64
+
65
+ def get_batch(batch_size, X, t):
66
+
67
+ rnum = [random.randint(0, len(X) - 1) for x in range(batch_size)]
68
+
69
+ xs = np.array([[[y] for y in list(X[r])] for r in rnum])
70
+
71
+ ts = np.array([[t[r]] for r in rnum])
72
+
73
+ return xs, ts
74
+
75
+
76
+
77
+
78
+
79
+ def create_data(nb_of_samples, sequence_len):
80
+
81
+ X = np.loadtxt('area.txt') ← 学習データファイルの読込み
82
+
83
+ t = np.loadtxt('seikai.txt') ← 学習正解値ファイルの読込み
84
+
85
+ print(X)
86
+
87
+ print(t)
88
+
89
+ return X, t
90
+
91
+
92
+
93
+
94
+
95
+ def make_prediction(nb_of_samples):
96
+
97
+ xs = np.loadtxt('area2.txt') ← 識別データファイルの読込み
98
+
99
+ ts = np.loadtxt('seikai2.txt') ← 識別正解値データファイルの読込み(正解率計算用。実際の使用時は正解値は使用しない)
100
+
101
+ return np.array([[[y] for y in x] for x in xs]), np.array([[x] for x in ts])
102
+
103
+
104
+
105
+
106
+
107
+ def inference(input_ph, istate_ph):
108
+
109
+ with tf.name_scope("inference") as scope:
110
+
111
+ weight1_var = tf.Variable(tf.truncated_normal(
112
+
113
+ [num_of_input_nodes, num_of_hidden_nodes], stddev=0.1), name="weight1")
114
+
115
+ weight2_var = tf.Variable(tf.truncated_normal(
116
+
117
+ [num_of_hidden_nodes, num_of_output_nodes], stddev=0.1), name="weight2")
118
+
119
+ bias1_var = tf.Variable(tf.truncated_normal([num_of_hidden_nodes], stddev=0.1), name="bias1")
120
+
121
+ bias2_var = tf.Variable(tf.truncated_normal([num_of_output_nodes], stddev=0.1), name="bias2")
122
+
123
+
124
+
125
+ in1 = tf.transpose(input_ph, [1, 0, 2])
126
+
127
+ in2 = tf.reshape(in1, [-1, num_of_input_nodes])
128
+
129
+ in3 = tf.matmul(in2, weight1_var) + bias1_var
130
+
131
+ in4 = tf.split(in3, length_of_sequences, 0)
132
+
133
+
134
+
135
+ cell = tf.nn.rnn_cell.BasicLSTMCell(num_of_hidden_nodes, forget_bias=forget_bias, state_is_tuple=False)
136
+
137
+ rnn_output, states_op = tf.contrib.rnn.static_rnn(cell, in4, initial_state=istate_ph)
138
+
139
+ output_op = tf.matmul(rnn_output[-1], weight2_var) + bias2_var
140
+
141
+
142
+
143
+ # Add summary ops to collect data
144
+
145
+ w1_hist = tf.summary.histogram("weights1", weight1_var)
146
+
147
+ w2_hist = tf.summary.histogram("weights2", weight2_var)
148
+
149
+ b1_hist = tf.summary.histogram("biases1", bias1_var)
150
+
151
+ b2_hist = tf.summary.histogram("biases2", bias2_var)
152
+
153
+ output_hist = tf.summary.histogram("output", output_op)
154
+
155
+ results = [weight1_var, weight2_var, bias1_var, bias2_var]
156
+
157
+ return output_op, states_op, results
158
+
159
+
160
+
161
+
162
+
163
+ def loss(output_op, supervisor_ph):
164
+
165
+ with tf.name_scope("loss") as scope:
166
+
167
+ square_error = tf.reduce_mean(tf.square(output_op - supervisor_ph))
168
+
169
+ loss_op = square_error
170
+
171
+ tf.summary.scalar("loss", loss_op)
172
+
173
+ return loss_op
174
+
175
+
176
+
177
+
178
+
179
+ def training(loss_op):
180
+
181
+ with tf.name_scope("training") as scope:
182
+
183
+ training_op = optimizer.minimize(loss_op)
184
+
185
+ return training_op
186
+
187
+
188
+
189
+
190
+
191
+ def calc_accuracy(output_op, prints=False):
192
+
193
+ inputs, ts = make_prediction(num_of_prediction_epochs)
194
+
195
+ pred_dict = {
196
+
197
+ input_ph: inputs,
198
+
199
+ supervisor_ph: ts,
200
+
201
+ istate_ph: np.zeros((num_of_prediction_epochs, num_of_hidden_nodes * 2)),
202
+
203
+ }
204
+
205
+ output = sess.run([output_op], feed_dict=pred_dict)
206
+
207
+
208
+
209
+ def print_result(i, p, q):
210
+
211
+ [print(list(x)[0]) for x in i]
212
+
213
+ print("output: %f, correct: %d" % (p, q))
214
+
215
+ if prints:
216
+
217
+ [print_result(i, p, q) for i, p, q in zip(inputs, output[0], ts)]
218
+
219
+
220
+
221
+ opt = abs(output - ts)[0]
222
+
223
+ total = sum([1 if x[0] < 0.5 else 0 for x in opt]) ← 0.05から0.5に変更
224
+
225
+ print("accuracy %f" % (total / float(len(ts))))
226
+
227
+ return output
228
+
229
+
230
+
231
+ random.seed(0)
232
+
233
+ np.random.seed(0)
234
+
235
+ tf.set_random_seed(0)
236
+
237
+
238
+
239
+ optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
240
+
241
+
242
+
243
+ X, t = create_data(num_of_sample, length_of_sequences)
244
+
245
+
246
+
247
+ with tf.Graph().as_default():
248
+
249
+ input_ph = tf.placeholder(tf.float32, [None, length_of_sequences, num_of_input_nodes], name="input")
250
+
251
+ supervisor_ph = tf.placeholder(tf.float32, [None, num_of_output_nodes], name="supervisor")
252
+
253
+ istate_ph = tf.placeholder(tf.float32, [None, num_of_hidden_nodes * 2], name="istate")
254
+
255
+
256
+
257
+ output_op, states_op, datas_op = inference(input_ph, istate_ph)
258
+
259
+ loss_op = loss(output_op, supervisor_ph)
260
+
261
+ training_op = training(loss_op)
262
+
263
+
264
+
265
+ summary_op = tf.summary.merge_all()
266
+
267
+ init = tf.initialize_all_variables()
268
+
269
+
270
+
271
+ with tf.Session() as sess:
272
+
273
+ saver = tf.train.Saver()
274
+
275
+ summary_writer = tf.summary.FileWriter("/tmp/tensorflow_log", graph=sess.graph)
276
+
277
+ sess.run(init)
278
+
279
+
280
+
281
+ for epoch in range(num_of_training_epochs):
282
+
283
+ inputs, supervisors = get_batch(size_of_mini_batch, X, t)
284
+
285
+ train_dict = {
286
+
287
+ input_ph: inputs,
288
+
289
+ supervisor_ph: supervisors,
290
+
291
+ istate_ph: np.zeros((size_of_mini_batch, num_of_hidden_nodes * 2)),
292
+
293
+ }
294
+
295
+ sess.run(training_op, feed_dict=train_dict)
296
+
297
+
298
+
299
+ if (epoch) % 100 == 0:
300
+
301
+ summary_str, train_loss = sess.run([summary_op, loss_op], feed_dict=train_dict)
302
+
303
+ print("train#%d, train loss: %e" % (epoch, train_loss))
304
+
305
+ summary_writer.add_summary(summary_str, epoch)
306
+
307
+ if (epoch) % 500 == 0:
308
+
309
+ calc_accuracy(output_op)
310
+
311
+
312
+
313
+ calc_accuracy(output_op, prints=True)
314
+
315
+ datas = sess.run(datas_op)
316
+
317
+ saver.save(sess, "model.ckpt")
318
+
5
319
  ```
6
-
7
- python
8
-
9
- ```で機械学習のプログラムを作るうえで
10
-
11
- 一変数ではなく、多変数のデータを読み込んで実行したい
12
-
13
-
14
-
15
- 一変数でのプログラムは作って実行もできたので、そこに何かを付け足せばいいのか
16
-
17
- または新しく作り直さなければならないのかを知りたく、またそうするにはどうすればいいかを教えてほしいです
18
-
19
-
20
-
21
-
22
-
23
- ### 該当のソースコード
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
- import tensorflow as tf
32
-
33
- import numpy as np
34
-
35
- import random
36
-
37
-
38
-
39
-
40
-
41
- num_of_input_nodes = 1
42
-
43
- num_of_hidden_nodes = 80
44
-
45
- num_of_output_nodes = 1
46
-
47
- length_of_sequences = 10 ← 学習・識別時の時系列データ数(area.txtとarea2.txtの列数)
48
-
49
- num_of_training_epochs = 5000 ← 学習回数
50
-
51
- size_of_mini_batch = 10
52
-
53
- num_of_prediction_epochs = 107 ← 識別ファイル(area2.txtとseikai2.txtの行数)
54
-
55
- learning_rate = 0.01
56
-
57
- forget_bias = 0.8
58
-
59
- num_of_sample = 109 ← 学習ファイル(area.txtとseikai.txtの行数)
60
-
61
-
62
-
63
-
64
-
65
- def get_batch(batch_size, X, t):
66
-
67
- rnum = [random.randint(0, len(X) - 1) for x in range(batch_size)]
68
-
69
- xs = np.array([[[y] for y in list(X[r])] for r in rnum])
70
-
71
- ts = np.array([[t[r]] for r in rnum])
72
-
73
- return xs, ts
74
-
75
-
76
-
77
-
78
-
79
- def create_data(nb_of_samples, sequence_len):
80
-
81
- X = np.loadtxt('area.txt') ← 学習データファイルの読込み
82
-
83
- t = np.loadtxt('seikai.txt') ← 学習正解値ファイルの読込み
84
-
85
- print(X)
86
-
87
- print(t)
88
-
89
- return X, t
90
-
91
-
92
-
93
-
94
-
95
- def make_prediction(nb_of_samples):
96
-
97
- xs = np.loadtxt('area2.txt') ← 識別データファイルの読込み
98
-
99
- ts = np.loadtxt('seikai2.txt') ← 識別正解値データファイルの読込み(正解率計算用。実際の使用時は正解値は使用しない)
100
-
101
- return np.array([[[y] for y in x] for x in xs]), np.array([[x] for x in ts])
102
-
103
-
104
-
105
-
106
-
107
- def inference(input_ph, istate_ph):
108
-
109
- with tf.name_scope("inference") as scope:
110
-
111
- weight1_var = tf.Variable(tf.truncated_normal(
112
-
113
- [num_of_input_nodes, num_of_hidden_nodes], stddev=0.1), name="weight1")
114
-
115
- weight2_var = tf.Variable(tf.truncated_normal(
116
-
117
- [num_of_hidden_nodes, num_of_output_nodes], stddev=0.1), name="weight2")
118
-
119
- bias1_var = tf.Variable(tf.truncated_normal([num_of_hidden_nodes], stddev=0.1), name="bias1")
120
-
121
- bias2_var = tf.Variable(tf.truncated_normal([num_of_output_nodes], stddev=0.1), name="bias2")
122
-
123
-
124
-
125
- in1 = tf.transpose(input_ph, [1, 0, 2])
126
-
127
- in2 = tf.reshape(in1, [-1, num_of_input_nodes])
128
-
129
- in3 = tf.matmul(in2, weight1_var) + bias1_var
130
-
131
- in4 = tf.split(in3, length_of_sequences, 0)
132
-
133
-
134
-
135
- cell = tf.nn.rnn_cell.BasicLSTMCell(num_of_hidden_nodes, forget_bias=forget_bias, state_is_tuple=False)
136
-
137
- rnn_output, states_op = tf.contrib.rnn.static_rnn(cell, in4, initial_state=istate_ph)
138
-
139
- output_op = tf.matmul(rnn_output[-1], weight2_var) + bias2_var
140
-
141
-
142
-
143
- # Add summary ops to collect data
144
-
145
- w1_hist = tf.summary.histogram("weights1", weight1_var)
146
-
147
- w2_hist = tf.summary.histogram("weights2", weight2_var)
148
-
149
- b1_hist = tf.summary.histogram("biases1", bias1_var)
150
-
151
- b2_hist = tf.summary.histogram("biases2", bias2_var)
152
-
153
- output_hist = tf.summary.histogram("output", output_op)
154
-
155
- results = [weight1_var, weight2_var, bias1_var, bias2_var]
156
-
157
- return output_op, states_op, results
158
-
159
-
160
-
161
-
162
-
163
- def loss(output_op, supervisor_ph):
164
-
165
- with tf.name_scope("loss") as scope:
166
-
167
- square_error = tf.reduce_mean(tf.square(output_op - supervisor_ph))
168
-
169
- loss_op = square_error
170
-
171
- tf.summary.scalar("loss", loss_op)
172
-
173
- return loss_op
174
-
175
-
176
-
177
-
178
-
179
- def training(loss_op):
180
-
181
- with tf.name_scope("training") as scope:
182
-
183
- training_op = optimizer.minimize(loss_op)
184
-
185
- return training_op
186
-
187
-
188
-
189
-
190
-
191
- def calc_accuracy(output_op, prints=False):
192
-
193
- inputs, ts = make_prediction(num_of_prediction_epochs)
194
-
195
- pred_dict = {
196
-
197
- input_ph: inputs,
198
-
199
- supervisor_ph: ts,
200
-
201
- istate_ph: np.zeros((num_of_prediction_epochs, num_of_hidden_nodes * 2)),
202
-
203
- }
204
-
205
- output = sess.run([output_op], feed_dict=pred_dict)
206
-
207
-
208
-
209
- def print_result(i, p, q):
210
-
211
- [print(list(x)[0]) for x in i]
212
-
213
- print("output: %f, correct: %d" % (p, q))
214
-
215
- if prints:
216
-
217
- [print_result(i, p, q) for i, p, q in zip(inputs, output[0], ts)]
218
-
219
-
220
-
221
- opt = abs(output - ts)[0]
222
-
223
- total = sum([1 if x[0] < 0.5 else 0 for x in opt]) ← 0.05から0.5に変更
224
-
225
- print("accuracy %f" % (total / float(len(ts))))
226
-
227
- return output
228
-
229
-
230
-
231
- random.seed(0)
232
-
233
- np.random.seed(0)
234
-
235
- tf.set_random_seed(0)
236
-
237
-
238
-
239
- optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
240
-
241
-
242
-
243
- X, t = create_data(num_of_sample, length_of_sequences)
244
-
245
-
246
-
247
- with tf.Graph().as_default():
248
-
249
- input_ph = tf.placeholder(tf.float32, [None, length_of_sequences, num_of_input_nodes], name="input")
250
-
251
- supervisor_ph = tf.placeholder(tf.float32, [None, num_of_output_nodes], name="supervisor")
252
-
253
- istate_ph = tf.placeholder(tf.float32, [None, num_of_hidden_nodes * 2], name="istate")
254
-
255
-
256
-
257
- output_op, states_op, datas_op = inference(input_ph, istate_ph)
258
-
259
- loss_op = loss(output_op, supervisor_ph)
260
-
261
- training_op = training(loss_op)
262
-
263
-
264
-
265
- summary_op = tf.summary.merge_all()
266
-
267
- init = tf.initialize_all_variables()
268
-
269
-
270
-
271
- with tf.Session() as sess:
272
-
273
- saver = tf.train.Saver()
274
-
275
- summary_writer = tf.summary.FileWriter("/tmp/tensorflow_log", graph=sess.graph)
276
-
277
- sess.run(init)
278
-
279
-
280
-
281
- for epoch in range(num_of_training_epochs):
282
-
283
- inputs, supervisors = get_batch(size_of_mini_batch, X, t)
284
-
285
- train_dict = {
286
-
287
- input_ph: inputs,
288
-
289
- supervisor_ph: supervisors,
290
-
291
- istate_ph: np.zeros((size_of_mini_batch, num_of_hidden_nodes * 2)),
292
-
293
- }
294
-
295
- sess.run(training_op, feed_dict=train_dict)
296
-
297
-
298
-
299
- if (epoch) % 100 == 0:
300
-
301
- summary_str, train_loss = sess.run([summary_op, loss_op], feed_dict=train_dict)
302
-
303
- print("train#%d, train loss: %e" % (epoch, train_loss))
304
-
305
- summary_writer.add_summary(summary_str, epoch)
306
-
307
- if (epoch) % 500 == 0:
308
-
309
- calc_accuracy(output_op)
310
-
311
-
312
-
313
- calc_accuracy(output_op, prints=True)
314
-
315
- datas = sess.run(datas_op)
316
-
317
- saver.save(sess, "model.ckpt")

1

コードブロックの使用

2019/05/21 08:59

投稿

tubusio
tubusio

スコア11

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
 
4
4
 
5
+ ```
6
+
7
+ python
8
+
5
- pythonで機械学習のプログラムを作るうえで
9
+ ```で機械学習のプログラムを作るうえで
6
10
 
7
11
  一変数ではなく、多変数のデータを読み込んで実行したい
8
12