本文整理匯總了Python中tensorflow.python.training.moving_averages.assign_moving_average方法的典型用法代碼示例。如果您正苦於以下問題:Python moving_averages.assign_moving_average方法的具體用法?Python moving_averages.assign_moving_average怎麽用?Python moving_averages.assign_moving_average使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.python.training.moving_averages
的用法示例。
在下文中一共展示了moving_averages.assign_moving_average方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_and_apply_batch_norm
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def create_and_apply_batch_norm(self, inp, n_features, decay, tower_setup, scope_name="bn"):
beta, gamma, moving_mean, moving_var = create_batch_norm_vars(n_features, tower_setup, scope_name)
self.n_params += 2 * n_features
if tower_setup.is_main_train_tower:
assert tower_setup.is_training
if tower_setup.is_training and not tower_setup.freeze_batchnorm:
xn, batch_mean, batch_var = tf.nn.fused_batch_norm(inp, gamma, beta, epsilon=Layer.BATCH_NORM_EPSILON,
is_training=True)
if tower_setup.is_main_train_tower:
update_op1 = moving_averages.assign_moving_average(
moving_mean, batch_mean, decay, zero_debias=False, name='mean_ema_op')
update_op2 = moving_averages.assign_moving_average(
moving_var, batch_var, decay, zero_debias=False, name='var_ema_op')
self.update_ops.append(update_op1)
self.update_ops.append(update_op2)
return xn
else:
xn = tf.nn.batch_normalization(inp, moving_mean, moving_var, beta, gamma, Layer.BATCH_NORM_EPSILON)
return xn
示例2: moving_average_update
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def moving_average_update(x, value, momentum):
"""Compute the moving average of a variable.
# Arguments
x: A `Variable`.
value: A tensor with the same shape as `x`.
momentum: The moving average momentum.
# Returns
An operation to update the variable.
"""
return moving_averages.assign_moving_average(
x, value, momentum, zero_debias=True)
# LINEAR ALGEBRA
示例3: _adaptive_max_norm
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def _adaptive_max_norm(norm, std_factor, decay, global_step, epsilon, name):
"""Find max_norm given norm and previous average."""
with vs.variable_scope(name, "AdaptiveMaxNorm", [norm]):
log_norm = math_ops.log(norm + epsilon)
def moving_average(name, value, decay):
moving_average_variable = vs.get_variable(
name, shape=value.get_shape(), dtype=value.dtype,
initializer=init_ops.zeros_initializer, trainable=False)
return moving_averages.assign_moving_average(
moving_average_variable, value, decay, zero_debias=False)
# quicker adaptation at the beginning
if global_step is not None:
n = math_ops.to_float(global_step)
decay = math_ops.minimum(decay, n / (n + 1.))
# update averages
mean = moving_average("mean", log_norm, decay)
sq_mean = moving_average("sq_mean", math_ops.square(log_norm), decay)
variance = sq_mean - math_ops.square(mean)
std = math_ops.sqrt(math_ops.maximum(epsilon, variance))
max_norms = math_ops.exp(mean + std_factor*std)
return max_norms, mean
示例4: batch_normalization
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def batch_normalization(incoming, is_training, beta=0.0, gamma=1.0, epsilon=1e-5, decay=0.9):
shape = incoming.get_shape()
dimensions_num = len(shape)
axis = list(range(dimensions_num - 1))
with tf.variable_scope('batchnorm'):
beta = tf.Variable(initial_value=tf.ones(shape=[shape[-1]]) * beta, name='beta')
gamma = tf.Variable(initial_value=tf.ones(shape=[shape[-1]]) * gamma, name='gamma')
moving_mean = tf.Variable(initial_value=tf.zeros(shape=shape[-1:]), trainable=False, name='moving_mean')
moving_variance = tf.Variable(initial_value=tf.zeros(shape=shape[-1:]), trainable=False, name='moving_variance')
def update_mean_var():
mean, variance = tf.nn.moments(incoming, axis)
update_moving_mean = moving_averages.assign_moving_average(moving_mean, mean, decay)
update_moving_variance = moving_averages.assign_moving_average(moving_variance, variance, decay)
with tf.control_dependencies([update_moving_mean, update_moving_variance]):
return tf.identity(mean), tf.identity(variance)
mean, var = tf.cond(is_training, update_mean_var, lambda: (moving_mean, moving_variance))
inference = tf.nn.batch_normalization(incoming, mean, var, beta, gamma, epsilon)
inference.set_shape(shape)
return inference
示例5: batch_normalization
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def batch_normalization(input, trainable, name, **kwargs):
input_shape = input.get_shape()
shape = input_shape.as_list()[-1::]
axis = list(range(len(input_shape) - 1))
moving_mean = tf.get_variable(shape=shape, initializer=tf.zeros_initializer, trainable=trainable, name=name + "_mean")
moving_variance = tf.get_variable(shape=shape, initializer=tf.ones_initializer, trainable=trainable, name=name + "_var")
offset = tf.get_variable(shape=shape, initializer=tf.zeros_initializer, trainable=trainable, name=name + "_bias")
scale = tf.get_variable(shape=shape, initializer=tf.ones_initializer, trainable=trainable, name=name + "_scale") if name != 'fc1' else None
mean, variance = tf.nn.moments(input, axis)
update_moving_mean = moving_averages.assign_moving_average(moving_mean, mean, BN_DECAY)
update_moving_variance = moving_averages.assign_moving_average(moving_variance, variance, BN_DECAY)
tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_mean)
tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_variance)
is_training = tf.convert_to_tensor(trainable, dtype='bool', name='is_training')
mean, variance = control_flow_ops.cond(is_training,
lambda: (mean, variance),
lambda: (moving_mean, moving_variance))
return tf.nn.batch_normalization(input, mean, variance, offset, scale, name=name, **kwargs)
示例6: update_bn_ema
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def update_bn_ema(xn, batch_mean, batch_var,
moving_mean, moving_var, decay, internal_update):
update_op1 = moving_averages.assign_moving_average(
moving_mean, batch_mean, decay, zero_debias=False,
name='mean_ema_op')
update_op2 = moving_averages.assign_moving_average(
moving_var, batch_var, decay, zero_debias=False,
name='var_ema_op')
if internal_update:
with tf.control_dependencies([update_op1, update_op2]):
return tf.identity(xn, name='output')
else:
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update_op1)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update_op2)
return tf.identity(xn, name='output')
示例7: get_output_for
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def get_output_for(self, input, phase='train', **kwargs):
if phase == 'train':
# Calculate the moments based on the individual batch.
mean, variance = tf.nn.moments(input, self.axis, shift=self.moving_mean)
# Update the moving_mean and moving_variance moments.
update_moving_mean = moving_averages.assign_moving_average(
self.moving_mean, mean, self.decay)
update_moving_variance = moving_averages.assign_moving_average(
self.moving_variance, variance, self.decay)
# Make sure the updates are computed here.
with tf.control_dependencies([update_moving_mean,
update_moving_variance]):
output = tf.nn.batch_normalization(
input, mean, variance, self.beta, self.gamma, self.epsilon)
else:
output = tf.nn.batch_normalization(
input, self.moving_mean, self.moving_variance, self.beta, self.gamma, self.epsilon)
output.set_shape(self.input_shape)
return output
示例8: batch_normalization
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def batch_normalization(self, input, name):
with tf.variable_scope(name):
bn_input_shape = input.get_shape()
moving_mean = tf.get_variable(name+'_mean', bn_input_shape[-1:] , initializer=tf.zeros_initializer, trainable=False)
moving_variance = tf.get_variable(name+'_variance', bn_input_shape[-1:] , initializer=tf.ones_initializer, trainable=False)
def mean_var_with_update():
mean, variance = tf.nn.moments(input, list(range(len(bn_input_shape) - 1)), name=name+'_moments')
with tf.control_dependencies([assign_moving_average(moving_mean, mean, self.conv_bn_decay),assign_moving_average(moving_variance, variance, self.conv_bn_decay)]):
return tf.identity(mean), tf.identity(variance)
#mean, variance = tf.cond(tf.cast(self.isTraining, tf.bool), mean_var_with_update, lambda: (moving_mean, moving_variance))
mean, variance = tf.cond(tf.cast(True, tf.bool), mean_var_with_update, lambda: (moving_mean, moving_variance))
beta = tf.get_variable(name+'_beta', bn_input_shape[-1:] , initializer=tf.zeros_initializer)
gamma = tf.get_variable(name+'_gamma', bn_input_shape[-1:] , initializer=tf.ones_initializer)
return tf.nn.batch_normalization(input, mean, variance, beta, gamma, self.conv_bn_epsilon, name+'_bn_opt')
# smooth_L1 算法
示例9: moving_average_update
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def moving_average_update(x, value, momentum):
"""Compute the moving average of a variable.
# Arguments
x: A `Variable`.
value: A tensor with the same shape as `x`.
momentum: The moving average momentum.
# Returns
An operation to update the variable.
"""
return moving_averages.assign_moving_average(
x, value, momentum, zero_debias=False)
# LINEAR ALGEBRA
示例10: vq_discrete_bottleneck
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def vq_discrete_bottleneck(x, hparams):
"""Simple vector quantized discrete bottleneck."""
tf.logging.info("Using EMA with beta = {}".format(hparams.beta))
bottleneck_size = 2**hparams.bottleneck_bits
x_shape = common_layers.shape_list(x)
x = tf.reshape(x, [-1, hparams.hidden_size])
x_means_hot, e_loss = vq_nearest_neighbor(
x, hparams)
means, ema_means, ema_count = (hparams.means, hparams.ema_means,
hparams.ema_count)
# Update the ema variables
updated_ema_count = moving_averages.assign_moving_average(
ema_count,
tf.reduce_sum(x_means_hot, axis=0),
hparams.decay,
zero_debias=False)
dw = tf.matmul(x_means_hot, x, transpose_a=True)
updated_ema_means = moving_averages.assign_moving_average(
ema_means, dw, hparams.decay, zero_debias=False)
n = tf.reduce_sum(updated_ema_count, axis=-1, keepdims=True)
updated_ema_count = (
(updated_ema_count + hparams.epsilon) /
(n + bottleneck_size * hparams.epsilon) * n)
# pylint: disable=g-no-augmented-assignment
updated_ema_means = updated_ema_means / tf.expand_dims(
updated_ema_count, axis=-1)
# pylint: enable=g-no-augmented-assignment
with tf.control_dependencies([e_loss]):
update_means = tf.assign(means, updated_ema_means)
with tf.control_dependencies([update_means]):
loss = hparams.beta * e_loss
discrete = tf.reshape(x_means_hot, x_shape[:-1] + [bottleneck_size])
return discrete, loss
示例11: vq_discrete_bottleneck
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def vq_discrete_bottleneck(x,
bottleneck_bits,
beta=0.25,
decay=0.999,
epsilon=1e-5,
soft_em=False,
num_samples=10):
"""Simple vector quantized discrete bottleneck."""
bottleneck_size = 2**bottleneck_bits
x_shape = common_layers.shape_list(x)
hidden_size = x_shape[-1]
means, ema_means, ema_count = get_vq_bottleneck(bottleneck_size, hidden_size)
x = tf.reshape(x, [-1, hidden_size])
x_means_hot, e_loss = vq_nearest_neighbor(
x, means, soft_em=soft_em, num_samples=num_samples)
# Update the ema variables
updated_ema_count = moving_averages.assign_moving_average(
ema_count,
tf.reduce_sum(
tf.reshape(x_means_hot, shape=[-1, bottleneck_size]), axis=0),
decay,
zero_debias=False)
dw = tf.matmul(x_means_hot, x, transpose_a=True)
updated_ema_means = tf.identity(moving_averages.assign_moving_average(
ema_means, dw, decay, zero_debias=False))
n = tf.reduce_sum(updated_ema_count, axis=-1, keepdims=True)
updated_ema_count = (
(updated_ema_count + epsilon) / (n + bottleneck_size * epsilon) * n)
updated_ema_means /= tf.expand_dims(updated_ema_count, axis=-1)
with tf.control_dependencies([e_loss]):
update_means = means.assign(updated_ema_means)
with tf.control_dependencies([update_means]):
loss = beta * e_loss
d = tf.reshape(x_means_hot, x_shape[:-1] + [bottleneck_size])
return d, loss
示例12: batchNormalization
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def batchNormalization(x, is_training, decay= 0.9, epsilon= 0.001, inference_only= False):
x_shape = x.get_shape()
params_shape = x_shape[-1:]
axis = list(range(len(x_shape) - 1))
beta = _get_variable('beta',
params_shape,
initializer= tf.zeros_initializer)
gamma = _get_variable('gamma',
params_shape,
initializer= tf.ones_initializer)
moving_mean = _get_variable('moving_mean',
params_shape,
initializer= tf.zeros_initializer,
trainable= False)
moving_variance = _get_variable('moving_variance',
params_shape,
initializer= tf.ones_initializer,
trainable= False)
# These ops will only be preformed when training.
mean, variance = tf.nn.moments(x, axis)
update_moving_mean = moving_averages.assign_moving_average(moving_mean,
mean, decay)
update_moving_variance = moving_averages.assign_moving_average(
moving_variance, variance, decay)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS , update_moving_mean)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS , update_moving_variance)
return tf.cond(is_training, lambda: tf.nn.batch_normalization(x, mean, variance, beta, gamma, epsilon), lambda: tf.nn.batch_normalization(x, moving_mean, moving_variance, beta, gamma, epsilon))
#return tf.contrib.layers.batch_norm(x, decay= decay, epsilon= epsilon, is_training= is_training)
# Flatten Layer
示例13: _batch_norm_without_layers
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def _batch_norm_without_layers(self, input_layer, decay, use_scale, epsilon):
"""Batch normalization on `input_layer` without tf.layers."""
# We make this function as similar as possible to the
# tf.contrib.layers.batch_norm, to minimize the differences between using
# layers and not using layers.
shape = input_layer.shape
num_channels = shape[3] if self.data_format == 'NHWC' else shape[1]
beta = self.get_variable('beta', [num_channels], tf.float32, tf.float32,
initializer=tf.zeros_initializer())
if use_scale:
gamma = self.get_variable('gamma', [num_channels], tf.float32,
tf.float32, initializer=tf.ones_initializer())
else:
gamma = tf.constant(1.0, tf.float32, [num_channels])
# For moving variables, we use tf.get_variable instead of self.get_variable,
# since self.get_variable returns the result of tf.cast which we cannot
# assign to.
moving_mean = tf.get_variable('moving_mean', [num_channels],
tf.float32,
initializer=tf.zeros_initializer(),
trainable=False)
moving_variance = tf.get_variable('moving_variance', [num_channels],
tf.float32,
initializer=tf.ones_initializer(),
trainable=False)
if self.phase_train:
bn, batch_mean, batch_variance = tf.nn.fused_batch_norm(
input_layer, gamma, beta, epsilon=epsilon,
data_format=self.data_format, is_training=True)
mean_update = moving_averages.assign_moving_average(
moving_mean, batch_mean, decay=decay, zero_debias=False)
variance_update = moving_averages.assign_moving_average(
moving_variance, batch_variance, decay=decay, zero_debias=False)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, mean_update)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, variance_update)
else:
bn, _, _ = tf.nn.fused_batch_norm(
input_layer, gamma, beta, mean=moving_mean,
variance=moving_variance, epsilon=epsilon,
data_format=self.data_format, is_training=False)
return bn
示例14: __call__
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def __call__(self, input_layer, epsilon=1e-5, decay=0.9, name="batch_norm",
in_dim=None, phase=Phase.train):
shape = input_layer.shape
shp = in_dim or shape[-1]
with tf.variable_scope(name) as scope:
self.mean = self.variable('mean', [shp], init=tf.constant_initializer(0.), train=False)
self.variance = self.variable('variance', [shp], init=tf.constant_initializer(1.0), train=False)
self.gamma = self.variable("gamma", [shp], init=tf.random_normal_initializer(1., 0.02))
self.beta = self.variable("beta", [shp], init=tf.constant_initializer(0.))
if phase == Phase.train:
mean, variance = tf.nn.moments(input_layer.tensor, [0, 1, 2])
mean.set_shape((shp,))
variance.set_shape((shp,))
update_moving_mean = moving_averages.assign_moving_average(self.mean, mean, decay)
update_moving_variance = moving_averages.assign_moving_average(self.variance, variance, decay)
with tf.control_dependencies([update_moving_mean, update_moving_variance]):
normalized_x = tf.nn.batch_norm_with_global_normalization(
input_layer.tensor, mean, variance, self.beta, self.gamma, epsilon,
scale_after_normalization=True)
else:
normalized_x = tf.nn.batch_norm_with_global_normalization(
input_layer.tensor, self.mean, self.variance,
self.beta, self.gamma, epsilon,
scale_after_normalization=True)
return input_layer.with_tensor(normalized_x, parameters=self.vars)
示例15: _adaptive_max_norm
# 需要導入模塊: from tensorflow.python.training import moving_averages [as 別名]
# 或者: from tensorflow.python.training.moving_averages import assign_moving_average [as 別名]
def _adaptive_max_norm(norm, std_factor, decay, global_step, epsilon, name):
"""Find max_norm given norm and previous average."""
with vs.variable_scope(name, "AdaptiveMaxNorm", [norm]):
log_norm = math_ops.log(norm + epsilon)
def moving_average(name, value, decay):
moving_average_variable = vs.get_variable(
name,
shape=value.get_shape(),
dtype=value.dtype,
initializer=init_ops.zeros_initializer(),
trainable=False)
return moving_averages.assign_moving_average(
moving_average_variable, value, decay, zero_debias=False)
# quicker adaptation at the beginning
if global_step is not None:
n = math_ops.cast(global_step, dtypes.float32)
decay = math_ops.minimum(decay, n / (n + 1.))
# update averages
mean = moving_average("mean", log_norm, decay)
sq_mean = moving_average("sq_mean", math_ops.square(log_norm), decay)
variance = sq_mean - math_ops.square(mean)
std = math_ops.sqrt(math_ops.maximum(epsilon, variance))
max_norms = math_ops.exp(mean + std_factor * std)
return max_norms, mean