(_)
| 43 | |
| 44 | #1.load data(X:list of lint,y:int). 2.create session. 3.feed data. 4.training (5.validation) ,(6.prediction) |
| 45 | def main(_): |
| 46 | #1.load data(X:list of lint,y:int). |
| 47 | #if os.path.exists(FLAGS.cache_path): # load training data from cache file. |
| 48 | # with open(FLAGS.cache_path, 'r') as data_f: |
| 49 | # trainX, trainY, testX, testY, vocabulary_index2word=pickle.load(data_f) |
| 50 | # vocab_size=len(vocabulary_index2word) |
| 51 | #else: |
| 52 | if 1==1: |
| 53 | trainX, trainY, testX, testY = None, None, None, None |
| 54 | vocabulary_word2index, vocabulary_index2word = create_voabulary(word2vec_model_path=FLAGS.word2vec_model_path,name_scope="dynamic_memory_network") #simple='simple' |
| 55 | vocab_size = len(vocabulary_word2index) |
| 56 | print("dynamic_memory_network.vocab_size:",vocab_size) |
| 57 | vocabulary_word2index_label,vocabulary_index2word_label = create_voabulary_label(name_scope="dynamic_memory_network") |
| 58 | if FLAGS.multi_label_flag: |
| 59 | FLAGS.traning_data_path='../training-data/train-zhihu6-title-desc.txt' #change this line if want to train in a small dataset. e.g. dataset from 'test-zhihu6-title-desc.txt' |
| 60 | train,test,_=load_data_multilabel_new(vocabulary_word2index,vocabulary_word2index_label,multi_label_flag=FLAGS.multi_label_flag, |
| 61 | traning_data_path=FLAGS.traning_data_path) |
| 62 | trainX, trainY = train |
| 63 | testX, testY = test |
| 64 | |
| 65 | print("trainY:",trainY[0:10]) |
| 66 | # 2.Data preprocessing.Sequence padding |
| 67 | print("start padding & transform to one hot...") |
| 68 | trainX = pad_sequences(trainX, maxlen=FLAGS.sequence_length, value=0.) # padding to max length |
| 69 | testX = pad_sequences(testX, maxlen=FLAGS.sequence_length, value=0.) # padding to max length |
| 70 | #with open(FLAGS.cache_path, 'w') as data_f: #save data to cache file, so we can use it next time quickly. |
| 71 | # pickle.dump((trainX,trainY,testX,testY,vocabulary_index2word),data_f) |
| 72 | print("trainX[0]:", trainX[0]) #;print("trainY[0]:", trainY[0]) |
| 73 | # Converting labels to binary vectors |
| 74 | print("end padding & transform to one hot...") |
| 75 | #2.create session. |
| 76 | config=tf.ConfigProto() |
| 77 | config.gpu_options.allow_growth=True |
| 78 | with tf.Session(config=config) as sess: |
| 79 | #Instantiate Model |
| 80 | model = DynamicMemoryNetwork(FLAGS.num_classes, FLAGS.learning_rate, FLAGS.batch_size, FLAGS.decay_steps, FLAGS.decay_rate, FLAGS.sequence_length, |
| 81 | FLAGS.story_length,vocab_size, FLAGS.embed_size, FLAGS.hidden_size, FLAGS.is_training,num_pass=FLAGS.num_pass, |
| 82 | use_gated_gru=FLAGS.use_gated_gru,decode_with_sequences=FLAGS.decode_with_sequences,multi_label_flag=FLAGS.multi_label_flag,l2_lambda=FLAGS.l2_lambda) |
| 83 | #Initialize Save |
| 84 | saver=tf.train.Saver() |
| 85 | if os.path.exists(FLAGS.ckpt_dir+"checkpoint"): |
| 86 | print("Restoring Variables from Checkpoint") |
| 87 | saver.restore(sess,tf.train.latest_checkpoint(FLAGS.ckpt_dir)) |
| 88 | else: |
| 89 | print('Initializing Variables') |
| 90 | sess.run(tf.global_variables_initializer()) |
| 91 | if FLAGS.use_embedding: #load pre-trained word embedding |
| 92 | assign_pretrained_word_embedding(sess, vocabulary_index2word, vocab_size, model,word2vec_model_path=FLAGS.word2vec_model_path) |
| 93 | curr_epoch=sess.run(model.epoch_step) |
| 94 | #3.feed data & training |
| 95 | number_of_training_data=len(trainX) |
| 96 | print("number_of_training_data:",number_of_training_data) |
| 97 | previous_eval_loss=10000 |
| 98 | best_eval_loss=10000 |
| 99 | batch_size=FLAGS.batch_size |
| 100 | for epoch in range(curr_epoch,FLAGS.num_epochs): |
| 101 | loss, acc, counter = 0.0, 0.0, 0 |
| 102 | for start, end in zip(range(0, number_of_training_data, batch_size),range(batch_size, number_of_training_data, batch_size)): |
nothing calls this directly
no test coverage detected