深入浅出TensorFlow2函数——tf.math.reduce_sum

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

分类目录:《深入浅出TensorFlow2函数》总目录
相关文章:
· 深入浅出TensorFlow2函数——tf.reduce_sum
· 深入浅出TensorFlow2函数——tf.math.reduce_sum
· 深入浅出Pytorch函数——torch.sum
· 深入浅出PaddlePaddle函数——paddle.sum


计算张量各维度上元素的总和。

语法
tf.math.reduce_sum(
    input_tensor, axis=None, keepdims=False, name=None
)
参数
  • input_tensor:[Tensor] 待求和的多维Tensor
  • axis:求和运算的维度。如果为None,则计算所有元素的和并返回包含单个元素的Tensor变量,否则必须在 [ − rank ( x ) , rank ( x ) ] [-\text{rank}(x), \text{rank}(x)] [rank(x),rank(x)]范围内。如果 axis [ i ] < 0 \text{axis}[i]<0 axis[i]<0,则维度将变为 rank + axis [ i ] \text{rank} + \text{axis}[i] rank+axis[i],默认值为None
  • keepdim:[bool] 是否在输出Tensor中保留减小的维度。如keepdim=True,否则结果张量的维度将比输入张量小,默认值为False
  • name:[可选, str] 操作的名称,默认值为None
返回值

input_tensor具有相同dtype 的求和后的tensor文章来源地址https://www.toymoban.com/news/detail-523858.html

实例
>>> # x has a shape of (2, 3) (two rows and three columns):
>>> x = tf.constant([[1, 1, 1], [1, 1, 1]])
>>> x.numpy()
array([[1, 1, 1],
       [1, 1, 1]], dtype=int32)
>>> # sum all the elements
>>> # 1 + 1 + 1 + 1 + 1+ 1 = 6
>>> tf.reduce_sum(x).numpy()
6
>>> # reduce along the first dimension
>>> # the result is [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
>>> tf.reduce_sum(x, 0).numpy()
array([2, 2, 2], dtype=int32)
>>> # reduce along the second dimension
>>> # the result is [1, 1] + [1, 1] + [1, 1] = [3, 3]
>>> tf.reduce_sum(x, 1).numpy()
array([3, 3], dtype=int32)
>>> # keep the original dimensions
>>> tf.reduce_sum(x, 1, keepdims=True).numpy()
array([[3],
       [3]], dtype=int32)
>>> # reduce along both dimensions
>>> # the result is 1 + 1 + 1 + 1 + 1 + 1 = 6
>>> # or, equivalently, reduce along rows, then reduce the resultant array
>>> # [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
>>> # 2 + 2 + 2 = 6
>>> tf.reduce_sum(x, [0, 1]).numpy()
6
函数实现
@tf_export("math.reduce_sum", "reduce_sum", v1=[])
@dispatch.add_dispatch_support
def reduce_sum(input_tensor, axis=None, keepdims=False, name=None):
  """Computes the sum of elements across dimensions of a tensor.
  This is the reduction operation for the elementwise `tf.math.add` op.
  Reduces `input_tensor` along the dimensions given in `axis`.
  Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each
  of the entries in `axis`, which must be unique. If `keepdims` is true, the
  reduced dimensions are retained with length 1.
  If `axis` is None, all dimensions are reduced, and a
  tensor with a single element is returned.
  For example:
    >>> # x has a shape of (2, 3) (two rows and three columns):
    >>> x = tf.constant([[1, 1, 1], [1, 1, 1]])
    >>> x.numpy()
    array([[1, 1, 1],
           [1, 1, 1]], dtype=int32)
    >>> # sum all the elements
    >>> # 1 + 1 + 1 + 1 + 1+ 1 = 6
    >>> tf.reduce_sum(x).numpy()
    6
    >>> # reduce along the first dimension
    >>> # the result is [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
    >>> tf.reduce_sum(x, 0).numpy()
    array([2, 2, 2], dtype=int32)
    >>> # reduce along the second dimension
    >>> # the result is [1, 1] + [1, 1] + [1, 1] = [3, 3]
    >>> tf.reduce_sum(x, 1).numpy()
    array([3, 3], dtype=int32)
    >>> # keep the original dimensions
    >>> tf.reduce_sum(x, 1, keepdims=True).numpy()
    array([[3],
           [3]], dtype=int32)
    >>> # reduce along both dimensions
    >>> # the result is 1 + 1 + 1 + 1 + 1 + 1 = 6
    >>> # or, equivalently, reduce along rows, then reduce the resultant array
    >>> # [1, 1, 1] + [1, 1, 1] = [2, 2, 2]
    >>> # 2 + 2 + 2 = 6
    >>> tf.reduce_sum(x, [0, 1]).numpy()
    6
  Args:
    input_tensor: The tensor to reduce. Should have numeric type.
    axis: The dimensions to reduce. If `None` (the default), reduces all
      dimensions. Must be in the range `[-rank(input_tensor),
      rank(input_tensor)]`.
    keepdims: If true, retains reduced dimensions with length 1.
    name: A name for the operation (optional).
  Returns:
    The reduced tensor, of the same dtype as the input_tensor.
  @compatibility(numpy)
  Equivalent to np.sum apart the fact that numpy upcast uint8 and int32 to
  int64 while tensorflow returns the same dtype as the input.
  @end_compatibility
  """

  return reduce_sum_with_dims(input_tensor, axis, keepdims, name,
                              _ReductionDims(input_tensor, axis))

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

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

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

相关文章

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

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.Tensor · 深入浅出Pytorch函数——torch.Tensor · 深入浅出PaddlePaddle函数——paddle.Tensor 一个 tf.Tensor 表示一个多维数组。在编写TensorFlow程序时,被操作和传递的主要对象就是 tf.Tensor 。 tf.Tens

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

    分类目录:《深入浅出TensorFlow2函数》总目录 相关文章: · 深入浅出TensorFlow2函数——tf.constant · 深入浅出TensorFlow2函数——tf.Tensor · 深入浅出Pytorch函数——torch.tensor · 深入浅出Pytorch函数——torch.as_tensor · 深入浅出PaddlePaddle函数——paddle.to_tensor 语法 参数 value :输出张量

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

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

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

    分类目录:《深入浅出TensorFlow2函数》总目录 绘制 shape 个来自每个给定均匀分布的样本。 语法 参数 shape :输出张量的形状,为一个一维整数张量或Python数组。 minval :要生成的随机值范围的下限(含),默认值为 0 。 minval :要生成的随机值范围的上限(不含),默认值为 1 。

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

    分类目录:《深入浅出TensorFlow2函数》总目录 绘制 shape 个来自每个给定泊松分布的样本。 语法 参数 shape :输出张量的形状,为一个一维整数张量或Python数组。 lam :样本提供描述泊松分布的参数。 dtype :输出的浮点类型: float16 、 bfloat16 、 float32 、 float64 ,默认为 float3

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

    分类目录:《深入浅出TensorFlow2函数》总目录 语法 参数 shape :输出张量的形状,为一个一维整数张量或Python数组。 mean 正态分布的平均值。类型为张量或 dtype ,可与 stddev 一起广播。 stddev :正态分布的标准偏差。类型为张量或 dtype ,可与 mean 一起广播。 dtype :输出的浮点

    2024年02月12日
    浏览(13)
  • 深入浅出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日
    浏览(20)
  • 深入浅出TensorFlow2函数——tf.data.Dataset.from_tensor_slices

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

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

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

    2024年02月17日
    浏览(41)
  • 深入浅出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日
    浏览(14)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包