Skip to content

Commit db94252

Browse files
author
lichuang
committed
some arg
1 parent f8c7b46 commit db94252

File tree

1 file changed

+10
-53
lines changed

1 file changed

+10
-53
lines changed

chatbotv2/my_seq2seq.py

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
max_w = 50
1616
float_size = 4
1717
word_vector_dict = {}
18+
word_vec_dim = 200
19+
max_seq_len = 16
1820

1921
def load_vectors(input):
2022
"""从vectors.bin加载词向量,返回一个word_vector_dict的词典,key是词,value是200维的向量
@@ -51,8 +53,8 @@ def load_vectors(input):
5153
vector.append(float(weight))
5254

5355
# 将词及其对应的向量存到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]
5658

5759
input_file.close()
5860

@@ -110,9 +112,9 @@ class MySeq2Seq(object):
110112
输出的时候把解码器的输出按照词向量的200维展平,这样输出就是(?,seqlen*200)
111113
这样就可以通过regression来做回归计算了,输入的y也展平,保持一致
112114
"""
113-
def __init__(self, max_seq_len = 16):
115+
def __init__(self, max_seq_len = 16, word_vec_dim = 200):
114116
self.max_seq_len = max_seq_len
115-
self.word_vec_dim = 200
117+
self.word_vec_dim = word_vec_dim
116118

117119
def generate_trainig_data(self):
118120
load_vectors("./vectors.bin")
@@ -132,51 +134,6 @@ def generate_trainig_data(self):
132134

133135
return np.array(xy_data), np.array(y_data)
134136

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
180137

181138
def model(self, feed_previous=False):
182139
# 通过输入的XY生成encoder_inputs和带GO头的decoder_inputs
@@ -189,7 +146,7 @@ def model(self, feed_previous=False):
189146

190147
# 编码器
191148
# 把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')
193150
encoder_output_sequence = tf.pack([encoder_output_tensor], axis=1)
194151

195152
# 解码器
@@ -199,7 +156,7 @@ def model(self, feed_previous=False):
199156
first_dec_input = go_inputs
200157
else:
201158
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')
203160
decoder_output_sequence_single = tf.pack([decoder_output_tensor], axis=1)
204161
decoder_output_sequence_list = [decoder_output_tensor]
205162
# 再用解码器的输出作为下一个时序的输入
@@ -208,7 +165,7 @@ def model(self, feed_previous=False):
208165
next_dec_input = decoder_output_sequence_single
209166
else:
210167
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')
212169
decoder_output_sequence_single = tf.pack([decoder_output_tensor], axis=1)
213170
decoder_output_sequence_list.append(decoder_output_tensor)
214171

@@ -232,6 +189,6 @@ def load(self):
232189
return model
233190

234191
if __name__ == '__main__':
235-
my_seq2seq = MySeq2Seq()
192+
my_seq2seq = MySeq2Seq(word_vec_dim=word_vec_dim, max_seq_len=max_seq_len)
236193
my_seq2seq.train()
237194
#model = my_seq2seq.load()

0 commit comments

Comments
 (0)