MCPcopy Create free account
hub / github.com/chinawithfrank/ChatBotCourse / train

Method train

chatbotv3/encoder_decoder_seq2seq.py:240–272  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

238 return optimizer, cost, decoder_layer2_outputs
239
240 def train(self):
241 x = tf.placeholder("float", [None, self.max_seq_len * 2, self.word_vec_dim])
242 y = tf.placeholder("float", [None, self.max_seq_len, self.one_hot_word_vectors_dim])
243
244 weights = {
245 'enc2dec': tf.Variable(tf.random_normal([self.word_vec_dim, self.one_hot_word_vectors_dim])),
246 'hid2tar': tf.Variable(tf.random_normal([self.n_hidden, self.one_hot_word_vectors_dim])),
247 }
248 biases = {
249 'enc2dec': tf.Variable(tf.random_normal([self.max_seq_len, self.one_hot_word_vectors_dim])),
250 'hid2tar': tf.Variable(tf.random_normal([self.max_seq_len, self.one_hot_word_vectors_dim])),
251 }
252
253 optimizer, cost, decoder_layer2_outputs = self.model(x, y, weights, biases)
254
255 init = tf.global_variables_initializer()
256 sess = tf.Session()
257 sess.run(init)
258
259 XY, Y = self.next_batch()
260 n_steps = len(XY)
261
262 for i in range(self.epoch):
263 for step in range(n_steps):
264 train_XY = XY[step:]
265 train_Y = Y[step:]
266 sess.run(optimizer, feed_dict={x: train_XY, y: train_Y})
267 loss = sess.run(cost, feed_dict={x: train_XY, y: train_Y})
268 if i % 1 == 0 and step == 0:
269 print 'i=%d, loss=%f' % (i, loss)
270
271 saver = tf.train.Saver()
272 saver.save(sess, self.model_dir)
273
274 def test(self):
275 x = tf.placeholder("float", [None, self.max_seq_len * 2, self.word_vec_dim])

Callers 1

mainFunction · 0.95

Calls 2

modelMethod · 0.95
next_batchMethod · 0.95

Tested by

no test coverage detected