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

这篇具有很好参考价值的文章主要介绍了深入浅出TensorFlow2函数——tf.random.uniform。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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


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

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

用均匀分布值填充的指定形状的张量。文章来源地址https://www.toymoban.com/news/detail-512002.html

实例
tf.random.uniform(shape=[2])
tf.random.uniform(shape=[], minval=-1., maxval=0.)
tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)
函数实现
@tf_export("random.uniform", v1=["random.uniform", "random_uniform"])
@dispatch.add_dispatch_support
@deprecation.deprecated_endpoints("random_uniform")
def random_uniform(shape,
                   minval=0,
                   maxval=None,
                   dtype=dtypes.float32,
                   seed=None,
                   name=None):
  """Outputs random values from a uniform distribution.
  The generated values follow a uniform distribution in the range
  `[minval, maxval)`. The lower bound `minval` is included in the range, while
  the upper bound `maxval` is excluded.
  For floats, the default range is `[0, 1)`.  For ints, at least `maxval` must
  be specified explicitly.
  In the integer case, the random integers are slightly biased unless
  `maxval - minval` is an exact power of two.  The bias is small for values of
  `maxval - minval` significantly smaller than the range of the output (either
  `2**32` or `2**64`).
  Examples:
  >>> tf.random.uniform(shape=[2])
  <tf.Tensor: shape=(2,), dtype=float32, numpy=array([..., ...], dtype=float32)>
  >>> tf.random.uniform(shape=[], minval=-1., maxval=0.)
  <tf.Tensor: shape=(), dtype=float32, numpy=-...>
  >>> tf.random.uniform(shape=[], minval=5, maxval=10, dtype=tf.int64)
  <tf.Tensor: shape=(), dtype=int64, numpy=...>
  The `seed` argument produces a deterministic sequence of tensors across
  multiple calls. To repeat that sequence, use `tf.random.set_seed`:
  >>> tf.random.set_seed(5)
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=2>
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=0>
  >>> tf.random.set_seed(5)
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=2>
  >>> tf.random.uniform(shape=[], maxval=3, dtype=tf.int32, seed=10)
  <tf.Tensor: shape=(), dtype=int32, numpy=0>
  Without `tf.random.set_seed` but with a `seed` argument is specified, small
  changes to function graphs or previously executed operations will change the
  returned value. See `tf.random.set_seed` for details.
  Args:
    shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
    minval: A Tensor or Python value of type `dtype`, broadcastable with
      `shape` (for integer types, broadcasting is not supported, so it needs to
      be a scalar). The lower bound on the range of random values to generate
      (inclusive).  Defaults to 0.
    maxval: A Tensor or Python value of type `dtype`, broadcastable with
      `shape` (for integer types, broadcasting is not supported, so it needs to
      be a scalar). The upper bound on the range of random values to generate
      (exclusive). Defaults to 1 if `dtype` is floating point.
    dtype: The type of the output: `float16`, `bfloat16`, `float32`, `float64`,
      `int32`, or `int64`. Defaults to `float32`.
    seed: A Python integer. Used in combination with `tf.random.set_seed` to
      create a reproducible sequence of tensors across multiple calls.
    name: A name for the operation (optional).
  Returns:
    A tensor of the specified shape filled with random uniform values.
  Raises:
    ValueError: If `dtype` is integral and `maxval` is not specified.
  """
  dtype = dtypes.as_dtype(dtype)
  accepted_dtypes = (dtypes.float16, dtypes.bfloat16, dtypes.float32,
                     dtypes.float64, dtypes.int32, dtypes.int64)
  if dtype not in accepted_dtypes:
    raise ValueError(
        f"Argument `dtype` got invalid value {dtype}. Accepted dtypes are "
        f"{accepted_dtypes}.")
  if maxval is None:
    if dtype.is_integer:
      raise ValueError("Must specify maxval for integer dtype %r" % dtype)
    maxval = 1
  with ops.name_scope(name, "random_uniform", [shape, minval, maxval]) as name:
    shape = tensor_util.shape_tensor(shape)
    # In case of [0,1) floating results, minval and maxval is unused. We do an
    # `is` comparison here since this is cheaper than isinstance or  __eq__.
    minval_is_zero = isinstance(minval, int) and minval == 0
    maxval_is_one = isinstance(maxval, int) and maxval == 1
    if not minval_is_zero or not maxval_is_one or dtype.is_integer:
      minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
      maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max")
    seed1, seed2 = random_seed.get_seed(seed)
    if dtype.is_integer:
      result = gen_random_ops.random_uniform_int(
          shape, minval, maxval, seed=seed1, seed2=seed2, name=name)
    else:
      result = gen_random_ops.random_uniform(
          shape, dtype, seed=seed1, seed2=seed2)
      if minval_is_zero:
        if not maxval_is_one:
          result = math_ops.multiply(result, maxval)
      else:
        result = math_ops.add(result * (maxval - minval), minval, name=name)
    # TODO(b/132092188): C++ shape inference inside functional ops does not
    # cross FuncGraph boundaries since that information is only available in
    # python. So we manually get the static shape using
    # `constant_value_as_shape` which *does* cross function boundaries.
    tensor_util.maybe_set_static_shape(result, shape)
    return result

到了这里,关于深入浅出TensorFlow2函数——tf.random.uniform的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 深入浅出TensorFlow2函数——tf.Variable

    分类目录:《深入浅出TensorFlow2函数》总目录 语法 参数 initial_value :[ Tensor ] 变量的初始值。初始值必须具有指定的形状,除非 validate_shape 被设置为 False 。也可以是一个不带参数的可调用函数,调用时返回初始值。在这种情况下, dtype 必须指定。 trainable :[ bool ] 如果为

    2024年02月06日
    浏览(11)
  • 深入浅出TensorFlow2函数——tf.reshape

    分类目录:《深入浅出TensorFlow2函数》总目录 语法 参数 返回值 返回一个新的形状为 shape 的 tf.Tensor 且具有与 tensor 以同样的顺序和相同的值。 实例 输入: 如果 shape 的一个参数为是 -1 ,则计算该维度的大小,使总大小保持不变。特别是,若 shape 为 [-1] ,则将 tensor 展平为一

    2024年02月11日
    浏览(11)
  • 深入浅出TensorFlow2函数——tf.rank

    分类目录:《深入浅出TensorFlow2函数》总目录 语法 参数 input : tf.Tensor 或 tf.SparseTensor name :[可选] 操作的名称 返回值 张量 input 的维度,是一个 int32 类型的张量 实例 输入: 输出: 函数实现

    2024年02月12日
    浏览(10)
  • 深入浅出TensorFlow2函数——tf.exp

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.exp · 深入浅出TensorFlow2函数——tf.math.exp · 深入浅出Pytorch函数——torch.exp · 深入浅出PaddlePaddle函数——paddle.exp 按元素计算 x x x 的指数 y = e x y=e^x y = e x 。 语法 参数 x :[ tf.Tensor ] 必须

    2024年02月12日
    浏览(10)
  • 深入浅出TensorFlow2函数——tf.math.exp

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.exp · 深入浅出TensorFlow2函数——tf.math.exp · 深入浅出Pytorch函数——torch.exp · 深入浅出PaddlePaddle函数——paddle.exp 按元素计算 x x x 的指数 y = e x y=e^x y = e x 。 语法 参数 x :[ tf.Tensor ] 必须

    2024年02月12日
    浏览(8)
  • 深入浅出TensorFlow2函数——tf.reduce_sum

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.reduce_sum · 深入浅出TensorFlow2函数——tf.math.reduce_sum · 深入浅出Pytorch函数——torch.sum · 深入浅出PaddlePaddle函数——paddle.sum 计算张量各维度上元素的总和。 语法 参数 input_tensor :[ Tensor

    2024年02月10日
    浏览(14)
  • 深入浅出TensorFlow2函数——tf.math.reduce_sum

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.reduce_sum · 深入浅出TensorFlow2函数——tf.math.reduce_sum · 深入浅出Pytorch函数——torch.sum · 深入浅出PaddlePaddle函数——paddle.sum 计算张量各维度上元素的总和。 语法 参数 input_tensor :[ Tensor

    2024年02月12日
    浏览(8)
  • 深入浅出TensorFlow2函数——tf.data.Dataset.from_tensor_slices

    分类目录:《深入浅出TensorFlow2函数》总目录 返回一个数据集,其元素是给定张量的切片。给定的张量沿着它们的第一维度进行切片。此操作保留输入张量的结构,删除每个张量的第一个维度并将其用作数据集维度。所有输入张量在其第一维度上必须具有相同的大小。 语法

    2024年02月12日
    浏览(12)
  • 深入浅出C语言—【函数】上

    深入浅出C语言—【函数】上

       目录 1.函数的概念 2.C语言函数的分类 2.1 库函数 2.1.1 strcpy库函数举例学习方式 2.1.2 库函数扩展知识 2.2 自定义函数 2.2.1求两个整数中的较大值 3. 函数的参数 3.1 实际参数(实参) 3.2 形式参数(形参) 4. 函数的调用 4.1 传值调用 4.2 传址调用 老铁们,网址自取,记得一键

    2024年02月07日
    浏览(37)
  • 深入浅出C语言—【函数】下

    深入浅出C语言—【函数】下

    函数和函数之间可以根据实际的需求进行组合的,也就是互相调用的。 注意: 函数可以嵌套调用,但是不能嵌套定义。 把一个函数的返回值作为另外一个函数的参数。 上面的strlen函数是求数组长度的库函数, 特别注意的是,当数组为字符数组时,数组的末尾会自动放一个

    2024年02月17日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包