本文整理汇总了Python中tensorflow.python.ops.init_ops.Initializer方法的典型用法代码示例。如果您正苦于以下问题:Python init_ops.Initializer方法的具体用法?Python init_ops.Initializer怎么用?Python init_ops.Initializer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.ops.init_ops
的用法示例。
在下文中一共展示了init_ops.Initializer方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _getter_kwargs
# 需要导入模块: from tensorflow.python.ops import init_ops [as 别名]
# 或者: from tensorflow.python.ops.init_ops import Initializer [as 别名]
def _getter_kwargs(self, instance):
defaults = instance._parameter_config.get(self.name, {})
kwargs = {}
for key in ['initial', 'shape']:
value = getattr(self, key, None)
if value is None:
try:
value = defaults[key]
except KeyError:
raise KeyError(
'Parameter {} does not specify a configuration for {}.'
.format(self.name, key))
kwargs[key] = value
kwargs['name'] = self.name
init = kwargs.pop('initial')
from tensorflow.python.ops.init_ops import Initializer
if init is not None and not isinstance(init, Initializer):
init = tf.constant_initializer(
value=init, dtype=self.dtype, verify_shape=True)
kwargs['initializer'] = init
kwargs['dtype'] = self.dtype
kwargs['trainable'] = self.trainable
return kwargs
示例2: linear
# 需要导入模块: from tensorflow.python.ops import init_ops [as 别名]
# 或者: from tensorflow.python.ops.init_ops import Initializer [as 别名]
def linear(input: tf.Tensor,
output_size: int,
weight_initializer: Optional[Initializer] = None,
bias_initializer: Optional[Initializer] = None,
name: str = "linear") -> tf.Tensor:
"""
Apply a linear transformation to a tensor.
Parameters
----------
input: tf.Tensor
The tensor which should be linearly transformed
output_size: int
The desired output size of the linear transformation
weight_initializer: tf.Initializer, optional
A custom initializer for the weight matrix of the linear transformation
bias_initializer: tf.Initializer, optional
A custom initializer for the bias vector of the linear transformation
name: str, optional
A name for the operation (default "linear")
Returns
-------
tf.Tensor
The linearly transformed input tensor
"""
shape = input.get_shape().as_list()
with tf.variable_scope(name):
weights = tf.get_variable(name="weights",
shape=[shape[-1], output_size],
dtype=tf.float32,
initializer=weight_initializer)
bias = tf.get_variable(name="bias",
shape=[output_size],
initializer=bias_initializer)
return tf.matmul(input, weights) + bias
示例3: time_distributed_linear
# 需要导入模块: from tensorflow.python.ops import init_ops [as 别名]
# 或者: from tensorflow.python.ops.init_ops import Initializer [as 别名]
def time_distributed_linear(inputs: tf.Tensor,
output_size: int,
weight_initializer: Optional[Initializer] = None,
bias_initializer: Optional[Initializer] = None,
name: str = "time_dist_linear") -> tf.Tensor:
"""
Applies the same linear transformation to all time steps of a sequence.
Parameters
----------
inputs: tf.Tensor
The input sequences, of shape [max_time, batch_size, num_features]
output_size: int
The desired number of features in the output sequences
weight_initializer: tf.Initializer, optional
A custom initializer for the weight matrix of the linear transformation
bias_initializer: tf.Initializer, optional
A custom initializer for the bias vector of the linear transformation
name: str, optional
A name for the operation (default "time_dist_linear")
Returns
-------
tf.Tensor
The linearly transformed input sequences, of shape [max_time, batch_size, output_size]
"""
max_time, batch_size, _ = tf.unstack(tf.shape(inputs))
static_shape = inputs.shape.as_list()
with tf.variable_scope(name):
result = flatten_time(inputs)
result = linear(result,
output_size=output_size,
weight_initializer=weight_initializer,
bias_initializer=bias_initializer)
result = restore_time(result, max_time, batch_size, output_size)
result.set_shape([static_shape[0], static_shape[1], output_size])
return result
示例4: get_variable
# 需要导入模块: from tensorflow.python.ops import init_ops [as 别名]
# 或者: from tensorflow.python.ops.init_ops import Initializer [as 别名]
def get_variable(self,
var_store,
name,
shape=None,
dtype=None,
initializer=None,
regularizer=None,
reuse=None,
trainable=True,
collections=None,
caching_device=None,
partitioner=None,
validate_shape=True,
use_resource=None,
custom_getter=None,):
"""Gets an existing variable with this name or create a new one."""
if regularizer is None:
regularizer = self._regularizer
if caching_device is None:
caching_device = self._caching_device
if partitioner is None:
partitioner = self._partitioner
if custom_getter is None:
custom_getter = self._custom_getter
if reuse is None:
reuse = self._reuse
full_name = self.name + "/" + name if self.name else name
# Variable names only depend on variable_scope (full_name here),
# not name_scope, so we reset it below for the time of variable creation.
with ops.name_scope(None):
# Check that `initializer` dtype and `dtype` are consistent before
# replacing them with defaults.
if (dtype is not None and initializer is not None and
not callable(initializer)):
init_dtype = ops.convert_to_tensor(initializer).dtype.base_dtype
if init_dtype != dtype:
raise ValueError("Initializer type '%s' and explicit dtype '%s' "
"don't match." % (init_dtype, dtype))
if initializer is None:
initializer = self._initializer
if dtype is None:
dtype = self._dtype
if use_resource is None:
use_resource = self._use_resource
return var_store.get_variable(
full_name, shape=shape, dtype=dtype, initializer=initializer,
regularizer=regularizer, reuse=reuse, trainable=trainable,
collections=collections, caching_device=caching_device,
partitioner=partitioner, validate_shape=validate_shape,
use_resource=use_resource, custom_getter=custom_getter)
示例5: conv2d
# 需要导入模块: from tensorflow.python.ops import init_ops [as 别名]
# 或者: from tensorflow.python.ops.init_ops import Initializer [as 别名]
def conv2d(input: tf.Tensor,
output_dim: int,
kernel_width: int = 5,
kernel_height: int = 5,
horizontal_stride: int = 2,
vertical_stride: int = 2,
weight_initializer: Optional[Initializer] = None,
bias_initializer: Optional[Initializer] = None,
name: str = "conv2d"):
"""
Apply a 2D-convolution to a tensor.
Parameters
----------
input: tf.Tensor
The tensor to which the convolution should be applied. Must be of shape [batch_size, height, width, channels]
output_dim: int
The number of convolutional filters
kernel_width: int, optional
The width of the convolutional filters (default 5)
kernel_height: int, optional
The height of the convolutional filters (default 5)
horizontal_stride: int, optional
The horizontal stride of the convolutional filters (default 2)
vertical_stride: int, optional
The vertical stride of the convolutional filters (default 2)
weight_initializer: tf.Initializer, optional
A custom initializer for the weight matrices of the filters
bias_initializer: tf.Initializer, optional
A custom initializer for the bias vectors of the filters
name: str, optional
A name for the operation (default "conv2d")
Returns
-------
tf.Tensor
The result of applying a 2D-convolution to the input tensor.
"""
shape = input.get_shape().as_list()
with tf.variable_scope(name):
weights = tf.get_variable(name="weights",
shape=[kernel_height, kernel_width, shape[-1], output_dim],
initializer=weight_initializer)
bias = tf.get_variable(name="bias",
shape=[output_dim],
initializer=bias_initializer)
conv = tf.nn.conv2d(input,
filter=weights,
strides=[1, vertical_stride, horizontal_stride, 1],
padding='SAME')
conv = tf.nn.bias_add(conv, bias)
return conv
示例6: deconv2d
# 需要导入模块: from tensorflow.python.ops import init_ops [as 别名]
# 或者: from tensorflow.python.ops.init_ops import Initializer [as 别名]
def deconv2d(input: tf.Tensor,
output_shape: Sequence[Union[int, tf.Tensor]],
kernel_width: int = 5,
kernel_height: int = 5,
horizontal_stride: int = 2,
vertical_stride: int = 2,
weight_initializer: Optional[Initializer] = None,
bias_initializer: Optional[Initializer] = None,
name: str = "deconv2d"):
"""
Applies a 2D-deconvolution to a tensor.
Parameters
----------
input: tf.Tensor
The tensor to which a 2D-deconvolution should be applied. Must be of shape [batch_size, height, width, channels]
output_shape: list of int or tf.Tensor
The desired output shape.
kernel_width: int, optional
The width of the convolutional filters (default 5)
kernel_height: int, optional
The height of the convolutional filters (default 5)
horizontal_stride: int, optional
The horizontal stride of the convolutional filters (default 2)
vertical_stride: int, optional
The vertical stride of the convolutional filters (default 2)
weight_initializer: tf.Initializer, optional
A custom initializer for the weight matrices of the filters
bias_initializer: tf.Initializer, optional
A custom initializer for the bias vectors of the filters
name: str, optional
A name for the operation (default "deconv2d")
Returns
-------
tf.Tensor
The result of applying a 2D-deconvolution to the input tensor
"""
shape = input.get_shape().as_list()
with tf.variable_scope(name):
# filter : [height, width, output_channels, in_channels]
weights = tf.get_variable(name="weights",
shape=[kernel_height, kernel_width, output_shape[-1], shape[-1]],
initializer=weight_initializer)
biases = tf.get_variable(name="bias",
shape=[output_shape[-1]],
initializer=bias_initializer)
deconv = tf.nn.conv2d_transpose(input,
filter=weights,
output_shape=output_shape,
strides=[1, vertical_stride, horizontal_stride, 1])
deconv = tf.nn.bias_add(deconv, biases)
deconv.set_shape([None] + output_shape[1:])
return deconv
示例7: get_variable
# 需要导入模块: from tensorflow.python.ops import init_ops [as 别名]
# 或者: from tensorflow.python.ops.init_ops import Initializer [as 别名]
def get_variable(self,
var_store,
name,
shape=None,
dtype=None,
initializer=None,
regularizer=None,
reuse=None,
trainable=True,
collections=None,
caching_device=None,
partitioner=None,
validate_shape=True,
use_resource=None,
custom_getter=None,
constraint=None):
"""Gets an existing variable with this name or create a new one."""
if regularizer is None:
regularizer = self._regularizer
if caching_device is None:
caching_device = self._caching_device
if partitioner is None:
partitioner = self._partitioner
if custom_getter is None:
custom_getter = self._custom_getter
if context.in_graph_mode():
if reuse is None:
reuse = self._reuse
if use_resource is None:
use_resource = self._use_resource
else:
reuse = AUTO_REUSE
use_resource = True
full_name = self.name + "/" + name if self.name else name
# Variable names only depend on variable_scope (full_name here),
# not name_scope, so we reset it below for the time of variable creation.
with ops.name_scope(None):
# Check that `initializer` dtype and `dtype` are consistent before
# replacing them with defaults.
if (dtype is not None and initializer is not None and
not callable(initializer)):
init_dtype = ops.convert_to_tensor(initializer).dtype.base_dtype
if init_dtype != dtype:
raise ValueError("Initializer type '%s' and explicit dtype '%s' "
"don't match." % (init_dtype, dtype))
if initializer is None:
initializer = self._initializer
if constraint is None:
constraint = self._constraint
if dtype is None:
dtype = self._dtype
return var_store.get_variable(
full_name, shape=shape, dtype=dtype, initializer=initializer,
regularizer=regularizer, reuse=reuse, trainable=trainable,
collections=collections, caching_device=caching_device,
partitioner=partitioner, validate_shape=validate_shape,
use_resource=use_resource, custom_getter=custom_getter,
constraint=constraint)
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:61,代码来源:variable_scope.py