def train(epoch): metrics = ' '.join( ['\r[{}]', '{:.1f}%', '{}/{}', 'accu={:.3f}', 'loss={:.3f}', '{}/{}']) for epoch_index in range(1, epoch + 1): print('Epoch {}:'.format(epoch_index)) time_start = time.time() epoch_trained = 0 right_ratio = [] bars_max = 20 batch_loss = [] batch_generator = data_util.get_batch(asks, answers) for encoder, decoder, target, weights in batch_generator: feed_dict = {} for i in range(len(encoder_inputs)): feed_dict[encoder_inputs[i]] = encoder[i] for i in range(len(decoder_inputs)): feed_dict[decoder_inputs[i]] = decoder[i] feed_dict[decoder_targets[i]] = target[i] feed_dict[decoder_weights[i]] = weights[i] _, output, loss = sess.run(ops, feed_dict) right = (target == np.asarray(output).argmax(axis=2)) right = right * weights right = np.sum(right) / np.sum(weights) right_ratio.append(right) epoch_trained += data_util.batch_size batch_loss.append(loss) time_now = time.time() time_spend = time_now - time_start time_estimate = time_spend / (epoch_trained / samples_per_epoch) percent = min(100, epoch_trained / samples_per_epoch) * 100 bars = math.floor(percent / 100 * bars_max) sys.stdout.write( metrics.format('=' * bars + '-' * (bars_max - bars), percent, epoch_trained, samples_per_epoch, np.mean(right_ratio), np.mean(batch_loss), data_util.time(time_spend), data_util.time(time_estimate))) sys.stdout.flush() if epoch_trained >= samples_per_epoch: break print('\n')
def train(noise_size, data_shape, batch_size, n_samples): """ @Author: Nelson Zhao -------------------- @param noise_size: 噪声size @param data_shape: 真实图像shape @batch_size: @n_samples: 显示示例图片数量 """ tf_img_batch = data_util.get_batch(image=img_files, image_W=IMAGE_W, image_H=IMAGE_H, batch_size=batch_size, capacity=INPUT_CAPACITY) # 存储loss losses = [] steps = 0 inputs_real, inputs_noise = get_inputs(noise_size, data_shape[1], data_shape[2], data_shape[3]) g_loss, d_loss = get_loss(inputs_real, inputs_noise, data_shape[-1]) g_train_opt, d_train_opt = get_optimizer(g_loss, d_loss, beta1, learning_rate) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: # for e in range(epochs): for step in np.arange(max_steps): step = step if coord.should_stop(): break batch_images = sess.run(tf_img_batch) # print(batch_images) # scale to -1, 1 # print(batch_images) # batch_images = batch_images * 2 - 1 # noise batch_noise = np.random.uniform(-1, 1, size=(batch_size, noise_size)) # run optimizer _ = sess.run(g_train_opt, feed_dict={ inputs_real: batch_images, inputs_noise: batch_noise }) _ = sess.run(d_train_opt, feed_dict={ inputs_real: batch_images, inputs_noise: batch_noise }) if step % 5000 == 0 or (step + 1) == max_steps: # if steps % 101 == 0: train_loss_d = d_loss.eval({ inputs_real: batch_images, inputs_noise: batch_noise }) train_loss_g = g_loss.eval({ inputs_real: batch_images, inputs_noise: batch_noise }) losses.append((train_loss_d, train_loss_g)) # 显示图片 samples = show_generator_output(sess, n_samples, inputs_noise, data_shape[-1]) plot_images(samples) print( "Epoch {}/{}....".format(step + 1, max_steps), "Discriminator Loss: {:.4f}....".format(train_loss_d), "Generator Loss: {:.4f}....".format(train_loss_g)) except tf.errors.OutOfRangeError: print('Done training -- epoch limit reached') finally: coord.request_stop() coord.join(threads)
# pred = tf.boolean_mask(pred_, mask) # accuracy = tf.reduce_mean(tf.cast(tf.equal(pred, true), tf.float32)) with tf.Session(graph=graph) as sess: if params.isTrain: saver = tf.train.Saver(tf.global_variables()) try: ckpt_path = tf.train.latest_checkpoint('./checkpoint/') saver.restore(sess, ckpt_path) except ValueError: init = tf.global_variables_initializer() sess.run(init) for epoch in range(params.epoch_num): for res_seq, res_labels, sentence_legth in data_util.get_batch( train_set, params.batch_size, word2id, tag2id, shuffle=params.shuffle): _, l, acc, global_nums, logits_, labels_ = sess.run( [train_op, loss, accuracy, global_add, pred_, true_], { word_ids: res_seq, labels: res_labels, sequence_lengths: sentence_legth, dropout_pl: params.dropout }) if global_nums % 20 == 0: saver.save(sess, './checkpoint/model.ckpt', global_step=global_nums) print( 'epoch {}, global_step {}, loss: {:.4}, accuracy: {:.4} '
def decode(): # Load vocabularies. doc_dict = data_util.load_dict(FLAGS.data_dir + "/doc_dict.txt") sum_dict = data_util.load_dict(FLAGS.data_dir + "/sum_dict.txt") if doc_dict is None or sum_dict is None: logging.warning("Dict not found.") docs, data = data_util.load_test_data(FLAGS.test_file, doc_dict) with tf.Session() as sess: # Create model and load parameters. logging.info("Creating %d layers of %d units." % (FLAGS.num_layers, FLAGS.size)) model = create_model(sess, True) class_model = create_class_model(sess, True) result = [] for idx, token_ids in enumerate(data): # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, encoder_len, decoder_len, class_output, class_len =\ data_util.get_batch( {0: [(token_ids, [data_util.ID_GO, data_util.ID_EOS],[0,0])]}, _buckets, 0, FLAGS.batch_size, False, 0) if FLAGS.batch_size == 1 and FLAGS.geneos: loss, outputs = model.step(sess, encoder_inputs, decoder_inputs, encoder_len, decoder_len, True) outputs = [np.argmax(item) for item in outputs[0]] else: outputs = model.step_beam(sess, encoder_inputs, encoder_len, geneos=FLAGS.geneos) # If there is an EOS symbol in outputs, cut them at that point. if data_util.ID_EOS in outputs: outputs = outputs[:outputs.index(data_util.ID_EOS)] gen_sum = " ".join(data_util.sen_map2tok(outputs, sum_dict[1])) gen_sum = data_util.sen_postprocess(gen_sum) result.append(gen_sum) logging.info("Finish {} samples. :: {}".format(idx, gen_sum[:75])) #Get Encoder outputs batchidx = 0 final_inputs = [] final_outputs = [] final_len = [] while batchidx + FLAGS.batch_size <= len(data): encoder_inputs, decoder_inputs, encoder_len, decoder_len, class_output, class_len =\ data_util.get_batch( {0: [(token_ids, [data_util.ID_GO, data_util.ID_EOS],[0,0])]}, _buckets, 0, FLAGS.batch_size, False, 0) _, _, enc_outputs = model.step(sess, encoder_inputs, decoder_inputs, encoder_len, decoder_len, True) enc_outputs = data_util.add_pad_for_hidden(enc_outputs, _buckets[0][0]) final_inputs.append(enc_outputs) final_outputs.append(class_output) final_len.append(class_len) batchidx += FLAGS.batch_size final_inputs = np.asarray(final_inputs) final_inputs = np.concatenate(final_inputs, 0) final_outputs = np.asarray(final_outputs) final_outputs = np.concatenate(final_outputs, 0) final_len = np.asarray(final_len) final_len = np.concatenate(final_len, 0) print(final_inputs.shape, final_outputs.shape, final_len.shape) #Hidden classifier step_loss, output = class_model.step(sess, final_inputs[:], final_outputs[:], final_len[:], True) clipped = np.array(output > 0.5, dtype=np.int) #label = data_util.hidden_label_gen(FLAGS.test_file, "data/test.1981.msg.txt") #make confusion matrix to get precision #tn, fp, fn, tp = confusion_matrix(label.flatten(), clipped.flatten()).ravel() #print("Test precision : ", tp/(tp+fp)) with open(FLAGS.test_output, "w") as f: for idx, item in enumerate(result): print(item, file=f) for j in range(len(docs[idx])): if clipped[idx][j] == 1: print("Recommended identifier: " + docs[idx][j] + " ", file=f) print("\n", file=f)
def train(): logging.info("Preparing summarization data.") docid, sumid, doc_dict, sum_dict, hidden_label= \ data_util.load_data( FLAGS.data_dir + "/train.48615.diff", FLAGS.data_dir + "/train.48615.msg", FLAGS.data_dir + "/doc_dict.txt", FLAGS.data_dir + "/sum_dict.txt", FLAGS.doc_vocab_size, FLAGS.sum_vocab_size) val_docid, val_sumid, val_hidd_label = \ data_util.load_valid_data( FLAGS.data_dir + "/valid.3000.diff", FLAGS.data_dir + "/valid.3000.msg", doc_dict, sum_dict) with tf.Session() as sess: # Create model. logging.info("Creating %d layers of %d units." % (FLAGS.num_layers, FLAGS.size)) train_writer = tf.summary.FileWriter(FLAGS.tfboard, sess.graph) model = create_model(sess, False) # Read data into buckets and compute their sizes. logging.info("Create buckets.") dev_set = create_bucket(val_docid, val_sumid, val_hidd_label) train_set = create_bucket(docid, sumid, hidden_label) train_bucket_sizes = [len(train_set[b]) for b in range(len(_buckets))] train_total_size = float(sum(train_bucket_sizes)) train_buckets_scale = [ sum(train_bucket_sizes[:i + 1]) / train_total_size for i in range(len(train_bucket_sizes)) ] for (s_size, t_size, _), nsample in zip(_buckets, train_bucket_sizes): logging.info("Train set bucket ({}, {}) has {} samples.".format( s_size, t_size, nsample)) # This is the training loop. step_time, loss = 0.0, 0.0 current_step = sess.run(model.global_step) while current_step < FLAGS.max_iter: random_number_01 = np.random.random_sample() bucket_id = min([ i for i in range(len(train_buckets_scale)) if train_buckets_scale[i] > random_number_01 ]) # Get a batch and make a step. start_time = time.time() encoder_inputs, decoder_inputs, encoder_len, decoder_len, class_output, class_len = \ data_util.get_batch(train_set, _buckets, bucket_id, FLAGS.batch_size, False,0) step_loss, _ = model.step(sess, encoder_inputs, decoder_inputs, encoder_len, decoder_len, False, train_writer) step_time += (time.time() - start_time) / \ FLAGS.steps_per_validation loss += step_loss * FLAGS.batch_size / np.sum(decoder_len) \ / FLAGS.steps_per_validation current_step += 1 # Once in a while, we save checkpoint. if current_step % FLAGS.steps_per_checkpoint == 0: # Save checkpoint and zero timer and loss. checkpoint_path = os.path.join(FLAGS.train_dir, "model.ckpt") model.saver.save(sess, checkpoint_path, global_step=model.global_step) # Once in a while, we print statistics and run evals. if current_step % FLAGS.steps_per_validation == 0: # Print statistics for the previous epoch. perplexity = np.exp(float(loss)) logging.info("global step %d step-time %.2f ppl %.2f" % (model.global_step.eval(), step_time, perplexity)) step_time, loss = 0.0, 0.0 # Run evals on development set and print their perplexity. for bucket_id in range(len(_buckets)): if len(dev_set[bucket_id]) == 0: logging.info(" eval: empty bucket %d" % (bucket_id)) continue encoder_inputs, decoder_inputs, encoder_len, decoder_len, class_output, class_len =\ data_util.get_batch(dev_set, _buckets, bucket_id, FLAGS.batch_size, False, 0) #cl_eval_loss, _ = class_model.step(sess, class_input, class_output, class_len, True) eval_loss, _, _ = model.step(sess, encoder_inputs, decoder_inputs, encoder_len, decoder_len, True) eval_loss = eval_loss * FLAGS.batch_size \ / np.sum(decoder_len) eval_ppx = np.exp(float(eval_loss)) logging.info(" eval: bucket %d ppl %.2f" % (bucket_id, eval_ppx)) sys.stdout.flush() #Get Encoder outputs batchidx = 0 final_inputs = [] final_outputs = [] final_len = [] while batchidx + FLAGS.batch_size <= train_bucket_sizes[0]: encoder_inputs, decoder_inputs, encoder_len, decoder_len, class_output, class_len = \ data_util.get_batch(train_set, _buckets, bucket_id, FLAGS.batch_size, True, batchidx) _, _, enc_outputs = model.step(sess, encoder_inputs, decoder_inputs, encoder_len, decoder_len, True) enc_outputs = data_util.add_pad_for_hidden(enc_outputs, _buckets[0][0]) final_inputs.append(enc_outputs) final_outputs.append(class_output) final_len.append(class_len) batchidx += FLAGS.batch_size final_inputs = np.asarray(final_inputs) final_inputs = np.concatenate(final_inputs, 0) final_outputs = np.asarray(final_outputs) final_outputs = np.concatenate(final_outputs, 0) final_len = np.asarray(final_len) final_len = np.concatenate(final_len, 0) print(final_inputs.shape, final_outputs.shape, final_len.shape) #Hidden classifier class_model = create_class_model(sess, False) classification_curr_step = sess.run(class_model.global_step) i = 0 while classification_curr_step <= FLAGS.class_max_iter: _, step_loss, output = class_model.step(sess, final_inputs[i:(i + 160)], final_outputs[i:(i + 160)], final_len[i:(i + 160)], False) classification_curr_step += 1 clipped = np.array(output > 0.5, dtype=np.int) #print("i", i) #print("clfcurrstep",classification_curr_step) #print("clipped", clipped.flatten()) #print("final_outputs", final_outputs[i:(i+160)].flatten()) tn, fp, fn, tp = confusion_matrix( final_outputs[i:(i + 160)].flatten(), clipped.flatten()).ravel() if (classification_curr_step % 40 == 0): print("Train Precision", tp / (tp + fp + 0.1)) print("Train Accuracy", (tp + tn) / (tp + fp + tn + fn)) if (i + 160 == len(final_len)): i = 0 else: i += 160 # Once in a while, we save checkpoint. if classification_curr_step % FLAGS.steps_per_checkpoint == 0: # Save checkpoint and zero timer and loss. checkpoint_path = os.path.join(FLAGS.class_train_dir, "class_model.ckpt") class_model.saver.save(sess, checkpoint_path, global_step=class_model.global_step) print("test_file", FLAGS.test_file) docs, data = data_util.load_test_data(FLAGS.test_file, doc_dict) #test # Create model and load parameters. ''' logging.info("Creating %d layers of %d units." % (FLAGS.num_layers, FLAGS.size)) result = [] for idx, token_ids in enumerate(data): # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, encoder_len, decoder_len, class_output, class_len =\ data_util.get_batch( {0: [(token_ids, [data_util.ID_GO, data_util.ID_EOS],[0,0])]}, _buckets, 0, FLAGS.batch_size, False, 0) if FLAGS.batch_size == 1 and FLAGS.geneos: loss, outputs = model.step(sess, encoder_inputs, decoder_inputs, encoder_len, decoder_len, False) outputs = [np.argmax(item) for item in outputs[0]] else: outputs = model.step_beam( sess, encoder_inputs, encoder_len, geneos=FLAGS.geneos) # If there is an EOS symbol in outputs, cut them at that point. if data_util.ID_EOS in outputs: outputs = outputs[:outputs.index(data_util.ID_EOS)] gen_sum = " ".join(data_util.sen_map2tok(outputs, sum_dict[1])) gen_sum = data_util.sen_postprocess(gen_sum) result.append(gen_sum) logging.info("Finish {} samples. :: {}".format(idx, gen_sum[:75])) ''' #Get Encoder outputs docid, sumid, doc_dict, sum_dict, hidden_label= \ data_util.load_data( FLAGS.data_dir + "/test.1981.diff.txt", FLAGS.data_dir + "/test.1981.msg.txt", FLAGS.data_dir + "/doc_dict.txt", FLAGS.data_dir + "/sum_dict.txt", FLAGS.doc_vocab_size, FLAGS.sum_vocab_size) test_set = create_bucket(docid, sumid, hidden_label) test_bucket_sizes = [len(test_set[b]) for b in range(len(_buckets))] test_total_size = float(sum(test_bucket_sizes)) test_buckets_scale = [ sum(test_bucket_sizes[:i + 1]) / test_total_size for i in range(len(test_bucket_sizes)) ] batchidx = 0 final_inputs = [] final_outputs = [] final_len = [] #data.shape == (1, 158, 3) so I changed FLAGS.batch_size FLAGS.batch_size = 158 while batchidx + FLAGS.batch_size <= len(data): #bucket_id = (i for i in range(len(test_buckets_scale)) encoder_inputs, decoder_inputs, encoder_len, decoder_len, class_output, class_len = \ data_util.get_batch(test_set, _buckets, bucket_id, FLAGS.batch_size, True, batchidx) _, _, enc_outputs = model.step(sess, encoder_inputs, decoder_inputs, encoder_len, decoder_len, True) enc_outputs = data_util.add_pad_for_hidden(enc_outputs, _buckets[0][0]) final_inputs.append(enc_outputs) final_outputs.append(class_output) final_len.append(class_len) batchidx += 1 final_inputs = np.asarray(final_inputs) final_inputs = np.concatenate(final_inputs, 0) final_outputs = np.asarray(final_outputs) final_outputs = np.concatenate(final_outputs, 0) final_len = np.asarray(final_len) final_len = np.concatenate(final_len, 0) print(final_inputs.shape, final_outputs.shape, final_len.shape) #Hidden classifier step_loss, output = class_model.step(sess, final_inputs[:], final_outputs[:], final_len[:], True) clipped = np.array(output > 0.5, dtype=np.int) tn, fp, fn, tp = confusion_matrix(final_outputs[:].flatten(), clipped.flatten()).ravel() #with open('data/test.1981.msg.txt')as reader: # testmsg=[] # for i in range(1981): # testmsg.append(reader.readline()) #sums = list(map(lambda x: x.split(), testmsg)) #labels = data_util.hidden_label_gen(FLAGS.test_file, sums) #tn, fp, fn, tp = confusion_matrix(labels.flatten(), clipped.flatten()) print("Test Precision : ", tp / (tp + fp + 0.1)) print("Test Accuracy", (tp + tn) / (tp + fp + tn + fn)) with open(FLAGS.test_output, "w") as f: for idx in range(1981): for j in range(len(docs[idx])): if clipped[idx][j] == 1: print("Recommended identifier: " + docs[idx][j] + " ", file=f) print("\n", file=f)
# config loading config = Config() # data loading train_source, train_source_length, \ train_target, train_target_length = load_data( FLAGS.train_source_file, FLAGS.train_target_file, config.source_max, config.target_max) test_source, test_source_length, \ test_target, test_target_length = load_data( FLAGS.test_source_file, FLAGS.test_target_file, config.source_max, config.target_max) batch_generator = get_batch(train_source, train_source_length, train_target, train_target_length, config.batch_size, config.num_epochs) # model build model = Seq2Seq(config, mode='train') model.build() sess = tf.Session() if FLAGS.keep_training: checkpoint_file = tf.train.latest_checkpoint(FLAGS.model_dir) saver = tf.train.Saver() saver.restore(sess, checkpoint_file) else: sess.run(tf.global_variables_initializer()) # summaries & saver summary_writer = tf.summary.FileWriter(FLAGS.summary_dir)