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都保存下来了。这个计算刚好可以用来进行体渲染。

相关推荐
用户83562907805115 小时前
Python 实现 PDF 文件加密与解密方法
后端·python
用户83562907805115 小时前
使用 Python 冻结与拆分 Excel 窗格教程
后端·python
你好潘先生1 天前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师1 天前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码1 天前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
copyer_xyf1 天前
FastAPI 如何连接 MySQL
后端·python
apocelipes2 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户8356290780512 天前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent2 天前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6252 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python