15
15
max_w = 50
16
16
float_size = 4
17
17
word_vector_dict = {}
18
+ word_vec_dim = 200
19
+ max_seq_len = 16
18
20
19
21
def load_vectors (input ):
20
22
"""从vectors.bin加载词向量,返回一个word_vector_dict的词典,key是词,value是200维的向量
@@ -51,8 +53,8 @@ def load_vectors(input):
51
53
vector .append (float (weight ))
52
54
53
55
# 将词及其对应的向量存到dict中
54
- word_vector_dict [word .decode ('utf-8' )] = vector
55
- # word_vector_dict[word.decode('utf-8')] = vector[0:4 ]
56
+ # word_vector_dict[word.decode('utf-8')] = vector
57
+ word_vector_dict [word .decode ('utf-8' )] = vector [0 :word_vec_dim ]
56
58
57
59
input_file .close ()
58
60
@@ -110,9 +112,9 @@ class MySeq2Seq(object):
110
112
输出的时候把解码器的输出按照词向量的200维展平,这样输出就是(?,seqlen*200)
111
113
这样就可以通过regression来做回归计算了,输入的y也展平,保持一致
112
114
"""
113
- def __init__ (self , max_seq_len = 16 ):
115
+ def __init__ (self , max_seq_len = 16 , word_vec_dim = 200 ):
114
116
self .max_seq_len = max_seq_len
115
- self .word_vec_dim = 200
117
+ self .word_vec_dim = word_vec_dim
116
118
117
119
def generate_trainig_data (self ):
118
120
load_vectors ("./vectors.bin" )
@@ -132,51 +134,6 @@ def generate_trainig_data(self):
132
134
133
135
return np .array (xy_data ), np .array (y_data )
134
136
135
- def embedding_rnn_seq2seq (self , encoder_inputs ,
136
- decoder_inputs ,
137
- cell ,
138
- output_projection = None ,
139
- feed_previous = False ,
140
- dtype = None ,
141
- scope = None ):
142
- _ , encoder_state = rnn .rnn (cell , encoder_inputs , dtype = dtype , scope = scope )
143
-
144
- def model_bak (self , feed_previous = False ):
145
- # 通过输入的XY生成encoder_inputs和带GO头的decoder_inputs
146
- input_data = tflearn .input_data (shape = [None , self .max_seq_len * 2 , self .word_vec_dim ], dtype = tf .float32 , name = "XY" )
147
- encoder_inputs = tf .slice (input_data , [0 , 0 , 0 ], [- 1 , self .max_seq_len , self .word_vec_dim ], name = "enc_in" )
148
- decoder_inputs_tmp = tf .slice (input_data , [0 , self .max_seq_len , 0 ], [- 1 , self .max_seq_len - 1 , self .word_vec_dim ], name = "dec_in_tmp" )
149
- go_inputs = tf .zeros_like (decoder_inputs_tmp )
150
- go_inputs = tf .slice (go_inputs , [0 , 0 , 0 ], [- 1 , 1 , self .word_vec_dim ])
151
- decoder_inputs = tf .concat (1 , [go_inputs , decoder_inputs_tmp ], name = "dec_in" )
152
-
153
- # 编码器
154
- # 把encoder_inputs交给编码器,返回一个输出(预测序列的第一个值)和一个状态(传给解码器)
155
- (encoder_output_tensor , states ) = tflearn .lstm (encoder_inputs , 200 , return_state = True , scope = 'encoder_lstm' )
156
- encoder_output_sequence = tf .pack ([encoder_output_tensor ], axis = 1 )
157
-
158
- # 解码器
159
- if feed_previous :
160
- # 预测过程用前一个时间序的输出作为下一个时间序的输入
161
- # 先用编码器的最后一个输出作为第一个输入
162
- decoder_output_tensor = tflearn .lstm (encoder_output_sequence , 200 , initial_state = states , return_seq = False , reuse = False , scope = 'decoder_lstm' )
163
- decoder_output_sequence_single = tf .pack ([decoder_output_tensor ], axis = 1 )
164
- decoder_output_sequence_list = [decoder_output_tensor ]
165
- # 再用解码器的输出作为下一个时序的输入
166
- for i in range (self .max_seq_len - 1 ):
167
- decoder_output_tensor = tflearn .lstm (decoder_output_sequence_single , 200 , return_seq = False , reuse = True , scope = 'decoder_lstm' )
168
- decoder_output_sequence_single = tf .pack ([decoder_output_tensor ], axis = 1 )
169
- decoder_output_sequence_list .append (decoder_output_tensor )
170
- else :
171
- # 把decoder_inputs交给解码器,返回一个输出序列
172
- decoder_output_sequence_list = tflearn .lstm (decoder_inputs , 200 , initial_state = states , return_seq = True , reuse = False , scope = 'decoder_lstm' )
173
-
174
- decoder_output_sequence = tf .pack (decoder_output_sequence_list , axis = 1 )
175
- real_output_sequence = tf .concat (1 , [encoder_output_sequence , decoder_output_sequence ])
176
-
177
- net = tflearn .regression (real_output_sequence , optimizer = 'sgd' , learning_rate = 0.1 , loss = 'mean_square' )
178
- model = tflearn .DNN (net )
179
- return model
180
137
181
138
def model (self , feed_previous = False ):
182
139
# 通过输入的XY生成encoder_inputs和带GO头的decoder_inputs
@@ -189,7 +146,7 @@ def model(self, feed_previous=False):
189
146
190
147
# 编码器
191
148
# 把encoder_inputs交给编码器,返回一个输出(预测序列的第一个值)和一个状态(传给解码器)
192
- (encoder_output_tensor , states ) = tflearn .lstm (encoder_inputs , 200 , return_state = True , scope = 'encoder_lstm' )
149
+ (encoder_output_tensor , states ) = tflearn .lstm (encoder_inputs , self . word_vec_dim , return_state = True , scope = 'encoder_lstm' )
193
150
encoder_output_sequence = tf .pack ([encoder_output_tensor ], axis = 1 )
194
151
195
152
# 解码器
@@ -199,7 +156,7 @@ def model(self, feed_previous=False):
199
156
first_dec_input = go_inputs
200
157
else :
201
158
first_dec_input = tf .slice (decoder_inputs , [0 , 0 , 0 ], [- 1 , 1 , self .word_vec_dim ])
202
- decoder_output_tensor = tflearn .lstm (first_dec_input , 200 , initial_state = states , return_seq = False , reuse = False , scope = 'decoder_lstm' )
159
+ decoder_output_tensor = tflearn .lstm (first_dec_input , self . word_vec_dim , initial_state = states , return_seq = False , reuse = False , scope = 'decoder_lstm' )
203
160
decoder_output_sequence_single = tf .pack ([decoder_output_tensor ], axis = 1 )
204
161
decoder_output_sequence_list = [decoder_output_tensor ]
205
162
# 再用解码器的输出作为下一个时序的输入
@@ -208,7 +165,7 @@ def model(self, feed_previous=False):
208
165
next_dec_input = decoder_output_sequence_single
209
166
else :
210
167
next_dec_input = tf .slice (decoder_inputs , [0 , i + 1 , 0 ], [- 1 , 1 , self .word_vec_dim ])
211
- decoder_output_tensor = tflearn .lstm (next_dec_input , 200 , return_seq = False , reuse = True , scope = 'decoder_lstm' )
168
+ decoder_output_tensor = tflearn .lstm (next_dec_input , self . word_vec_dim , return_seq = False , reuse = True , scope = 'decoder_lstm' )
212
169
decoder_output_sequence_single = tf .pack ([decoder_output_tensor ], axis = 1 )
213
170
decoder_output_sequence_list .append (decoder_output_tensor )
214
171
@@ -232,6 +189,6 @@ def load(self):
232
189
return model
233
190
234
191
if __name__ == '__main__' :
235
- my_seq2seq = MySeq2Seq ()
192
+ my_seq2seq = MySeq2Seq (word_vec_dim = word_vec_dim , max_seq_len = max_seq_len )
236
193
my_seq2seq .train ()
237
194
#model = my_seq2seq.load()
0 commit comments