Pytorch:torch.sum()函数用法

torch.sum()

先看看官网描述:https://pytorch.org/docs/stable/generated/torch.sum.html#torch.sum

函数torch.sum有两种形式:

第一种:
  torch.sum(input, *, dtype=None) → Tensor .
  Returns the sum of all elements in the input tensor.(返回输入张量input所有元素的和。)

第二种:
   torch.sum(input, dim, keepdim=False, *, dtype=None) → Tensor .
  Returns the sum of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.
  ( 返回给定维度dim中输入张量的每一行的总和。如果dim是一个维度列表,则对列表里所有维度进行缩小。

  If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).
  (如果keepdim为True,则输出张量的大小与输入的大小相同,但在指定维度上的大小为1。否则,dim会被挤压(参考torch.squeeze())。

  比如一个矩阵维度(2,3,4),dim=0,那就是消去第0维,变成了 (3,4),消去第1维,变成了 (2,4),消去第2维,变成了 (2,3),dim=-1,也就是最后一维,在这个矩阵中也就是第二维。

numpy.sum()

   sum(a, axis, dtype, out, keepdims, initial, where) .

参数:
  a:第一个参数是是传入/输入的数组元素

  aixs(可选) 沿轴使用(可选填,默认为数组的展平成一维形式,即0,1,2,3,4…等)

  • 如果是沿着0轴,则返回每一列最大值的索引
  • 如果是沿着1轴,则返回每一行最大值的索引
  • 如果axis为负数,则从最后一个轴到第一个轴进行相加
  • 如果默认为None,则把输入的所有元素都加起来

  dtype(可选) 默认为:numpy.float64(可以进行修改,比如dtype = numpy.int32)

  out(可选) 数组类型

  keepdims(可选) 布尔类型

  • 如果设置为True值,则缩小轴的尺寸保留为1,保存在维数中(结果为)
  • 如果设置为False值,则不会保留这个1

   where(可选) 布尔类型

  • 如果为True值,则会正常的进行sum运算
  • 如果非False值,则不会进行sum运算,从而赋值为0

相关推荐

  1. SummaryWriter函数

    2023-12-23 13:52:04       54 阅读
  2. apply&lambda函数

    2023-12-23 13:52:04       65 阅读
  3. python:map()函数

    2023-12-23 13:52:04       29 阅读
  4. MATLAB中dlmwrite函数

    2023-12-23 13:52:04       58 阅读
  5. MATLAB中cell函数

    2023-12-23 13:52:04       63 阅读
  6. Excel中的INDIRECT函数

    2023-12-23 13:52:04       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-23 13:52:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-23 13:52:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-23 13:52:04       82 阅读
  4. Python语言-面向对象

    2023-12-23 13:52:04       91 阅读

热门阅读

  1. Python实现逐行读取文本文件的几种方法

    2023-12-23 13:52:04       55 阅读
  2. centos 安装 Miniconda

    2023-12-23 13:52:04       66 阅读
  3. Python:Scrapy+Selenium相关依赖包记录

    2023-12-23 13:52:04       57 阅读
  4. 面向LLM的App架构——技术维度

    2023-12-23 13:52:04       48 阅读
  5. k8s中Chart的命名模板

    2023-12-23 13:52:04       60 阅读
  6. 19-二分-值域二分-有序矩阵中第 K 小的元素

    2023-12-23 13:52:04       64 阅读
  7. c# opencv 提取图片文字,如读取身份证号

    2023-12-23 13:52:04       64 阅读
  8. 各大高校科研工具链培训PPT汇总

    2023-12-23 13:52:04       59 阅读