0


深入浅出TensorFlow2函数——tf.random.uniform

分类目录:《深入浅出TensorFlow2函数》总目录


绘制

  1. shape

个来自每个给定均匀分布的样本。

语法

  1. tf.random.uniform(
  2. shape,
  3. minval=0,
  4. maxval=None,
  5. dtype=tf.dtypes.float32,
  6. seed=None,
  7. name=None
  8. )

参数

  • shape:输出张量的形状,为一个一维整数张量或Python数组。
  • minval :要生成的随机值范围的下限(含),默认值为0
  • minval :要生成的随机值范围的上限(不含),默认值为1
  • dtype:输出的浮点类型:float16bfloat16float32float64,默认为float32
  • seed:[int] 用于为创建分布的随机种子。可参考tf.random.set_seed
  • name:[可选] 操作的名称。

返回值

用均匀分布值填充的指定形状的张量。

实例

  1. tf.random.uniform(shape=[2])
  2. tf.random.uniform(shape=[], minval=-1., maxval=0.)
  3. tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)

函数实现

  1. @tf_export("random.uniform", v1=["random.uniform", "random_uniform"])
  2. @dispatch.add_dispatch_support
  3. @deprecation.deprecated_endpoints("random_uniform")
  4. def random_uniform(shape,
  5. minval=0,
  6. maxval=None,
  7. dtype=dtypes.float32,
  8. seed=None,
  9. name=None):
  10. """Outputs random values from a uniform distribution.
  11. The generated values follow a uniform distribution in the range
  12. `[minval, maxval)`. The lower bound `minval` is included in the range, while
  13. the upper bound `maxval` is excluded.
  14. For floats, the default range is `[0, 1)`. For ints, at least `maxval` must
  15. be specified explicitly.
  16. In the integer case, the random integers are slightly biased unless
  17. `maxval - minval` is an exact power of two. The bias is small for values of
  18. `maxval - minval` significantly smaller than the range of the output (either
  19. `2**32` or `2**64`).
  20. Examples:
  21. >>> tf.random.uniform(shape=[2])
  22. <tf.Tensor: shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)>
  23. >>> tf.random.uniform(shape=[], minval=-1., maxval=0.)
  24. <tf.Tensor: shape=(), dtype=float32, numpy=-...>
  25. >>> tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)
  26. <tf.Tensor: shape=(), dtype=int64, numpy=...>
  27. The `seed` argument produces a deterministic sequence of tensors across
  28. multiple calls. To repeat that sequence, use `tf.random.set_seed`:
  29. >>> tf.random.set_seed(5)
  30. >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  31. <tf.Tensor: shape=(), dtype=int32, numpy=2>
  32. >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  33. <tf.Tensor: shape=(), dtype=int32, numpy=0>
  34. >>> tf.random.set_seed(5)
  35. >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  36. <tf.Tensor: shape=(), dtype=int32, numpy=2>
  37. >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  38. <tf.Tensor: shape=(), dtype=int32, numpy=0>
  39. Without `tf.random.set_seed` but with a `seed` argument is specified, small
  40. changes to function graphs or previously executed operations will change the
  41. returned value. See `tf.random.set_seed` for details.
  42. Args:
  43. shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
  44. minval: A Tensor or Python value of type `dtype`, broadcastable with
  45. `shape` (for integer types, broadcasting is not supported, so it needs to
  46. be a scalar). The lower bound on the range of random values to generate
  47. (inclusive). Defaults to 0.
  48. maxval: A Tensor or Python value of type `dtype`, broadcastable with
  49. `shape` (for integer types, broadcasting is not supported, so it needs to
  50. be a scalar). The upper bound on the range of random values to generate
  51. (exclusive). Defaults to 1 if `dtype` is floating point.
  52. dtype: The type of the output: `float16`, `bfloat16`, `float32`, `float64`,
  53. `int32`, or `int64`. Defaults to `float32`.
  54. seed: A Python integer. Used in combination with `tf.random.set_seed` to
  55. create a reproducible sequence of tensors across multiple calls.
  56. name: A name for the operation (optional).
  57. Returns:
  58. A tensor of the specified shape filled with random uniform values.
  59. Raises:
  60. ValueError: If `dtype` is integral and `maxval` is not specified.
  61. """
  62. dtype = dtypes.as_dtype(dtype)
  63. accepted_dtypes = (dtypes.float16, dtypes.bfloat16, dtypes.float32,
  64. dtypes.float64, dtypes.int32, dtypes.int64)
  65. if dtype not in accepted_dtypes:
  66. raise ValueError(
  67. f"Argument `dtype` got invalid value {dtype}. Accepted dtypes are "
  68. f"{accepted_dtypes}.")
  69. if maxval is None:
  70. if dtype.is_integer:
  71. raise ValueError("Must specify maxval for integer dtype %r" % dtype)
  72. maxval = 1
  73. with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) as name:
  74. shape = tensor_util.shape_tensor(shape)
  75. # In case of [0,1) floating results, minval and maxval is unused. We do an
  76. # `is` comparison here since this is cheaper than isinstance or __eq__.
  77. minval_is_zero = isinstance(minval, int) and minval == 0
  78. maxval_is_one = isinstance(maxval, int) and maxval == 1
  79. if not minval_is_zero or not maxval_is_one or dtype.is_integer:
  80. minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
  81. maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")
  82. seed1, seed2 = random_seed.get_seed(seed)
  83. if dtype.is_integer:
  84. result = gen_random_ops.random_uniform_int(
  85. shape, minval, maxval, seed=seed1, seed2=seed2, name=name)
  86. else:
  87. result = gen_random_ops.random_uniform(
  88. shape, dtype, seed=seed1, seed2=seed2)
  89. if minval_is_zero:
  90. if not maxval_is_one:
  91. result = math_ops.multiply(result, maxval)
  92. else:
  93. result = math_ops.add(result * (maxval - minval), minval, name=name)
  94. # TODO(b/132092188): C++ shape inference inside functional ops does not
  95. # cross FuncGraph boundaries since that information is only available in
  96. # python. So we manually get the static shape using
  97. # `constant_value_as_shape` which *does* cross function boundaries.
  98. tensor_util.maybe_set_static_shape(result, shape)
  99. return result

本文转载自: https://blog.csdn.net/hy592070616/article/details/129802863
版权归原作者 von Neumann 所有, 如有侵权,请联系我们删除。

“深入浅出TensorFlow2函数——tf.random.uniform”的评论:

还没有评论