def build_model(self): with self.testgraph.as_default(): self.testmodel = pointer_net.PointerNet(batch_size=1, max_input_sequence_len=FLAGS.max_input_sequence_len, max_output_sequence_len=FLAGS.max_output_sequence_len, rnn_size=FLAGS.rnn_size, attention_size=FLAGS.attention_size, num_layers=FLAGS.num_layers, beam_width=FLAGS.beam_width, learning_rate=FLAGS.learning_rate, max_gradient_norm=FLAGS.max_gradient_norm, forward_only=True) with self.graph.as_default(): # Build model self.model = pointer_net.PointerNet(batch_size=FLAGS.batch_size, max_input_sequence_len=FLAGS.max_input_sequence_len, max_output_sequence_len=FLAGS.max_output_sequence_len, rnn_size=FLAGS.rnn_size, attention_size=FLAGS.attention_size, num_layers=FLAGS.num_layers, beam_width=FLAGS.beam_width, learning_rate=FLAGS.learning_rate, max_gradient_norm=FLAGS.max_gradient_norm, forward_only=self.forward_only) self.sess.run(tf.global_variables_initializer()) ckpt = tf.train.get_checkpoint_state(FLAGS.models_dir) print(ckpt, FLAGS.models_dir) if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): print("Load model parameters from %s" % ckpt.model_checkpoint_path) self.model.saver.restore(self.sess, ckpt.model_checkpoint_path) else: print("Created model with fresh parameters.") self.writer = tf.summary.FileWriter(FLAGS.models_dir + '/train',self.sess.graph)
def main(): train_data = DataGenerator(FLAGS, 'train', 40000) valid_data = DataGenerator(FLAGS, 'valid', 5000) # test_data = DataGenerator(FLAGS, 'test') my_model = pointer_net.PointerNet(batch_size=FLAGS.batch_size, max_input_sequence_len=FLAGS.max_input_sequence_len, max_output_sequence_len=FLAGS.max_output_sequence_len, rnn_size=FLAGS.rnn_size, attention_size=FLAGS.attention_size, num_layers=FLAGS.num_layers, beam_width=FLAGS.beam_width, learning_rate=FLAGS.learning_rate, max_gradient_norm=FLAGS.max_gradient_norm, ) trainsummary = open('trainingsummary.txt', 'w+') with tf.Session() as sess: writer = tf.summary.FileWriter(FLAGS.log_dir + '/train', sess.graph) ckpt = tf.train.get_checkpoint_state(FLAGS.log_dir) if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): print("Load model parameters from %s" % ckpt.model_checkpoint_path) my_model.saver.restore(sess, ckpt.model_checkpoint_path) else: print("Created model with fresh parameters.") sess.run(tf.global_variables_initializer()) print('start!!!!!!!!!!!!!!!!!') step_time = 0.0 loss = 0.0 current_step = 0 train_flag_var = False for _ in range(FLAGS.train_epoch*(train_data.data_size//FLAGS.batch_size)): start_time = time.time() inputs, enc_input_weights, outputs, dec_input_weights = \ train_data.get_batch(True) summary, step_loss, predicted_ids_with_logits, targets, debug_var = \ my_model.step(sess, inputs, enc_input_weights, outputs, dec_input_weights, train_flag_var) step_time += (time.time() - start_time) / FLAGS.steps_per_checkpoint loss += step_loss / FLAGS.steps_per_checkpoint current_step += 1 # Time to print statistic and save model if current_step % FLAGS.steps_per_checkpoint == 0: train_flag_var = True with sess.as_default(): gstep = my_model.global_step.eval() print("global step %d step-time %.2f loss %.2f" % (gstep, step_time, loss)) trainsummary.write('Epoch %d \n' % (current_step/FLAGS.steps_per_checkpoint)) trainsummary.write("global step %d step-time %.2f loss %.2f \n" % (gstep, step_time, loss)) #### eval_valid(valid_data, my_model, sess, train_flag_var, trainsummary) writer.add_summary(summary, gstep) checkpoint_path = os.path.join(FLAGS.log_dir, "wdp.ckpt") my_model.saver.save(sess, checkpoint_path, global_step=my_model.global_step) step_time, loss = 0.0, 0.0 train_flag_var = False trainsummary.close()
def build_model(self): with self.graph.as_default(): self.model = pointer_net.PointerNet( batch_size=self.batch_size, max_input_sequence_len=self.max_input_sequence_len, max_output_sequence_len=self.max_output_sequence_len, rnn_size=self.rnn_size, attention_size=self.attention_size, num_layers=self.num_layers, beam_width=self.beam_width, forward_only=self.forward_only) ckpt = tf.train.get_checkpoint_state(self.modelpath) print(ckpt, self.modelpath) if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): print("Load model parameters from %s" % ckpt.model_checkpoint_path) self.model.saver.restore(self.sess, ckpt.model_checkpoint_path) self.sess.graph.finalize()
def main(): test_data = DataGenerator(FLAGS, 'test', 10000) my_model = pointer_net.PointerNet( batch_size=FLAGS.batch_size, max_input_sequence_len=FLAGS.max_input_sequence_len, max_output_sequence_len=FLAGS.max_output_sequence_len, rnn_size=FLAGS.rnn_size, attention_size=FLAGS.attention_size, num_layers=FLAGS.num_layers, beam_width=FLAGS.beam_width, learning_rate=FLAGS.learning_rate, max_gradient_norm=FLAGS.max_gradient_norm, ) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(FLAGS.log_dir) if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): print("Load model parameters from %s" % ckpt.model_checkpoint_path) my_model.saver.restore(sess, ckpt.model_checkpoint_path) else: return inputs, enc_input_weights, outputs, dec_input_weights = \ test_data.get_batch(False) test_data.read_unit() units = test_data.units_batch(False) # read the input, output and the units from the database correct_count = 0 feasible_count = 0 largest_gap = 0 count = 0 while inputs.shape[0] >= FLAGS.batch_size: batch_inputs = inputs[:FLAGS.batch_size] batch_enc_input_weights = enc_input_weights[:FLAGS.batch_size] batch_outputs = outputs[:FLAGS.batch_size] batch_dec_input_weights = dec_input_weights[:FLAGS.batch_size] batch_units = units[:FLAGS.batch_size] # get a batch of data which is used to evaluate inputs = inputs[FLAGS.batch_size:] enc_input_weights = enc_input_weights[FLAGS.batch_size:] outputs = outputs[FLAGS.batch_size:] dec_input_weights = dec_input_weights[FLAGS.batch_size:] units = units[FLAGS.batch_size:] # delete that batch from the read_in data predicted_ids_with_logits, targets = \ my_model.step(sess, batch_inputs, batch_enc_input_weights, batch_outputs, batch_dec_input_weights, True) # model prediction # print('*'*20) # print('Predicted_ids_with_logits:') # print(predicted_ids_with_logits[1].reshape(FLAGS.batch_size, -1)) # print('Targets:') # print(targets[0]) # print('Batch_units:') # print(batch_units.shape) temp_correct, temp_feasible, temp_gap = eval( predicted_ids_with_logits, targets, batch_units, batch_inputs) correct_count = correct_count + temp_correct feasible_count = feasible_count + temp_feasible largest_gap = max(largest_gap, temp_gap) count = count + 1 print('=' * 20) print('round{}'.format(count)) print('Acc: {:.2f}% ({}/{})'.format( correct_count / (count * FLAGS.batch_size) * 100, correct_count, count * FLAGS.batch_size)) print('Feasible_rate: {:.2f}% ({}/{})'.format( feasible_count / (count * FLAGS.batch_size) * 100, feasible_count, count * FLAGS.batch_size)) print('Largest_gap: {:.2f}'.format(largest_gap))
def main(): test_data = WdpDataGenerator(FLAGS, 'test', 2000) my_model = pointer_net.PointerNet( batch_size=FLAGS.batch_size, max_input_sequence_len=FLAGS.max_input_sequence_len, max_output_sequence_len=FLAGS.max_output_sequence_len, rnn_size=FLAGS.rnn_size, attention_size=FLAGS.attention_size, num_layers=FLAGS.num_layers, beam_width=FLAGS.beam_width, learning_rate=FLAGS.learning_rate, max_gradient_norm=FLAGS.max_gradient_norm, ) pred, tar, pred_time = eval(test_data, my_model) print('The total time of prediction is ', pred_time / pred.shape[0]) # use the model to predict inp = test_data.inputs[:tar.shape[0]] inp = np.delete(inp, range(FLAGS.num_bid, FLAGS.max_input_sequence_len), axis=1) u = test_data.units[:tar.shape[0]] print('pred:', pred.shape) print('tar:', tar.shape) print('inp', inp.shape) print('u', u.shape) # basic check algorithm unit_ = [] current_revenue = np.zeros(tar.shape[0]) target_revenue = np.zeros(tar.shape[0]) for i in range(tar.shape[0]): bid_pred = pred[i] bid_tar = tar[i] current_u = np.zeros(FLAGS.num_item) current_r = 0 target_revenue[i] = sum([ inp[i][bid_tar[k] - 1][-1] if bid_tar[k] > 0 else 0 for k in range(FLAGS.num_bid) ]) for j in range(FLAGS.num_bid): if bid_pred[j] == 0: current_revenue[i] = current_r unit_.append(current_u) break current_u = current_u + inp[i][bid_pred[j] - 1][:-1] current_r = current_r + inp[i][bid_pred[j] - 1][-1] if any(current_u > u[i]): current_u = current_u - inp[i][bid_pred[j] - 1][:-1] current_r = current_r - inp[i][bid_pred[j] - 1][-1] unit_.append(current_u) current_revenue[i] = current_r pred[i][j:] = 0 break unit_ = np.stack(unit_) accuracy_check(pred, tar) feasible_check(unit_, u) gap_check(current_revenue, target_revenue) # improvement using local searching local_p = [] local_r = [] start = time.perf_counter() for i in range(pred.shape[0]): if all(pred[i] == tar[i]): local_p.append(pred[i]) local_r.append(current_revenue[i]) pass else: instance = inp[i] unit = u[i] result_opt = fromindex2vector(pred[i][:], FLAGS.num_bid) result_opt = np.array(result_opt) if i == 20: print(instance) print(unit) print(result_opt) local_result, local_revenue = local_search(instance, unit, result_opt, wp=0.5) # print("instance:") # print(instance) # print("unit:") # print(unit) # print('tar:') # print(tar[i]) # print('tar_revenue:') # print(target_revenue[i]) # print('local_result:') # print(local_result) # print('local_revenue') # print(local_revenue) local_p.append(local_result) local_r.append(local_revenue) print('___finish___{}___search'.format(i + 1)) # use for debug # if i == 0: # break # return 0 # use for debug walltime = time.perf_counter() - start print('Average time is {} s'.format(walltime / pred.shape[0])) local_p = np.stack(local_p) local_r = np.stack(local_r) accuracy_check(local_p, tar) gap_check(local_r, target_revenue)