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

相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
风合星语2 天前
2026 具身智能技术实战(一):VLA 到底怎么控制机器人?——用 LeRobot 跑通 SmolVLA 推理
pytorch·机器人·具身智能·vla·lerobot·smolvla
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
Tancenter2 天前
sequeeze()和unsequeeze()
pytorch·tensorr
奇思妙想聪明勤奋的小羊2 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1232 天前
2026年第38周GitHub趋势周报
python·科技·github