本文整理汇总了Python中tensorflow.rsqrt方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.rsqrt方法的具体用法?Python tensorflow.rsqrt怎么用?Python tensorflow.rsqrt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.rsqrt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: instance_norm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def instance_norm(input):
"""
Instance normalization
"""
with tf.variable_scope('instance_norm'):
num_out = input.get_shape()[-1]
scale = tf.get_variable(
'scale', [num_out],
initializer=tf.random_normal_initializer(mean=1.0, stddev=0.02))
offset = tf.get_variable(
'offset', [num_out],
initializer=tf.random_normal_initializer(mean=0.0, stddev=0.02))
mean, var = tf.nn.moments(input, axes=[1, 2], keep_dims=True)
epsilon = 1e-6
inv = tf.rsqrt(var + epsilon)
return scale * (input - mean) * inv + offset
示例2: diet_expert
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def diet_expert(x, hidden_size, params):
"""A two-layer feed-forward network with relu activation on hidden layer.
Uses diet variables.
Recomputes hidden layer on backprop to save activation memory.
Args:
x: a Tensor with shape [batch, io_size]
hidden_size: an integer
params: a diet variable HParams object.
Returns:
a Tensor with shape [batch, io_size]
"""
@fn_with_diet_vars(params)
def diet_expert_internal(x):
dim = x.get_shape().as_list()[-1]
h = tf.layers.dense(x, hidden_size, activation=tf.nn.relu, use_bias=False)
y = tf.layers.dense(h, dim, use_bias=False)
y *= tf.rsqrt(tf.to_float(dim * hidden_size))
return y
return diet_expert_internal(x)
示例3: learning_rate_factor
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def learning_rate_factor(name, step_num, hparams):
"""Compute the designated learning rate factor from hparams."""
if name == "constant":
tf.logging.info("Base learning rate: %f", hparams.learning_rate_constant)
return hparams.learning_rate_constant
elif name == "linear_warmup":
return tf.minimum(1.0, step_num / hparams.learning_rate_warmup_steps)
elif name == "linear_decay":
ret = (hparams.train_steps - step_num) / hparams.learning_rate_decay_steps
return tf.minimum(1.0, tf.maximum(0.0, ret))
elif name == "rsqrt_decay":
return tf.rsqrt(tf.maximum(step_num, hparams.learning_rate_warmup_steps))
elif name == "rsqrt_hidden_size":
return hparams.hidden_size ** -0.5
elif name == "legacy":
return legacy_learning_rate_schedule(hparams)
else:
raise ValueError("unknown learning rate factor %s" % name)
示例4: group_norm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def group_norm(x, filters=None, num_groups=8, epsilon=1e-5):
"""Group normalization as in https://arxiv.org/abs/1803.08494."""
x_shape = shape_list(x)
if filters is None:
filters = x_shape[-1]
assert len(x_shape) == 4
assert filters % num_groups == 0
# Prepare variables.
scale = tf.get_variable(
"group_norm_scale", [filters], initializer=tf.ones_initializer())
bias = tf.get_variable(
"group_norm_bias", [filters], initializer=tf.zeros_initializer())
epsilon, scale, bias = [cast_like(t, x) for t in [epsilon, scale, bias]]
# Reshape and compute group norm.
x = tf.reshape(x, x_shape[:-1] + [num_groups, filters // num_groups])
# Calculate mean and variance on heights, width, channels (not groups).
mean, variance = tf.nn.moments(x, [1, 2, 4], keep_dims=True)
norm_x = (x - mean) * tf.rsqrt(variance + epsilon)
return tf.reshape(norm_x, x_shape) * scale + bias
示例5: scaled_dot_product_attention_simple
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def scaled_dot_product_attention_simple(q, k, v, bias, name=None):
"""Scaled dot-product attention. One head. One spatial dimension.
Args:
q: a Tensor with shape [batch, length_q, depth_k]
k: a Tensor with shape [batch, length_kv, depth_k]
v: a Tensor with shape [batch, length_kv, depth_v]
bias: optional Tensor broadcastable to [batch, length_q, length_kv]
name: an optional string
Returns:
A Tensor.
"""
with tf.variable_scope(
name, default_name="scaled_dot_product_attention_simple"):
scalar = tf.rsqrt(tf.to_float(common_layers.shape_list(q)[2]))
logits = tf.matmul(q * scalar, k, transpose_b=True)
if bias is not None:
logits += bias
weights = tf.nn.softmax(logits, name="attention_weights")
if common_layers.should_generate_summaries():
tf.summary.image(
"attention", tf.expand_dims(tf.pow(weights, 0.2), 3), max_outputs=1)
return tf.matmul(weights, v)
示例6: layer_norm_compute
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def layer_norm_compute(x, epsilon, scale, bias, layer_collection=None):
"""Layer norm raw computation."""
# Save these before they get converted to tensors by the casting below
params = (scale, bias)
epsilon, scale, bias = [cast_like(t, x) for t in [epsilon, scale, bias]]
mean = tf.reduce_mean(x, axis=[-1], keepdims=True)
variance = tf.reduce_mean(
tf.squared_difference(x, mean), axis=[-1], keepdims=True)
norm_x = (x - mean) * tf.rsqrt(variance + epsilon)
output = norm_x * scale + bias
return output
示例7: layer_normalization
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def layer_normalization(self,x):
"""
x should be:[batch_size,sequence_length,d_model]
:return:
"""
filter=x.get_shape()[-1] #last dimension of x. e.g. 512
#print("layer_normalization:==================>variable_scope:","layer_normalization"+str(self.layer_index))
with tf.variable_scope("layer_normalization"+str(self.layer_index)):
# 1. normalize input by using mean and variance according to last dimension
mean=tf.reduce_mean(x,axis=-1,keepdims=True) #[batch_size,sequence_length,1]
variance=tf.reduce_mean(tf.square(x-mean),axis=-1,keepdims=True) #[batch_size,sequence_length,1]
norm_x=(x-mean)*tf.rsqrt(variance+1e-6) #[batch_size,sequence_length,d_model]
# 2. re-scale normalized input back
scale=tf.get_variable("layer_norm_scale",[filter],initializer=tf.ones_initializer) #[filter]
bias=tf.get_variable("layer_norm_bias",[filter],initializer=tf.ones_initializer) #[filter]
output=norm_x*scale+bias #[batch_size,sequence_length,d_model]
return output #[batch_size,sequence_length,d_model]
示例8: layer_norm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def layer_norm(inputs, epsilon=1e-6, dtype=None, scope=None):
"""
Layer Normalization
:param inputs: A Tensor of shape [..., channel_size]
:param epsilon: A floating number
:param dtype: An optional instance of tf.DType
:param scope: An optional string
:returns: A Tensor with the same shape as inputs
"""
with tf.variable_scope(scope, default_name="layer_norm", values=[inputs],
dtype=dtype):
channel_size = inputs.get_shape().as_list()[-1]
scale = tf.get_variable("scale", shape=[channel_size],
initializer=tf.ones_initializer())
offset = tf.get_variable("offset", shape=[channel_size],
initializer=tf.zeros_initializer())
mean = tf.reduce_mean(inputs, -1, True)
variance = tf.reduce_mean(tf.square(inputs - mean), -1, True)
norm_inputs = (inputs - mean) * tf.rsqrt(variance + epsilon)
return norm_inputs * scale + offset
示例9: layer_norm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def layer_norm(input_data,
epsilon=1e-6,
dtype=None,
scope=None):
with tf.variable_scope(scope, default_name="layer_norm"):
input_size = infer_shape(input_data)[-1]
scale = tf.get_variable("scale", shape=[input_size],
initializer=tf.ones_initializer())
bias = tf.get_variable("bias", shape=[input_size],
initializer=tf.zeros_initializer)
mean = tf.reduce_mean(input_data, -1, True)
variance = tf.reduce_mean(tf.square(input_data - mean), -1, True)
input_norm = (input_data - mean) * tf.rsqrt(variance + epsilon)
output = input_norm * scale + bias
return output
示例10: pixel_norm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def pixel_norm(x, epsilon=1e-8):
with tf.variable_scope('PixelNorm'):
return x * tf.rsqrt(tf.reduce_mean(tf.square(x), axis=1, keep_dims=True) + epsilon)
#----------------------------------------------------------------------------
# Minibatch standard deviation.
示例11: Attention
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def Attention(Q, K, V, mononotic_attention=False, prev_max_attentions=None):
'''
Args:
Q: Queries. (B, T/r, d)
K: Keys. (B, N, d)
V: Values. (B, N, d)
mononotic_attention: A boolean. At training, it is False.
prev_max_attentions: (B,). At training, it is set to None.
Returns:
R: [Context Vectors; Q]. (B, T/r, 2d)
alignments: (B, N, T/r)
max_attentions: (B, T/r)
'''
A = tf.matmul(Q, K, transpose_b=True) * tf.rsqrt(tf.to_float(hp.d))
if mononotic_attention: # for inference
key_masks = tf.sequence_mask(prev_max_attentions, hp.max_N)
reverse_masks = tf.sequence_mask(hp.max_N - hp.attention_win_size - prev_max_attentions, hp.max_N)[:, ::-1]
masks = tf.logical_or(key_masks, reverse_masks)
masks = tf.tile(tf.expand_dims(masks, 1), [1, hp.max_T, 1])
paddings = tf.ones_like(A) * (-2 ** 32 + 1) # (B, T/r, N)
A = tf.where(tf.equal(masks, False), A, paddings)
A = tf.nn.softmax(A) # (B, T/r, N)
max_attentions = tf.argmax(A, -1) # (B, T/r)
R = tf.matmul(A, V)
R = tf.concat((R, Q), -1)
alignments = tf.transpose(A, [0, 2, 1]) # (B, N, T/r)
return R, alignments, max_attentions
示例12: l2_batch_normalize
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def l2_batch_normalize(x, epsilon=1e-12, scope=None):
"""
Helper function to normalize a batch of vectors.
:param x: the input placeholder
:param epsilon: stabilizes division
:return: the batch of l2 normalized vector
"""
with tf.name_scope(scope, "l2_batch_normalize") as scope:
x_shape = tf.shape(x)
x = tf.contrib.layers.flatten(x)
x /= (epsilon + reduce_max(tf.abs(x), 1, keepdims=True))
square_sum = reduce_sum(tf.square(x), 1, keepdims=True)
x_inv_norm = tf.rsqrt(np.sqrt(epsilon) + square_sum)
x_norm = tf.multiply(x, x_inv_norm)
return tf.reshape(x_norm, x_shape, scope)
示例13: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def __call__(self, shape, dtype=None, partition_info=None):
del partition_info
dtype = self.dtype if dtype is None else dtype
std = tf.rsqrt(tf.cast(tf.reduce_prod(shape[:-1]), tf.float32) + 1e-7)
return tf.random_normal(shape, stddev=std, dtype=dtype)
示例14: BatchClipByL2norm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def BatchClipByL2norm(t, upper_bound, name=None):
"""Clip an array of tensors by L2 norm.
Shrink each dimension-0 slice of tensor (for matrix it is each row) such
that the l2 norm is at most upper_bound. Here we clip each row as it
corresponds to each example in the batch.
Args:
t: the input tensor.
upper_bound: the upperbound of the L2 norm.
name: optional name.
Returns:
the clipped tensor.
"""
assert upper_bound > 0
with tf.name_scope(values=[t, upper_bound], name=name,
default_name="batch_clip_by_l2norm") as name:
saved_shape = tf.shape(t)
batch_size = tf.slice(saved_shape, [0], [1])
t2 = tf.reshape(t, tf.concat(axis=0, values=[batch_size, [-1]]))
upper_bound_inv = tf.fill(tf.slice(saved_shape, [0], [1]),
tf.constant(1.0/upper_bound))
# Add a small number to avoid divide by 0
l2norm_inv = tf.rsqrt(tf.reduce_sum(t2 * t2, [1]) + 0.000001)
scale = tf.minimum(l2norm_inv, upper_bound_inv) * upper_bound
clipped_t = tf.matmul(tf.diag(scale), t2)
clipped_t = tf.reshape(clipped_t, saved_shape, name=name)
return clipped_t
示例15: _learning_rate_default
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rsqrt [as 别名]
def _learning_rate_default(self, multiply_by_parameter_scale):
learning_rate = tf.minimum(tf.rsqrt(step_num() + 1.0), 0.01)
if not multiply_by_parameter_scale:
learning_rate *= 0.05
return learning_rate