eniops库中reduce函数使用方法

reduce 是 eniops 中的一个常用函数,用于对张量进行降维操作。它允许你通过指定维度名称和操作类型(如求和、均值等)来简化张量的形状。

python 复制代码
import eniops
import torch

# 创建一个示例张量
x = torch.randn(2, 3, 4)

# 使用 reduce 进行降维操作
result = eniops.reduce(x, 'b c h -> b h', 'mean')

print(result.shape)  # 输出: torch.Size([2, 4])

输入张量 x 的形状为 (2, 3, 4),对应模式 'b c h'。

reduce 操作将 c 维度通过 'mean' 操作降维,最终输出形状为 (2, 4),对应模式 'b h'。

除了mean,还有sum,max等降维方式.

如下,

python 复制代码
result = eniops.reduce(x, 'b c h -> b h', 'sum')
print(result.shape)  # 输出: torch.Size([2, 4])
python 复制代码
result = eniops.reduce(x, 'b c h -> b h', 'max')
print(result.shape)  # 输出: torch.Size([2, 4])
相关推荐
哈里谢顿1 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户83562907805115 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
用户14748530797416 小时前
AI-动手深度学习环境搭建-d2l
深度学习
markfeng817 小时前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi18 小时前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee18 小时前
Python 中 `if x:` 到底在判断什么?
后端·python
OpenBayes贝式计算18 小时前
解决视频模型痛点,TurboDiffusion 高效视频扩散生成系统;Google Streetview 涵盖多个国家的街景图像数据集
人工智能·深度学习·机器学习
ServBay18 小时前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
OpenBayes贝式计算18 小时前
OCR教程汇总丨DeepSeek/百度飞桨/华中科大等开源创新技术,实现OCR高精度、本地化部署
人工智能·深度学习·机器学习
闲云一鹤18 小时前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi