我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.moving_average_variables()。
def __init__(self, checkpoint_path): layers = 50 num_blocks = [3, 4, 6, 3] self.inference = lambda images, is_train : inference(images, is_training=is_train, num_classes=NUM_AGES*2, num_blocks=num_blocks, bottleneck=True) self.x = tf.placeholder(tf.uint8, shape=(256,256,3), name='input_image') self.crops = fixed_crops(self.x) self.logits = self.inference(self.crops, is_train=False) self.pred = tf.nn.softmax(self.logits, name='prediction') # Restore saved weights restore_variables = tf.trainable_variables() \ + tf.moving_average_variables() self.saver = tf.train.Saver(restore_variables) self.sess = tf.Session() self.saver.restore(self.sess, checkpoint_path) #self.sess.run(tf.initialize_variables([var for var \ # in tf.all_variables() if var not in restore_variables]))
def variables_to_restore(self, moving_avg_variables=None): """""" name_map = {} if moving_avg_variables is None: moving_avg_variables = tf.trainable_variables() moving_avg_variables += tf.moving_average_variables() # Remove duplicates moving_avg_variables = set(moving_avg_variables) # Collect all the variables with moving average, for v in moving_avg_variables: name_map[self.average_name(v)] = v # Make sure we restore variables without moving average as well. for v in list(set(tf.all_variables()) - moving_avg_variables): if v.op.name not in name_map: name_map[v.op.name] = v return name_map #===============================================================
def testCreateVariables(self): height, width = 3, 3 with self.test_session(): images = tf.random_uniform((5, height, width, 3), seed=1) ops.batch_norm(images) beta = variables.get_variables_by_name('beta')[0] self.assertEquals(beta.op.name, 'BatchNorm/beta') gamma = variables.get_variables_by_name('gamma') self.assertEquals(gamma, []) moving_mean = tf.moving_average_variables()[0] moving_variance = tf.moving_average_variables()[1] self.assertEquals(moving_mean.op.name, 'BatchNorm/moving_mean') self.assertEquals(moving_variance.op.name, 'BatchNorm/moving_variance')
def testCreateVariablesWithScale(self): height, width = 3, 3 with self.test_session(): images = tf.random_uniform((5, height, width, 3), seed=1) ops.batch_norm(images, scale=True) beta = variables.get_variables_by_name('beta')[0] gamma = variables.get_variables_by_name('gamma')[0] self.assertEquals(beta.op.name, 'BatchNorm/beta') self.assertEquals(gamma.op.name, 'BatchNorm/gamma') moving_mean = tf.moving_average_variables()[0] moving_variance = tf.moving_average_variables()[1] self.assertEquals(moving_mean.op.name, 'BatchNorm/moving_mean') self.assertEquals(moving_variance.op.name, 'BatchNorm/moving_variance')
def testCreateVariablesWithoutCenterWithScale(self): height, width = 3, 3 with self.test_session(): images = tf.random_uniform((5, height, width, 3), seed=1) ops.batch_norm(images, center=False, scale=True) beta = variables.get_variables_by_name('beta') self.assertEquals(beta, []) gamma = variables.get_variables_by_name('gamma')[0] self.assertEquals(gamma.op.name, 'BatchNorm/gamma') moving_mean = tf.moving_average_variables()[0] moving_variance = tf.moving_average_variables()[1] self.assertEquals(moving_mean.op.name, 'BatchNorm/moving_mean') self.assertEquals(moving_variance.op.name, 'BatchNorm/moving_variance')
def testCreateVariablesWithoutCenterWithoutScale(self): height, width = 3, 3 with self.test_session(): images = tf.random_uniform((5, height, width, 3), seed=1) ops.batch_norm(images, center=False, scale=False) beta = variables.get_variables_by_name('beta') self.assertEquals(beta, []) gamma = variables.get_variables_by_name('gamma') self.assertEquals(gamma, []) moving_mean = tf.moving_average_variables()[0] moving_variance = tf.moving_average_variables()[1] self.assertEquals(moving_mean.op.name, 'BatchNorm/moving_mean') self.assertEquals(moving_variance.op.name, 'BatchNorm/moving_variance')
def testMovingAverageVariables(self): height, width = 3, 3 with self.test_session(): images = tf.random_uniform((5, height, width, 3), seed=1) ops.batch_norm(images, scale=True) moving_mean = tf.moving_average_variables()[0] moving_variance = tf.moving_average_variables()[1] self.assertEquals(moving_mean.op.name, 'BatchNorm/moving_mean') self.assertEquals(moving_variance.op.name, 'BatchNorm/moving_variance')
def _setup_model_loss(self, inputs, labels, validation_inputs, validation_labels, is_chief, task_id, num_workers, is_training, scope, initial_lr=0.1, reuse=None, global_step=None, num_replicas_to_aggregate=-1): validation_metric = [] validation_metric_tmp = [[] for _, _ in self.validation_metrics_def] self.learning_rate = tf.placeholder( tf.float32, shape=[], name="learning_rate_placeholder") losses, total_loss = self._tower_loss( scope, self.model, inputs, labels, is_training, reuse, is_classification=True) val_total_loss = self._tower_loss( scope, self.model, validation_inputs, validation_labels, False, True, is_classification=True) for i, (_, metric_function) in enumerate(self.validation_metrics_def): metric_score = metric_function( validation_labels, tf.argmax(self.validation_predictions, 1)) validation_metric_tmp[i].append(metric_score) for i, (_, _) in enumerate(self.validation_metrics_def): validation_metric.append(sum(validation_metric_tmp[i])) validation_metric.append(val_total_loss) if is_chief: loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') loss_averages_op = loss_averages.apply(losses + [total_loss]) with tf.control_dependencies([loss_averages_op]): total_loss = tf.identity(total_loss) exp_moving_averager = tf.train.ExponentialMovingAverage( self.cnf.get('mv_decay', 0.9), global_step) variables_to_average = ( tf.trainable_variables() + tf.moving_average_variables()) # Create synchronous replica optimizer. learning_rate = self.lr_policy.batch_update(initial_lr, 0) opt = self._optimizer(learning_rate, optname=self.cnf.get( 'optname', 'momentum'), **self.cnf.get('opt_kwargs', {'decay': 0.9})) opt = tf.train.SyncReplicasOptimizer(opt, replicas_to_aggregate=num_replicas_to_aggregate, total_num_replicas=num_workers, variable_averages=exp_moving_averager, variables_to_average=variables_to_average) return total_loss, opt, validation_metric