def train(self,total_loss, lr, global_step): # all of them are tensor #total_sample = 274 yike: ok to comment out? #num_batches_per_epoch = 274/1 yike: ok to comment out? loss_averages_op = _add_loss_summaries(total_loss) # Compute gradients. with tf.control_dependencies([loss_averages_op]): #print('try...') opt = tf.train.AdamOptimizer(lr) print('toto_loss_shape: '+str(total_loss)) opt.compute_gradients(total_loss) grads = opt.compute_gradients(total_loss) #print(grads) apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) # Add histograms for trainable variables. for var in tf.trainable_variables(): tf.summary.histogram(var.op.name, var) # Add histograms for gradients. for grad, var in grads: if grad is not None: tf.summary.histogram(var.op.name + '/gradients', grad) # Track the moving averages of all trainable variables. variable_averages = tf.train.ExponentialMovingAverage(Model.MOVING_AVERAGE_DECAY, global_step) variables_averages_op = variable_averages.apply(tf.trainable_variables()) with tf.control_dependencies([apply_gradient_op, variables_averages_op]): train_op = tf.no_op(name='train') return train_op
def train_batch(total_loss, global_step, optimizer, learning_rate, moving_average_decay, update_gradient_vars): loss_averages_op = utils._add_loss_summaries(total_loss) #计算梯度 with tf.control_dependencies([loss_averages_op]): if optimizer == 'ADAGRAD': opt = tf.train.AdagradOptimizer(learning_rate) elif optimizer == 'ADADELTA': opt = tf.train.AdadeltaOptimizer(learning_rate,rho=0.9,epsilon=1e-6) elif optimizer == 'ADAM': opt = tf.train.AdamOptimizer(learning_rate,beta1=0.9,beta2=0.999,epsilon=0.1) elif optimizer == 'RMSPROP': opt = tf.train.RMSPropOptimizer(learning_rate,decay=0.9,momentum=0.9,epsilon=1.0) elif optimizer == 'MOM': opt = tf.train.MomentumOptimizer(learning_rate,0.9,use_nesterov=True) else: raise ValueError('Invalid optimization algorithm') grads = opt.compute_gradients(total_loss,update_gradient_vars) apply_gradient_op = opt.apply_gradients(grads,global_step=global_step) variable_averages = tf.train.ExponentialMovingAverage(moving_average_decay,global_step) variables_averages_op = variable_averages.apply(tf.trainable_variables()) with tf.control_dependencies([apply_gradient_op,variables_averages_op]): train_op = tf.no_op(name='train') return train_op
def captcha_train(total_loss, global_step): """ Train captcha model. Create an optimizer and apply to all trainable variables. Add moving average for all trainable variables. Args: total_loss: Total loss from loss(). global_step: Integer Variable counting the number of training steps processed. Returns: train_op: op for training. """ # Variables that affect learning rate. num_batches_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / FLAGS.batch_size decay_steps = int(num_batches_per_epoch * NUM_EPOCHS_PER_DECAY) # Decay the learning rate exponentially based on the number of steps. lr = tf.train.exponential_decay( INITIAL_LEARNING_RATE, global_step, decay_steps, LEARNING_RATE_DECAY_FACTOR, staircase=True ) tf.scalar_summary('learning_rate', lr) # Generate moving averages of all losses and associated summaries. loss_averages_op = _add_loss_summaries(total_loss) # Compute gradients. with tf.control_dependencies([loss_averages_op]): opt = tf.train.AdamOptimizer(lr) grads = opt.compute_gradients(total_loss) # Apply gradients. apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) # Add histograms for trainable variables. for var in tf.trainable_variables(): tf.histogram_summary(var.op.name, var) # Add histograms for gradients. for grad, var in grads: if grad: tf.histogram_summary(var.op.name + '/gradients', grad) # Track the moving averages of all trainable variables. variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) variables_to_average = list(set(tf.trainable_variables() + filter(lambda v: "_mean" in v.name or "_variance" in v.name, tf.all_variables()))) variables_averages_op = variable_averages.apply(variables_to_average) with tf.control_dependencies([apply_gradient_op, variables_averages_op]): train_op = tf.no_op(name='train') return train_op
def captcha_train(total_loss, global_step): """ Train captcha model. Create an optimizer and apply to all trainable variables. Add moving average for all trainable variables. Args: total_loss: Total loss from loss(). global_step: Integer Variable counting the number of training steps processed. Returns: train_op: op for training. """ # Variables that affect learning rate. num_batches_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / FLAGS.batch_size decay_steps = int(num_batches_per_epoch * NUM_EPOCHS_PER_DECAY) # Decay the learning rate exponentially based on the number of steps. lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE, global_step, decay_steps, LEARNING_RATE_DECAY_FACTOR, staircase=True) tf.scalar_summary('learning_rate', lr) # Generate moving averages of all losses and associated summaries. loss_averages_op = _add_loss_summaries(total_loss) # Compute gradients. with tf.control_dependencies([loss_averages_op]): opt = tf.train.AdamOptimizer(lr) grads = opt.compute_gradients(total_loss) # Apply gradients. apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) # Add histograms for trainable variables. for var in tf.trainable_variables(): tf.histogram_summary(var.op.name, var) # Add histograms for gradients. for grad, var in grads: if grad: tf.histogram_summary(var.op.name + '/gradients', grad) # Track the moving averages of all trainable variables. variable_averages = tf.train.ExponentialMovingAverage( MOVING_AVERAGE_DECAY, global_step) variables_to_average = list( set(tf.trainable_variables() + filter(lambda v: "_mean" in v.name or "_variance" in v.name, tf.all_variables()))) variables_averages_op = variable_averages.apply(variables_to_average) with tf.control_dependencies([apply_gradient_op, variables_averages_op]): train_op = tf.no_op(name='train') return train_op
def __init__( self, q_values, im_shape, num_channels, batch_size, actions, train_dir, optimizer, decay_steps, initial_learning_rate, learning_rate_decay_factor=.1, checkpoint_path=None): """ checkpoint_path: path to model checkpoint file logits is a function taking a tensor -> float array logits must take a single tensor argument ind_to_label is object such that ind_to_label[ind] = label """ self.im_shape = im_shape self.num_channels = num_channels self.batch_size = batch_size self.train_dir = train_dir self.g = tf.Graph() with self.g.as_default(): self.sess = tf.Session() # variables to represent images, q values, expected q_values, and action inds self.images_placeholder = tf.placeholder("float", shape=[None, im_shape[0], im_shape[1], num_channels]) self.images = self.images_placeholder #preprocess_images(self.images_placeholder) # TODO APPLY THIS NORMALIZATION ACROSS THE BATCH self.q_values = q_values(self.images, num_channels, len(actions)) # TODO FIGURE OUT HOW TO REUSE THE VARIABLES BETWEEN THESE TWO OUTPUTS self.target_values = tf.placeholder("float", shape=[None, len(actions)]) self.target_inds = tf.placeholder("float", shape=[None, len(actions)]) # generate the loss and training up self.q_learning_loss = losses.q_learning_loss(self.q_values, self.target_values, self.target_inds, "q_loss") tf.add_to_collection('losses', self.q_learning_loss) self.loss = tf.add_n(tf.get_collection('losses'), name='total_loss') loss_averages_op = _add_loss_summaries(self.loss) self.global_step = tf.Variable(0, trainable=False) # Decay the learning rate exponentially based on the number of steps. lr = tf.train.exponential_decay( INITIAL_LEARNING_RATE, self.global_step, decay_steps, learning_rate_decay_factor, staircase=True ) tf.scalar_summary('learning_rate', lr) # Compute gradients. with tf.control_dependencies([loss_averages_op]): opt = optimizer(lr) # tf.train.AdamOptimizer(lr) grads = opt.compute_gradients(self.loss) # Apply gradients. self.apply_gradient_op = opt.apply_gradients(grads, global_step=self.global_step) # Add histograms for trainable variables. for var in tf.trainable_variables(): tf.histogram_summary(var.op.name, var) # Add histograms for gradients. for grad, var in grads: if grad: tf.histogram_summary(var.op.name + '/gradients', grad) with tf.control_dependencies([self.apply_gradient_op]): self.train_op = tf.no_op(name='train') # Build the summary operation based on the TF collection of Summaries. self.summary_op = tf.merge_all_summaries() self.saver = tf.train.Saver(tf.all_variables()) self.actions = actions # Build an initialization operation to run below. init = tf.initialize_all_variables() # Start running operations on the Graph. self.sess.run(init) self.summary_writer = tf.train.SummaryWriter(train_dir, graph_def=self.sess.graph_def) if checkpoint_path is not None: # restore the model's parameters self.checkpoint_step = checkpoint_path.split('-')[-1] self.checkpoint_path = checkpoint_path self.saver.restore(self.sess, checkpoint_path)
def __init__(self, q_values, im_shape, num_channels, batch_size, actions, train_dir, optimizer, decay_steps, initial_learning_rate, learning_rate_decay_factor=.1, checkpoint_path=None): """ checkpoint_path: path to model checkpoint file logits is a function taking a tensor -> float array logits must take a single tensor argument ind_to_label is object such that ind_to_label[ind] = label """ self.im_shape = im_shape self.num_channels = num_channels self.batch_size = batch_size self.train_dir = train_dir self.g = tf.Graph() with self.g.as_default(): self.sess = tf.Session() # variables to represent images, q values, expected q_values, and action inds self.images_placeholder = tf.placeholder( "float", shape=[None, im_shape[0], im_shape[1], num_channels]) self.images = self.images_placeholder #preprocess_images(self.images_placeholder) # TODO APPLY THIS NORMALIZATION ACROSS THE BATCH self.q_values = q_values(self.images, num_channels, len(actions)) # TODO FIGURE OUT HOW TO REUSE THE VARIABLES BETWEEN THESE TWO OUTPUTS self.target_values = tf.placeholder("float", shape=[None, len(actions)]) self.target_inds = tf.placeholder("float", shape=[None, len(actions)]) # generate the loss and training up self.q_learning_loss = losses.q_learning_loss( self.q_values, self.target_values, self.target_inds, "q_loss") tf.add_to_collection('losses', self.q_learning_loss) self.loss = tf.add_n(tf.get_collection('losses'), name='total_loss') loss_averages_op = _add_loss_summaries(self.loss) self.global_step = tf.Variable(0, trainable=False) # Decay the learning rate exponentially based on the number of steps. lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE, self.global_step, decay_steps, learning_rate_decay_factor, staircase=True) tf.scalar_summary('learning_rate', lr) # Compute gradients. with tf.control_dependencies([loss_averages_op]): opt = optimizer(lr) # tf.train.AdamOptimizer(lr) grads = opt.compute_gradients(self.loss) # Apply gradients. self.apply_gradient_op = opt.apply_gradients( grads, global_step=self.global_step) # Add histograms for trainable variables. for var in tf.trainable_variables(): tf.histogram_summary(var.op.name, var) # Add histograms for gradients. for grad, var in grads: if grad: tf.histogram_summary(var.op.name + '/gradients', grad) with tf.control_dependencies([self.apply_gradient_op]): self.train_op = tf.no_op(name='train') # Build the summary operation based on the TF collection of Summaries. self.summary_op = tf.merge_all_summaries() self.saver = tf.train.Saver(tf.all_variables()) self.actions = actions # Build an initialization operation to run below. init = tf.initialize_all_variables() # Start running operations on the Graph. self.sess.run(init) self.summary_writer = tf.train.SummaryWriter( train_dir, graph_def=self.sess.graph_def) if checkpoint_path is not None: # restore the model's parameters self.checkpoint_step = checkpoint_path.split('-')[-1] self.checkpoint_path = checkpoint_path self.saver.restore(self.sess, checkpoint_path)