當前位置: 首頁>>代碼示例>>Python>>正文


Python tensorflow.random方法代碼示例

本文整理匯總了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") 
開發者ID:TUMFTM,項目名稱:CameraRadarFusionNet,代碼行數:19,代碼來源:helpers.py

示例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) 
開發者ID:google,項目名稱:compare_gan,代碼行數:27,代碼來源:tpu_random.py

示例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) 
開發者ID:google,項目名稱:compare_gan,代碼行數:8,代碼來源:tpu_random.py

示例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]) 
開發者ID:google,項目名稱:compare_gan,代碼行數:29,代碼來源:tpu_random.py

示例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) 
開發者ID:google,項目名稱:compare_gan,代碼行數:29,代碼來源:tpu_random.py

示例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) 
開發者ID:google,項目名稱:compare_gan,代碼行數:10,代碼來源:tpu_random.py


注:本文中的tensorflow.random方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。