def train(): """Train ring_net for a number of steps.""" with tf.Graph().as_default(): # make inputs state, reward, action = ring_net.inputs(4, 15) # possible input dropout input_keep_prob = tf.placeholder("float") state_drop = tf.nn.dropout(state, input_keep_prob) # possible dropout inside keep_prob_encoding = tf.placeholder("float") keep_prob_lstm = tf.placeholder("float") # unwrap x_2_o = [] # first step x_2, reward_2, hidden_state = ring_net.encode_compress_decode(state[:,0,:,:,:], action[:,1,:], None, keep_prob_encoding, keep_prob_lstm) tf.get_variable_scope().reuse_variables() # unroll for 9 more steps for i in xrange(9): x_2, reward_2, hidden_state = ring_net.encode_compress_decode(state[:,i+1,:,:,:], action[:,i+2,:], hidden_state, keep_prob_encoding, keep_prob_lstm) # now collect values x_2_o.append(x_2) for i in xrange(4): x_2, reward_2, hidden_state = ring_net.encode_compress_decode(x_2, action[:,i+11,:], hidden_state, keep_prob_encoding, keep_prob_lstm) x_2_o.append(x_2) tf.image_summary('images_gen_' + str(i), x_2) x_2_o = tf.pack(x_2_o) x_2_o = tf.transpose(x_2_o, perm=[1,0,2,3,4]) # error error = tf.nn.l2_loss(state[:,10:15,:,:,:] - x_2_o) tf.scalar_summary('loss', error) # train (hopefuly) train_op = ring_net.train(error, 1e-5) # List of all Variables variables = tf.all_variables() # Build a saver saver = tf.train.Saver(tf.all_variables(), max_to_keep=1) # Summary op summary_op = tf.merge_all_summaries() # Start running operations on the Graph. sess = tf.Session() # init from seq 1 model print("init from " + RESTORE_DIR) saver_restore = tf.train.Saver(variables) ckpt = tf.train.get_checkpoint_state(RESTORE_DIR) saver_restore.restore(sess, ckpt.model_checkpoint_path) # Start que runner tf.train.start_queue_runners(sess=sess) # Summary op graph_def = sess.graph.as_graph_def(add_shapes=True) summary_writer = tf.train.SummaryWriter(SAVE_DIR, graph_def=graph_def) for step in xrange(500000): t = time.time() _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:1.0}) elapsed = time.time() - t assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step%100 == 0: print("loss value at " + str(loss_value)) print("time per batch is " + str(elapsed)) summary_str = sess.run(summary_op, feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:1.0}) summary_writer.add_summary(summary_str, step) if step%1000 == 0: checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print("saved to " + SAVE_DIR)
def train(iteration): """Train ring_net for a number of steps.""" with tf.Graph().as_default(): # make inputs state, reward, action = ring_net.inputs(CURRICULUM_BATCH_SIZE[iteration], CURRICULUM_SEQ[iteration]) # possible input dropout input_keep_prob = tf.placeholder("float") state_drop = tf.nn.dropout(state, input_keep_prob) # possible dropout inside keep_prob_encoding = tf.placeholder("float") keep_prob_lstm = tf.placeholder("float") # create and unrap network output_f, output_t, output_g, output_reward, output_autoencoder = ring_net.unwrap(state_drop, action, keep_prob_encoding, keep_prob_lstm, CURRICULUM_SEQ[iteration], CURRICULUM_TRAIN_PEICE[iteration]) # calc error error = ring_net.loss(state, reward, output_f, output_t, output_g, output_reward, output_autoencoder, CURRICULUM_TRAIN_PEICE[iteration]) error = tf.div(error, CURRICULUM_SEQ[iteration]) # train (hopefuly) train_op = ring_net.train(error, CURRICULUM_LEARNING_RATE[iteration]) # List of all Variables variables = tf.all_variables() #for i, variable in enumerate(variables): # print '----------------------------------------------' # print variable.name[:variable.name.index(':')] # Build a saver saver = tf.train.Saver(tf.all_variables()) # Summary op summary_op = tf.merge_all_summaries() # Build an initialization operation to run below. if iteration == 0: init = tf.initialize_all_variables() # Start running operations on the Graph. sess = tf.Session() # init if this is the very time training if iteration == 0: print("init network from scratch") sess.run(init) # restore if iteration is not 0 if iteration != 0: variables_to_restore = tf.all_variables() autoencoder_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" not in variable.name[:variable.name.index(':')]) and ("RNN" not in variable.name[:variable.name.index(':')])] rnn_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" in variable.name[:variable.name.index(':')]) or ("RNN" in variable.name[:variable.name.index(':')])] ckpt = tf.train.get_checkpoint_state(SAVE_DIR) autoencoder_saver = tf.train.Saver(autoencoder_variables) print("restoring autoencoder part of network form " + ckpt.model_checkpoint_path) autoencoder_saver.restore(sess, ckpt.model_checkpoint_path) print("restored file from " + ckpt.model_checkpoint_path) if CURRICULUM_SEQ[iteration-1] == 1: print("init compression part of network from scratch") rnn_init = tf.initialize_variables(rnn_variables) sess.run(rnn_init) else: rnn_saver = tf.train.Saver(rnn_variables) print("restoring compression part of network") rnn_saver.restore(sess, ckpt.model_checkpoint_path) print("restored file from " + ckpt.model_checkpoint_path) # Start que runner tf.train.start_queue_runners(sess=sess) # Summary op graph_def = sess.graph.as_graph_def(add_shapes=True) summary_writer = tf.train.SummaryWriter(SAVE_DIR, graph_def=graph_def) for step in xrange(CURRICULUM_STEPS[iteration]): t = time.time() _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:.8}) elapsed = time.time() - t assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step%100 == 0: print("loss value at " + str(loss_value)) print("time per batch is " + str(elapsed)) summary_str = sess.run(summary_op, feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:.8}) summary_writer.add_summary(summary_str, step) if step%1000 == 0: checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print("saved to " + SAVE_DIR)
def train(iteration): """Train ring_net for a number of steps.""" with tf.Graph().as_default(): # make inputs state, reward, action = ring_net.inputs(CURRICULUM_BATCH_SIZE[iteration], CURRICULUM_SEQ[iteration]) # possible input dropout input_keep_prob = tf.placeholder("float") state_drop = tf.nn.dropout(state, input_keep_prob) # possible dropout inside keep_prob_encoding = tf.placeholder("float") keep_prob_lstm = tf.placeholder("float") # create and unrap network output_f, output_t, output_g, output_reward, output_autoencoder = ring_net.unwrap(state_drop, action, keep_prob_encoding, keep_prob_lstm, CURRICULUM_SEQ[iteration], CURRICULUM_TRAIN_PEICE[iteration]) # calc error error = ring_net.loss(state, reward, output_f, output_t, output_g, output_reward, output_autoencoder, CURRICULUM_TRAIN_PEICE[iteration]) error = tf.div(error, CURRICULUM_SEQ[iteration]) # train (hopefuly) train_op = ring_net.train(error, CURRICULUM_LEARNING_RATE[iteration]) # List of all Variables variables = tf.all_variables() #for i, variable in enumerate(variables): # print '----------------------------------------------' # print variable.name[:variable.name.index(':')] # Build a saver saver = tf.train.Saver(tf.all_variables()) # Summary op summary_op = tf.merge_all_summaries() # Start running operations on the Graph. sess = tf.Session() # restore if iteration is not 0 variables_to_restore = tf.all_variables() autoencoder_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" not in variable.name[:variable.name.index(':')]) and ("RNN" not in variable.name[:variable.name.index(':')])] rnn_variables = [variable for i, variable in enumerate(variables_to_restore) if ("compress" in variable.name[:variable.name.index(':')]) or ("RNN" in variable.name[:variable.name.index(':')])] if iteration == 0: ckpt = tf.train.get_checkpoint_state(LOAD_DIR) else: ckpt = tf.train.get_checkpoint_state(SAVE_DIR) autoencoder_saver = tf.train.Saver(autoencoder_variables) print("restoring autoencoder part of network form " + ckpt.model_checkpoint_path) autoencoder_saver.restore(sess, ckpt.model_checkpoint_path) print("restored file from " + ckpt.model_checkpoint_path) if iteration == 0: print("init compression part of network from scratch") rnn_init = tf.initialize_variables(rnn_variables) sess.run(rnn_init) else: rnn_saver = tf.train.Saver(rnn_variables) print("restoring compression part of network") rnn_saver.restore(sess, ckpt.model_checkpoint_path) print("restored file from " + ckpt.model_checkpoint_path) # Start que runner tf.train.start_queue_runners(sess=sess) # Summary op graph_def = sess.graph.as_graph_def(add_shapes=True) summary_writer = tf.train.SummaryWriter(SAVE_DIR, graph_def=graph_def) for step in xrange(CURRICULUM_STEPS[iteration]): t = time.time() _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:.8}) elapsed = time.time() - t assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step%100 == 0: print("loss value at " + str(loss_value)) print("time per batch is " + str(elapsed)) summary_str = sess.run(summary_op, feed_dict={keep_prob_encoding:1.0, keep_prob_lstm:1.0, input_keep_prob:.8}) summary_writer.add_summary(summary_str, step) if step%1000 == 0: checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print("saved to " + SAVE_DIR)
def train(): """Train ring_net for a number of steps.""" with tf.Graph().as_default(): # make inputs state, reward, action = ring_net.inputs(4, 15) # possible input dropout input_keep_prob = tf.placeholder("float") state_drop = tf.nn.dropout(state, input_keep_prob) # possible dropout inside keep_prob_encoding = tf.placeholder("float") keep_prob_lstm = tf.placeholder("float") # unwrap x_2_o = [] # first step x_2, reward_2, hidden_state = ring_net.encode_compress_decode( state[:, 0, :, :, :], action[:, 1, :], None, keep_prob_encoding, keep_prob_lstm) tf.get_variable_scope().reuse_variables() # unroll for 9 more steps for i in xrange(9): x_2, reward_2, hidden_state = ring_net.encode_compress_decode( state[:, i + 1, :, :, :], action[:, i + 2, :], hidden_state, keep_prob_encoding, keep_prob_lstm) # now collect values x_2_o.append(x_2) for i in xrange(4): x_2, reward_2, hidden_state = ring_net.encode_compress_decode( x_2, action[:, i + 11, :], hidden_state, keep_prob_encoding, keep_prob_lstm) x_2_o.append(x_2) tf.image_summary('images_gen_' + str(i), x_2) x_2_o = tf.pack(x_2_o) x_2_o = tf.transpose(x_2_o, perm=[1, 0, 2, 3, 4]) # error error = tf.nn.l2_loss(state[:, 10:15, :, :, :] - x_2_o) tf.scalar_summary('loss', error) # train (hopefuly) train_op = ring_net.train(error, 1e-5) # List of all Variables variables = tf.all_variables() # Build a saver saver = tf.train.Saver(tf.all_variables(), max_to_keep=1) # Summary op summary_op = tf.merge_all_summaries() # Start running operations on the Graph. sess = tf.Session() # init from seq 1 model print("init from " + RESTORE_DIR) saver_restore = tf.train.Saver(variables) ckpt = tf.train.get_checkpoint_state(RESTORE_DIR) saver_restore.restore(sess, ckpt.model_checkpoint_path) # Start que runner tf.train.start_queue_runners(sess=sess) # Summary op graph_def = sess.graph.as_graph_def(add_shapes=True) summary_writer = tf.train.SummaryWriter(SAVE_DIR, graph_def=graph_def) for step in xrange(500000): t = time.time() _, loss_value = sess.run([train_op, error], feed_dict={ keep_prob_encoding: 1.0, keep_prob_lstm: 1.0, input_keep_prob: 1.0 }) elapsed = time.time() - t assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 100 == 0: print("loss value at " + str(loss_value)) print("time per batch is " + str(elapsed)) summary_str = sess.run(summary_op, feed_dict={ keep_prob_encoding: 1.0, keep_prob_lstm: 1.0, input_keep_prob: 1.0 }) summary_writer.add_summary(summary_str, step) if step % 1000 == 0: checkpoint_path = os.path.join(SAVE_DIR, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print("saved to " + SAVE_DIR)
def train(iteration): """Train ring_net for a number of steps.""" with tf.Graph().as_default(): # make dynamic system #if FLAGS.system == "cannon": # k = cn.Cannon() # make inputs x = ring_net.inputs(CURRICULUM_BATCH_SIZE[iteration], CURRICULUM_SEQ[iteration]) # possible input dropout input_keep_prob = tf.placeholder("float") x_drop = tf.nn.dropout(x, input_keep_prob) # possible dropout inside keep_prob = tf.placeholder("float") # create and unrap network output_t, output_g, output_f = ring_net.unwrap(x_drop, keep_prob, CURRICULUM_SEQ[iteration]) # calc error error = ring_net.loss(x, output_t, output_g, output_f) error = tf.div(error, CURRICULUM_SEQ[iteration]) # train hopefuly train_op = ring_net.train(error, CURRICULUM_LEARNING_RATE[iteration]) # List of all Variables variables = tf.all_variables() # Build a saver saver = tf.train.Saver(tf.all_variables()) for i, variable in enumerate(variables): print '----------------------------------------------' print variable.name[:variable.name.index(':')] # Summary op summary_op = tf.merge_all_summaries() # Build an initialization operation to run below. if iteration == 0: init = tf.initialize_all_variables() # Start running operations on the Graph. sess = tf.Session() # init if this is the very time training if iteration == 0: print("init network from scratch") sess.run(init) # restore if iteration is not 0 if iteration != 0: variables_to_restore = tf.all_variables() autoencoder_variables = [variable for i, variable in enumerate(variables_to_restore) if "RNNCell" not in variable.name[:variable.name.index(':')]] rnn_variables = [variable for i, variable in enumerate(variables_to_restore) if "RNNCell" in variable.name[:variable.name.index(':')]] ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir + FLAGS.model + FLAGS.system) autoencoder_saver = tf.train.Saver(autoencoder_variables) print("restoring autoencoder part of network form " + ckpt.model_checkpoint_path) autoencoder_saver.restore(sess, ckpt.model_checkpoint_path) print("restored file from " + ckpt.model_checkpoint_path) if CURRICULUM_SEQ[iteration-1] == 1: print("init rnn part of network from scratch") rnn_init = tf.initialize_variables(rnn_variables) sess.run(rnn_init) else: rnn_saver = tf.train.Saver(rnn_variables) print("restoring rnn part of network") rnn_saver.restore(sess, ckpt.model_checkpoint_path) print("restored file from " + ckpt.model_checkpoint_path) # Start que runner tf.train.start_queue_runners(sess=sess) # Summary op graph_def = sess.graph.as_graph_def(add_shapes=True) summary_writer = tf.train.SummaryWriter(FLAGS.train_dir + FLAGS.model + FLAGS.system, graph_def=graph_def) for step in xrange(CURRICULUM_STEPS[iteration]): t = time.time() _ , loss_value = sess.run([train_op, error],feed_dict={keep_prob:0.9, input_keep_prob:.8}) elapsed = time.time() - t assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step%100 == 0: summary_str = sess.run(summary_op, feed_dict={keep_prob:0.9, input_keep_prob:.8}) summary_writer.add_summary(summary_str, step) if step%1000 == 0: checkpoint_path = os.path.join(FLAGS.train_dir + FLAGS.model + FLAGS.system, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) print("saved to " + FLAGS.train_dir + FLAGS.model + FLAGS.system) print("loss value at " + str(loss_value)) print("time per batch is " + str(elapsed))