本文整理汇总了Python中tensorflow.compat.v1.variance_scaling_initializer方法的典型用法代码示例。如果您正苦于以下问题:Python v1.variance_scaling_initializer方法的具体用法?Python v1.variance_scaling_initializer怎么用?Python v1.variance_scaling_initializer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.variance_scaling_initializer方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: log_conv2d
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def log_conv2d(self, input_tensor, output_tensor, stride_height, stride_width,
filters, initializer, use_bias):
"""Log a conv2d call."""
if self.model == 'resnet50_v1.5':
assert stride_height == stride_width, (
'--ml_perf_compliance_logging does not support convolutions where '
'the stride height is not equal to the stride width. '
'stride_height=%d, stride_width=%d' % (stride_height, stride_width))
if isinstance(initializer, tf.truncated_normal_initializer) or (
isinstance(initializer, tf.variance_scaling_initializer) and
initializer.distribution == 'truncated_normal'):
initializer = tags.TRUNCATED_NORMAL
elif (isinstance(initializer, tf.glorot_uniform_initializer) or
initializer is None):
initializer = 'glorot_uniform'
resnet_log_helper.log_conv2d(input_tensor, output_tensor, stride_width,
filters, initializer, use_bias)
示例2: conv_kernel_initializer
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def conv_kernel_initializer(shape, dtype=None, partition_info=None):
"""Initialization for convolutional kernels.
The main difference with tf.variance_scaling_initializer is that
tf.variance_scaling_initializer uses a truncated normal with an uncorrected
standard deviation, whereas here we use a normal distribution. Similarly,
tf.initializers.variance_scaling uses a truncated normal with
a corrected standard deviation.
Args:
shape: shape of variable
dtype: dtype of variable
partition_info: unused
Returns:
an initialization for the variable
"""
del partition_info
kernel_height, kernel_width, _, out_filters = shape
fan_out = int(kernel_height * kernel_width * out_filters)
return tf.random_normal(
shape, mean=0.0, stddev=np.sqrt(2.0 / fan_out), dtype=dtype)
示例3: dense_kernel_initializer
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def dense_kernel_initializer(shape, dtype=None, partition_info=None):
"""Initialization for dense kernels.
This initialization is equal to
tf.variance_scaling_initializer(scale=1.0/3.0, mode='fan_out',
distribution='uniform').
It is written out explicitly here for clarity.
Args:
shape: shape of variable
dtype: dtype of variable
partition_info: unused
Returns:
an initialization for the variable
"""
del partition_info
init_range = 1.0 / np.sqrt(shape[1])
return tf.random_uniform(shape, -init_range, init_range, dtype=dtype)
示例4: conv2d_fixed_padding
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def conv2d_fixed_padding(inputs, filters, kernel_size, strides, data_format):
"""Strided 2-D convolution with explicit padding."""
# The padding is consistent and is based only on `kernel_size`, not on the
# dimensions of `inputs` (as opposed to using `tf.layers.conv2d` alone).
if strides > 1:
inputs = fixed_padding(inputs, kernel_size, data_format)
return tf.layers.conv2d(
inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides,
padding=('SAME' if strides == 1 else 'VALID'), use_bias=False,
kernel_initializer=tf.variance_scaling_initializer(),
data_format=data_format)
################################################################################
# ResNet block definitions.
################################################################################
示例5: conv2d_fixed_padding
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def conv2d_fixed_padding(inputs, filters, kernel_size, strides, data_format,
weight_decay):
"""Strided 2-D convolution with explicit padding."""
# The padding is consistent and is based only on `kernel_size`, not on the
# dimensions of `inputs` (as opposed to using `tf.layers.conv2d` alone).
if strides > 1:
inputs = fixed_padding(inputs, kernel_size, data_format)
if weight_decay is not None:
weight_decay = contrib_layers.l2_regularizer(weight_decay)
return tf.layers.conv2d(
inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides,
padding=('SAME' if strides == 1 else 'VALID'), use_bias=False,
kernel_initializer=tf.variance_scaling_initializer(),
kernel_regularizer=weight_decay,
data_format=data_format)
示例6: get_variable_initializer
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def get_variable_initializer(hparams):
"""Get variable initializer from hparams."""
if not hparams.initializer:
return None
mlperf_log.transformer_print(key=mlperf_log.MODEL_HP_INITIALIZER_GAIN,
value=hparams.initializer_gain,
hparams=hparams)
if not tf.executing_eagerly():
tf.logging.info("Using variable initializer: %s", hparams.initializer)
if hparams.initializer == "orthogonal":
return tf.orthogonal_initializer(gain=hparams.initializer_gain)
elif hparams.initializer == "uniform":
max_val = 0.1 * hparams.initializer_gain
return tf.random_uniform_initializer(-max_val, max_val)
elif hparams.initializer == "normal_unit_scaling":
return tf.variance_scaling_initializer(
hparams.initializer_gain, mode="fan_avg", distribution="normal")
elif hparams.initializer == "uniform_unit_scaling":
return tf.variance_scaling_initializer(
hparams.initializer_gain, mode="fan_avg", distribution="uniform")
elif hparams.initializer == "xavier":
return tf.initializers.glorot_uniform()
else:
raise ValueError("Unrecognized initializer: %s" % hparams.initializer)
示例7: conv_linear_map
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def conv_linear_map(inputs, nin, nout, bias_start, prefix):
"""Convolutional liner map.
Maps 3D tensor by last dimension.
Args:
inputs: Inputs that should be shuffled
nin: Input feature map count
nout: Output feature map count
bias_start: Bias start value
prefix: Name prefix
Returns:
tf.Tensor: Inputs with applied convolution
"""
with tf.variable_scope(prefix):
inp_shape = tf.shape(inputs)
initializer = tf.variance_scaling_initializer(
scale=1.0, mode="fan_avg", distribution="uniform")
kernel = tf.get_variable("CvK", [nin, nout], initializer=initializer)
bias_term = tf.get_variable(
"CvB", [nout], initializer=tf.constant_initializer(0.0))
mul_shape = [inp_shape[0] * inp_shape[1], nin]
res = tf.matmul(tf.reshape(inputs, mul_shape), kernel)
res = tf.reshape(res, [inp_shape[0], inp_shape[1], nout])
return res + bias_start + bias_term
# pylint: disable=useless-object-inheritance
示例8: build
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def build(self, input_shape):
"""Initialize layer weights and sublayers.
Args:
input_shape: shape of inputs
"""
in_units = input_shape[-1]
middle_units = in_units * 4
out_units = in_units * 2
init = tf.variance_scaling_initializer(
scale=1.0, mode="fan_avg", distribution="uniform")
self.first_linear = tf.keras.layers.Dense(
middle_units,
use_bias=False,
kernel_initializer=init,
name=self.prefix + "/cand1")
self.second_linear = tf.keras.layers.Dense(
out_units, kernel_initializer=init, name=self.prefix + "/cand2")
self.layer_norm = LayerNormalization()
init = tf.constant_initializer(self.init_value)
self.residual_scale = self.add_weight(
self.prefix + "/residual", [out_units], initializer=init)
super(RSU, self).build(input_shape)
示例9: _set_initializer
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def _set_initializer():
"""Set initializer used for all model variables."""
tf.get_variable_scope().set_initializer(
tf.variance_scaling_initializer(scale=1.0, mode="fan_avg"))
示例10: __init__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def __init__(self, sparsity, seed=None, dtype=tf.float32):
if sparsity < 0. or sparsity > 1.:
raise ValueError('sparsity must be in the range [0., 1.].')
self.kernel_initializer = tf.variance_scaling_initializer(seed=seed,
dtype=dtype)
self.seed = seed
self.dtype = dtype
self.sparsity = float(sparsity)
示例11: __init__
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def __init__(self,
batchnorm_training,
batchnorm_scale=True,
default_batchnorm_momentum=0.997,
default_batchnorm_epsilon=1e-5,
weight_decay=0.0001,
conv_hyperparams=None,
min_depth=8,
depth_multiplier=1):
"""Alternative tf.keras.layers interface, for use by the Keras Resnet V1.
The class is used by the Keras applications kwargs injection API to
modify the Resnet V1 Keras application with changes required by
the Object Detection API.
Args:
batchnorm_training: Bool. Assigned to Batch norm layer `training` param
when constructing `freezable_batch_norm.FreezableBatchNorm` layers.
batchnorm_scale: If True, uses an explicit `gamma` multiplier to scale
the activations in the batch normalization layer.
default_batchnorm_momentum: Float. When 'conv_hyperparams' is None,
batch norm layers will be constructed using this value as the momentum.
default_batchnorm_epsilon: Float. When 'conv_hyperparams' is None,
batch norm layers will be constructed using this value as the epsilon.
weight_decay: The weight decay to use for regularizing the model.
conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object
containing hyperparameters for convolution ops. Optionally set to `None`
to use default resnet_v1 layer builders.
min_depth: Minimum number of filters in the convolutional layers.
depth_multiplier: The depth multiplier to modify the number of filters
in the convolutional layers.
"""
self._batchnorm_training = batchnorm_training
self._batchnorm_scale = batchnorm_scale
self._default_batchnorm_momentum = default_batchnorm_momentum
self._default_batchnorm_epsilon = default_batchnorm_epsilon
self._conv_hyperparams = conv_hyperparams
self._min_depth = min_depth
self._depth_multiplier = depth_multiplier
self.regularizer = tf.keras.regularizers.l2(weight_decay)
self.initializer = tf.variance_scaling_initializer()
示例12: variance_scaling_initializer
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def variance_scaling_initializer(scale=2.0, mode='fan_in',
distribution='truncated_normal',
mean=0.0, seed=None, dtype=tf.float32):
"""Like tf.variance_scaling_initializer but supports non-zero means."""
if not dtype.is_floating:
raise TypeError('Cannot create initializer for non-floating point type.')
if mode not in ['fan_in', 'fan_out', 'fan_avg']:
raise TypeError('Unknown mode %s [fan_in, fan_out, fan_avg]' % mode)
# pylint: disable=unused-argument
def _initializer(shape, dtype=dtype, partition_info=None):
"""Initializer function."""
if not dtype.is_floating:
raise TypeError('Cannot create initializer for non-floating point type.')
# Estimating fan_in and fan_out is not possible to do perfectly, but we try.
# This is the right thing for matrix multiply and convolutions.
if shape:
fan_in = float(shape[-2]) if len(shape) > 1 else float(shape[-1])
fan_out = float(shape[-1])
else:
fan_in = 1.0
fan_out = 1.0
for dim in shape[:-2]:
fan_in *= float(dim)
fan_out *= float(dim)
if mode == 'fan_in':
# Count only number of input connections.
n = fan_in
elif mode == 'fan_out':
# Count only number of output connections.
n = fan_out
elif mode == 'fan_avg':
# Average number of inputs and output connections.
n = (fan_in + fan_out) / 2.0
if distribution == 'truncated_normal':
# To get stddev = math.sqrt(scale / n) need to adjust for truncated.
trunc_stddev = math.sqrt(1.3 * scale / n)
return tf.truncated_normal(shape, mean, trunc_stddev, dtype, seed=seed)
elif distribution == 'uniform':
# To get stddev = math.sqrt(scale / n) need to adjust for uniform.
limit = math.sqrt(3.0 * scale / n)
return tf.random_uniform(shape, mean-limit, mean+limit, dtype, seed=seed)
else:
assert 'Unexpected distribution %s.' % distribution
# pylint: enable=unused-argument
return _initializer
示例13: conv2d_fixed_padding
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import variance_scaling_initializer [as 别名]
def conv2d_fixed_padding(inputs,
filters,
kernel_size,
strides,
pruning_method='baseline',
data_format='channels_first',
weight_decay=0.,
name=None):
"""Strided 2-D convolution with explicit padding.
The padding is consistent and is based only on `kernel_size`, not on the
dimensions of `inputs` (as opposed to using `tf.layers.conv2d` alone).
Args:
inputs: Input tensor, float32 or bfloat16 of size [batch, channels, height,
width].
filters: Int specifying number of filters for the first two convolutions.
kernel_size: Int designating size of kernel to be used in the convolution.
strides: Int specifying the stride. If stride >1, the input is downsampled.
pruning_method: String that specifies the pruning method used to identify
which weights to remove.
data_format: String that specifies either "channels_first" for [batch,
channels, height,width] or "channels_last" for [batch, height, width,
channels].
weight_decay: Weight for the l2 regularization loss.
name: String that specifies name for model layer.
Returns:
The output activation tensor of size [batch, filters, height_out, width_out]
Raises:
ValueError: If the data_format provided is not a valid string.
"""
if strides > 1:
inputs = resnet_model.fixed_padding(
inputs, kernel_size, data_format=data_format)
padding = 'VALID'
else:
padding = 'SAME'
kernel_initializer = tf.variance_scaling_initializer()
kernel_regularizer = contrib_layers.l2_regularizer(weight_decay)
return sparse_conv2d(
x=inputs,
units=filters,
activation=None,
kernel_size=[kernel_size, kernel_size],
use_bias=False,
kernel_initializer=kernel_initializer,
kernel_regularizer=kernel_regularizer,
bias_initializer=None,
biases_regularizer=None,
sparsity_technique=pruning_method,
normalizer_fn=None,
strides=[strides, strides],
padding=padding,
data_format=data_format,
name=name)