深入浅出Pytorch函数——torch.sum

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

相关文章:

· 深入浅出Pytorch函数------torch.Tensor


函数torch.sum有两种形式:

  • torch.sum(input, *, dtype=None):返回输入张量input所有元素的和。
  • torch.sum(input, dim, keepdim=False, *, dtype=None):返回给定维度dim中输入张量的每一行的总和。如果dim是一个维度列表,则对所有维度进行缩小。如果keepdimTrue,则输出张量的大小与输入的大小相同,但维度dim的大小为1。否则,dim会被挤压(参考torch.squeeze())。

语法

dart 复制代码
torch.sum(input, *, dtype=None) -> Tensor
torch.sum(input, dim, keepdim=False, *, dtype=None) -> Tensor

参数

  • input:输入张量
  • dim:[可选, int/tuple] 要减少的一个或多个维度。如果为None,则所有维度都将被裁剪。
  • keepdim:[bool] 输出张量是否保留了dim
  • dtype:[可选, torch.dtype] 返回张量的所需数据类型。如果指定,则在执行操作之前将输入张量强制转换为dtype。这对于防止数据类型溢出非常有用,默认值为None

实例

dart 复制代码
>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.1133, -0.9567,  0.2958]])
>>> torch.sum(a)
tensor(-0.5475)
>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.0569, -0.2475,  0.0737, -0.3429],
        [-0.2993,  0.9138,  0.9337, -1.6864],
        [ 0.1132,  0.7892, -0.1003,  0.5688],
        [ 0.3637, -0.9906, -0.4752, -1.5197]])
>>> torch.sum(a, 1)
tensor([-0.4598, -0.1381,  1.3708, -2.6217])
>>> b = torch.arange(4 * 5 * 6).view(4, 5, 6)
>>> torch.sum(b, (2, 1))
tensor([  435.,  1335.,  2235.,  3135.])
相关推荐
小a彤40 分钟前
elec-ops-inspection:电力巡检缺陷检测,NPU推理速度提升3倍
人工智能·cann
ZhengEnCi1 小时前
09aaa-LayerNorm是什么?
人工智能
这是谁的博客?1 小时前
AI Agent 安全架构设计:漏洞分析与防护策略深度解析
人工智能·安全·网络安全·ai·agent·安全架构·架构设计
人月神话-Lee1 小时前
【图像处理】Sobel 边缘检测——让机器“看见“轮廓
图像处理·人工智能·计算机视觉·ios·ai编程·swift
冬奇Lab2 小时前
Agent系列(四):工具调用深度解析——Agent 的手和眼
人工智能·llm
Black蜡笔小新2 小时前
自动化AI算法训练服务器DLTM助力医学影像分析进入AI智能分析新时代
人工智能·算法·自动化
冬奇Lab2 小时前
一天一个开源项目(第111篇):Understand Anything - 把代码库变成可探索知识图谱的 AI 引擎
人工智能·开源·llm
猿饵块2 小时前
git--github
人工智能
黎阳之光2 小时前
黎阳之光:以视频孪生重构智慧防火,打造“天空地人智”一体化森林防火新范式
大数据·运维·人工智能·物联网·安全
why技术3 小时前
AI Coding开始进入第四个时代,我还没上车呢!
前端·人工智能·后端