本文整理汇总了Python中tensorflow.random方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.random方法的具体用法?Python tensorflow.random怎么用?Python tensorflow.random使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.random方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize_seed
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random [as 别名]
def initialize_seed(seed=0):
"""
This makes experiments more comparable by
forcing the random number generator to produce
the same numbers in each run
"""
random.seed(a=seed)
numpy.random.seed(seed)
if hasattr(tf, 'set_random_seed'):
tf.set_random_seed(seed)
elif hasattr(tf.random, 'set_random_seed'):
tf.random.set_random_seed(seed)
elif hasattr(tf.random, 'set_seed'):
tf.random.set_seed(seed)
else:
raise AttributeError("Could not set seed for TensorFlow")
示例2: add_random_offset_to_features
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random [as 别名]
def add_random_offset_to_features(dataset, start=1):
"""Add a random offset to the dataset.
Args:
dataset: `tf.data.Dataset` object that contains tuples (features, labels),
where `features` is a Python dictionary.
start: A starting value for the global offset. Optional.
Returns:
A new `tf.data.Dataset` object with a extra feature for the random offset.
"""
dataset = dataset.apply(tf.data.experimental.enumerate_dataset(start=start))
def map_fn(offset, data):
offset = tf.cast(offset, tf.int32)
if isinstance(data, tuple) and len(data) == 2 and isinstance(data[0], dict):
# Data is a tuple (features, labels) as expected by the Estimator
# interface.
logging.info("Passing random offset: %s with data %s.", offset, data)
features, labels = data
features[_RANDOM_OFFSET_FEATURE_KEY] = offset
return features, labels
raise ValueError("Data in dataset must be a tuple (features, labels) and "
"features must be a Python dictionary. data was {}".format(
data))
return dataset.map(map_fn)
示例3: set_random_offset_from_features
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random [as 别名]
def set_random_offset_from_features(features):
"""Set the global random offset from the random offset feature."""
# Take the first index in case the TPU core got multiple examples.
global _RANDOM_OFFSET_TENSOR
_RANDOM_OFFSET_TENSOR = features.pop(_RANDOM_OFFSET_FEATURE_KEY)[0]
logging.info("Got global random offset: %s", _RANDOM_OFFSET_TENSOR)
示例4: _get_seed
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random [as 别名]
def _get_seed(name=None):
"""Get a deterministic random seed for stateless generators.
Args:
name: Name of the operation that will use the seed. If None a unique name
will be determined.
Returns:
An integer`Tensor` of shape (2,) with the seed for this op and the global
random offset.
"""
if _RANDOM_OFFSET_TENSOR is None:
raise ValueError("_RANDOM_OFFSET_TENSOR is None. Did you call "
"set_random_offset_from_features() in your model_fn?")
# Get a seed from the hash name of a dummy operation. This seed will only
# depend on the name of the operation (incl. the scope name). It will be
# unique within the graph and only change if the name of operation changes.
with tf.name_scope("dummy_for_seed"):
dummy_op = tf.no_op(name)
# Using SHA-512 gives us a non-negative and uniformly distributed seed in the
# interval [0, 2**512). This is consistent with TensorFlow, as TensorFlow
# operations internally use the residue of the given seed modulo `2**31 - 1`
# (see`tensorflow/python/framework/random_seed.py`).
op_seed = int(hashlib.sha512(dummy_op.name.encode("utf-8")).hexdigest(), 16)
op_seed = tf.constant(op_seed % (2**31 - 1))
logging.info("Using op_seed %s for operation %s.", op_seed, dummy_op.name)
return tf.stack([op_seed, _RANDOM_OFFSET_TENSOR])
示例5: uniform
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random [as 别名]
def uniform(shape, name=None):
"""Outputs pseudorandom random values from a uniform distribution.
If the _RANDOM_OFFSET_TENSOR is set these output is deterministic based on the
seed and the `name` of this operation. If `name` is None this will use the
index in the graph instead.
There is no `dtype` parameter since the underlying
tf.contrib.stateless.stateless_random_uniform only supports tf.half,
tf.float32 and tf.float64 and we do not care about tf.half and tf.float64.
Patches welcome.
Args:
shape: A Tensor. Must be one of the following types: int32, int64.
The shape of the output tensor.
name: A name for the operation (optional).
Returns:
A Tensor.
"""
if _RANDOM_OFFSET_TENSOR is None:
logging.warning("No global random offset set, falling back to "
"un-deterministic pseudorandom numbers for operation %s.",
name)
return tf.random.uniform(shape, name=name)
return tf.contrib.stateless.stateless_random_uniform(
shape=shape, seed=_get_seed(name), name=name)
示例6: normal
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random [as 别名]
def normal(shape, name=None):
if _RANDOM_OFFSET_TENSOR is None:
logging.warning("No global random offset set, falling back to "
"un-deterministic pseudorandom numbers for operation %s.",
name)
return tf.random.normal(shape, name=name)
return tf.contrib.stateless.stateless_random_normal(
shape=shape, seed=_get_seed(name), name=name)