MCPcopy Create free account
hub / github.com/brightmart/text_classification / train

Function train

a09_DynamicMemoryNet/a8_dynamic_memory_network.py:315–354  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

313#step1. run train function to train the model. it will save checkpoint
314#step2. run predict function to make a prediction based on the model restore from the checkpoint.
315def train():
316 # below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
317 num_classes = 15
318 learning_rate = 0.001
319 batch_size = 8
320 decay_steps = 1000
321 decay_rate = 0.9
322 sequence_length = 10
323 vocab_size = 10000
324 embed_size = 100
325 hidden_size = 100
326 is_training = True
327 story_length = 3
328 dropout_keep_prob = 1
329 model = DynamicMemoryNetwork(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
330 story_length, vocab_size, embed_size, hidden_size, is_training,
331 multi_label_flag=False)
332 ckpt_dir = 'checkpoint_dmn/dummy_test/'
333 saver = tf.train.Saver()
334 with tf.Session() as sess:
335 sess.run(tf.global_variables_initializer())
336 for i in range(1500):
337 # input_x should be:[batch_size, num_sentences,self.sequence_length]
338 story = np.random.randn(batch_size, story_length, sequence_length)
339 story[story > 0] = 1
340 story[story <= 0] = 0
341 query = np.random.randn(batch_size, sequence_length) # [batch_size, sequence_length]
342 query[query > 0] = 1
343 query[query <= 0] = 0
344 answer_single = np.sum(query, axis=1) + np.round(0.1 * np.sum(np.sum(story, axis=1),
345 axis=1)) # [batch_size].e.g. np.array([1, 0, 1, 1, 1, 2, 1, 1])
346 loss, acc, predict, _ = sess.run(
347 [model.loss_val, model.accuracy, model.predictions, model.train_op],
348 feed_dict={model.query: query, model.story: story, model.answer_single: answer_single,
349 model.dropout_keep_prob: dropout_keep_prob})
350 print(i, "query:", query, "=====================>")
351 print(i, "loss:", loss, "acc:", acc, "label:", answer_single, "prediction:", predict)
352 if i % 300 == 0:
353 save_path = ckpt_dir + "model.ckpt"
354 saver.save(sess, save_path, global_step=i * 300)
355
356def predict():
357 num_classes = 15

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected