torch.cumprod实现累乘计算

cumprod取自"cumulative product"的缩写,即"累计乘法 "。

数学公式为:
y i = x 1 × x 2 × x 3 × . . . × x i y_i=x_1\times{x_2}\times{x_3}\times{...}\times{x_i} yi=x1×x2×x3×...×xi

官方链接:torch.cumprod

用法:

python 复制代码
import torch
a = torch.Tensor([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
r0 = torch.cumprod(a, dim=0)
print(r0)
# tensor([[ 1.,  2.,  3.,  4.,  5.],
#         [ 1.,  4.,  9., 16., 25.]])

r1 = torch.cumprod(a, dim=1)
print(r1)
# tensor([[  1.,   2.,   6.,  24., 120.],
#         [  1.,   2.,   6.,  24., 120.]])

我们自习观察r0和r1的区别,在不同维度上进行累乘。更重要的是,每个阶段乘法结果都保存下来了,比如 1 × 2 × 3 × 4 × 5 1\times2\times3\times4\times5 1×2×3×4×5结果等于120,但前四步的结果1,2,6,24都保存下来了。这个计算刚好可以用来进行体渲染。

相关推荐
花妖大人33 分钟前
Python和LLM问题
python·llm
不喜欢学数学er1 小时前
算法第五十三天:图论part04(第十一章)
开发语言·python·图论
你怎么知道我是队长1 小时前
python---构造函数、析构函数
开发语言·python
CF14年老兵1 小时前
深入浅出 Python 一等函数:一份友好的全面解析
后端·python·trae
jumin18062 小时前
python采用jdbc连接oracle
python·oracle
君万2 小时前
【go语言】字符串函数
爬虫·python·golang
captainOO72 小时前
MRO and mixin in Python Django
后端·python·django
小磊哥er3 小时前
【办公自动化】如何使用Python库高效自动化处理图像?
python
蔗理苦3 小时前
2025-08-22 Python进阶10——魔术方法
开发语言·python
、水水水水水3 小时前
RAG学习(五)——查询构建、Text2SQL、查询重构与分发
人工智能·python·深度学习·nlp