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])
相关推荐
hhzz17 小时前
基于监控视频的水位尺自动识别技术方案与实现
python·opencv·yolo·图像识别·cv
yongche_shi17 小时前
ragas官方文档中文版(五十)
开发语言·python·ai·ragas·如何评估和改进 rag 应用
weixin_4080996718 小时前
OCR批量识别图片方案:从手动处理到自动化API系统(Python/Java/PHP实战)
图像处理·python·ocr·文字识别·api调用·批量识别·石榴智能
AI行业学习18 小时前
Notepad++ 官方下载 + 完整安装 + 全套优化配置(2026最新)
开发语言·人工智能·python·前端框架·html·notepad++
Token炼金师19 小时前
幂律的预言:Kaplan 与 Chinchilla 的算力账本 —— Scaling Laws 与最优配比
人工智能·深度学习·大模型架构·kv cache·scaling laws
大圣编程19 小时前
Python中continue语句的用法是什么?
开发语言·前端·python
云烟成雨TD19 小时前
LangFlow 1.x 系列【5】可视化编辑页面功能说明
人工智能·python·agent
geovindu21 小时前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
tryCbest21 小时前
Python 文件操作
服务器·python
涛声依旧-底层原理研究所21 小时前
Agent 长任务可靠性设计:实现暂停、恢复、续跑与崩溃重启的完整方案
人工智能·python·系统架构