質問編集履歴

4

コードの入力し直し

2019/01/11 08:03

投稿

miyukiha
miyukiha

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,21 +1,15 @@
1
+ ``````### 前提・実現したいこと
2
+
3
+
4
+
5
+ lstmautoencoderを実装したいのですが、やり方が分からないため教えていただきたいです。
6
+
7
+ 自分が持っているデータをCSVファイルで入力したいのですが、どう入力すればいいか分からないため、教えてください。入力方法は、numpyでもpandasでもどちらでも大丈夫です。
8
+
1
9
  ```
2
10
 
3
11
 
4
12
 
5
-
6
-
7
- ``````### 前提・実現したいこと
8
-
9
-
10
-
11
- lstmautoencoderを実装したいのですが、やり方が分からないため教えていただきたいです。
12
-
13
- 自分が持っているデータをCSVファイルで入力したいのですが、どう入力すればいいか分からないため、教えてください。入力方法は、numpyでもpandasでもどちらでも大丈夫です。
14
-
15
-
16
-
17
-
18
-
19
13
  ```ここに言語を入力
20
14
 
21
15
  import tensorflow as tf

3

コードの入力し直し

2019/01/11 08:03

投稿

miyukiha
miyukiha

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,37 +1,105 @@
1
+ ```
2
+
3
+
4
+
5
+
6
+
7
+ ``````### 前提・実現したいこと
8
+
9
+
10
+
11
+ lstmautoencoderを実装したいのですが、やり方が分からないため教えていただきたいです。
12
+
13
+ 自分が持っているデータをCSVファイルで入力したいのですが、どう入力すればいいか分からないため、教えてください。入力方法は、numpyでもpandasでもどちらでも大丈夫です。
14
+
15
+
16
+
17
+
18
+
1
19
  ```ここに言語を入力
2
20
 
3
-
4
-
5
- ``````### 前提・実現したいこと
6
-
7
-
8
-
9
- lstmautoencoderを実装したいのですが、やり方が分からないため教えていただきたいです。
10
-
11
- 自分が持っているデータをCSVファイルで入力したいのですが、どう入力すればいいか分からないため、教えてください。入力方法は、numpyでもpandasでもどちらでも大丈夫です。
12
-
13
-
14
-
15
-
16
-
17
- ```ここに言語を入力
18
-
19
21
  import tensorflow as tf
20
22
 
21
23
  from tensorflow.python.ops.rnn_cell import LSTMCell
22
24
 
25
+
26
+
23
27
  import numpy as np
24
28
 
25
29
 
26
30
 
31
+
32
+
27
33
  class LSTMAutoencoder(object):
28
34
 
35
+
36
+
37
+ """Basic version of LSTM-autoencoder.
38
+
39
+ (cf. http://arxiv.org/abs/1502.04681)
40
+
41
+
42
+
43
+ Usage:
44
+
45
+ ae = LSTMAutoencoder(hidden_num, inputs)
46
+
47
+ sess.run(ae.train)
48
+
49
+ """
50
+
51
+
52
+
53
+ def __init__(
54
+
55
+ self,
56
+
57
+ hidden_num,
58
+
59
+ inputs,
60
+
61
+ cell=None,
62
+
63
+ optimizer=None,
64
+
65
+ reverse=True,
66
+
67
+ decode_without_input=False,
68
+
69
+ ):
70
+
71
+ """
72
+
73
+ Args:
74
+
75
+ hidden_num : number of hidden elements of each LSTM unit.
76
+
77
+ inputs : a list of input tensors with size
78
+
79
+ (batch_num x elem_num)
80
+
81
+ cell : an rnn cell object (the default option
82
+
83
+ is `tf.python.ops.rnn_cell.LSTMCell`)
84
+
85
+ optimizer : optimizer for rnn (the default option is
86
+
87
+ `tf.train.AdamOptimizer`)
88
+
89
+ reverse : Option to decode in reverse order.
90
+
29
- def __init__(self, hidden_num, inputs, cell=None, optimizer=None, reverse=True, decode_without_input=False):
91
+ decode_without_input : Option to decode without input.
92
+
93
+ """
94
+
95
+
30
96
 
31
97
  self.batch_num = inputs[0].get_shape().as_list()[0]
32
98
 
33
99
  self.elem_num = inputs[0].get_shape().as_list()[1]
34
100
 
101
+
102
+
35
103
  if cell is None:
36
104
 
37
105
  self._enc_cell = LSTMCell(hidden_num)
@@ -44,37 +112,65 @@
44
112
 
45
113
  self._dec_cell = cell
46
114
 
115
+
116
+
47
117
  with tf.variable_scope('encoder'):
48
118
 
49
119
  (self.z_codes, self.enc_state) = tf.contrib.rnn.static_rnn(self._enc_cell, inputs, dtype=tf.float32)
50
120
 
121
+
122
+
51
123
  with tf.variable_scope('decoder') as vs:
52
124
 
53
- dec_weight_ = tf.Variable(tf.truncated_normal([hidden_num, self.elem_num], dtype=tf.float32), name='dec_weight')
125
+ dec_weight_ = tf.Variable(tf.truncated_normal([hidden_num,
54
-
126
+
55
- dec_bias_ = tf.Variable(tf.constant(0.1, shape=[self.elem_num], dtype=tf.float32), name='dec_bias')
127
+ self.elem_num], dtype=tf.float32), name='dec_weight'
128
+
129
+ )
130
+
131
+ dec_bias_ = tf.Variable(tf.constant(0.1,
132
+
133
+ shape=[self.elem_num],
134
+
135
+ dtype=tf.float32), name='dec_bias')
136
+
137
+
56
138
 
57
139
  if decode_without_input:
58
140
 
59
- dec_inputs = [tf.zeros(tf.shape(inputs[0]), dtype=tf.float32) for _ in range(len(inputs))]
141
+ dec_inputs = [tf.zeros(tf.shape(inputs[0]),
142
+
60
-
143
+ dtype=tf.float32) for _ in
144
+
145
+ range(len(inputs))]
146
+
61
- (dec_outputs, dec_state) = tf.contrib.rnn.static_rnn(self._dec_cell, dec_inputs, initial_state=self.enc_state, dtype=tf.float32)
147
+ (dec_outputs, dec_state) = tf.contrib.rnn.static_rnn(self._dec_cell, dec_inputs, initial_state=self.enc_state,
148
+
149
+ dtype=tf.float32)
62
150
 
63
151
  if reverse:
64
152
 
65
153
  dec_outputs = dec_outputs[::-1]
66
154
 
67
- dec_output_ = tf.transpose(tf.stack(dec_outputs), [1, 0, 2])
155
+ dec_output_ = tf.transpose(tf.stack(dec_outputs), [1, 0,
156
+
68
-
157
+ 2])
158
+
69
- dec_weight_ = tf.tile(tf.expand_dims(dec_weight_, 0), [self.batch_num, 1, 1])
159
+ dec_weight_ = tf.tile(tf.expand_dims(dec_weight_, 0),
160
+
161
+ [self.batch_num, 1, 1])
70
162
 
71
163
  self.output_ = tf.matmul(dec_output_, dec_weight_) + dec_bias_
72
164
 
73
165
  else:
74
166
 
167
+
168
+
75
169
  dec_state = self.enc_state
76
170
 
77
- dec_input_ = tf.zeros(tf.shape(inputs[0]), dtype=tf.float32)
171
+ dec_input_ = tf.zeros(tf.shape(inputs[0]),
172
+
173
+ dtype=tf.float32)
78
174
 
79
175
  dec_outputs = []
80
176
 
@@ -84,19 +180,33 @@
84
180
 
85
181
  vs.reuse_variables()
86
182
 
183
+ (dec_input_, dec_state) = \
184
+
87
- (dec_input_, dec_state) = self._dec_cell(dec_input_, dec_state)
185
+ self._dec_cell(dec_input_, dec_state)
88
-
186
+
89
- dec_input_ = tf.matmul(dec_input_, dec_weight_) + dec_bias_dec_outputs.append(dec_input_)
187
+ dec_input_ = tf.matmul(dec_input_, dec_weight_) \
188
+
189
+ + dec_bias_
190
+
191
+ dec_outputs.append(dec_input_)
90
192
 
91
193
  if reverse:
92
194
 
93
195
  dec_outputs = dec_outputs[::-1]
94
196
 
95
- self.output_ = tf.transpose(tf.stack(dec_outputs), [1, 0, 2])
197
+ self.output_ = tf.transpose(tf.stack(dec_outputs), [1,
198
+
199
+ 0, 2])
200
+
201
+
96
202
 
97
203
  self.input_ = tf.transpose(tf.stack(inputs), [1, 0, 2])
98
204
 
99
- self.loss = tf.reduce_mean(tf.square(self.input_ - self.output_))
205
+ self.loss = tf.reduce_mean(tf.square(self.input_
206
+
207
+ - self.output_))
208
+
209
+
100
210
 
101
211
  if optimizer is None:
102
212
 
@@ -108,18 +218,24 @@
108
218
 
109
219
 
110
220
 
111
- import numpy as np
112
-
113
- import tensorflow as tf
114
-
115
- from LSTMAutoencoder import LSTMAutoencoder
116
-
117
221
 
118
222
 
119
223
  tf.reset_default_graph()
120
224
 
121
225
  tf.set_random_seed(2016)
122
226
 
227
+ np.random.seed(2016)
228
+
229
+
230
+
231
+ # LSTM-autoencoder
232
+
233
+
234
+
235
+
236
+
237
+ # Constants
238
+
123
239
  batch_num = 128
124
240
 
125
241
  hidden_num = 12
@@ -130,12 +246,14 @@
130
246
 
131
247
  iteration = 10000
132
248
 
249
+
250
+
251
+ # placeholder list
252
+
133
253
  p_input = tf.placeholder(tf.float32, shape=(batch_num, step_num, elem_num))
134
254
 
135
255
  p_inputs = [tf.squeeze(t, [1]) for t in tf.split(p_input, step_num, 1)]
136
256
 
137
- import ipdb; ipdb.set_trace()
138
-
139
257
 
140
258
 
141
259
  cell = tf.nn.rnn_cell.LSTMCell(hidden_num, use_peepholes=True)
@@ -148,8 +266,22 @@
148
266
 
149
267
  sess.run(tf.global_variables_initializer())
150
268
 
269
+
270
+
151
271
  for i in range(iteration):
152
272
 
273
+ """Random sequences.
274
+
275
+ Every sequence has size batch_num * step_num * elem_num
276
+
277
+ Each step number increases 1 by 1.
278
+
279
+ An initial number of each sequence is in the range from 0 to 19.
280
+
281
+ (ex. [8. 9. 10. 11. 12. 13. 14. 15])
282
+
283
+ """
284
+
153
285
  r = np.random.randint(20, size=batch_num).reshape([batch_num, 1, 1])
154
286
 
155
287
  r = np.tile(r, (1, step_num, elem_num))
@@ -160,13 +292,15 @@
160
292
 
161
293
  random_sequences = r + d
162
294
 
295
+
296
+
163
297
  (loss_val, _) = sess.run([ae.loss, ae.train], {p_input: random_sequences})
164
298
 
165
299
  print('iter %d:' % (i + 1), loss_val)
166
300
 
167
- inputData = r + d
301
+
168
-
302
+
169
- (input_, output_) = sess.run([ae.input_, ae.output_], {p_input: inputData})
303
+ (input_, output_) = sess.run([ae.input_, ae.output_], {p_input: r + d})
170
304
 
171
305
  print('train result :')
172
306
 

2

コードの入力し直し

2019/01/11 07:47

投稿

miyukiha
miyukiha

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,8 @@
1
+ ```ここに言語を入力
2
+
3
+
4
+
1
- ```### 前提・実現したいこと
5
+ ``````### 前提・実現したいこと
2
6
 
3
7
 
4
8
 

1

コードの入力し直し

2019/01/11 07:40

投稿

miyukiha
miyukiha

スコア10

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- ### 前提・実現したいこと
1
+ ```### 前提・実現したいこと
2
2
 
3
3
 
4
4
 
@@ -10,9 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- ### 該当のソースコード
13
+ ```ここに言語を入力
14
-
15
-
16
14
 
17
15
  import tensorflow as tf
18
16
 
@@ -171,3 +169,5 @@
171
169
  print('input :', input_[0, :, :].flatten())
172
170
 
173
171
  print('output :', output_[0, :, :].flatten())
172
+
173
+ ```