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])
相关推荐
Astro_ChaoXu5 分钟前
GAMSE使用日志与教程(高分辨率光谱数据缩减)
linux·数据库·python
人工智能培训9 分钟前
基于知识图谱的故障推理方法与算法
人工智能·python·深度学习·机器学习·知识图谱·故障诊断
张登杰踩25 分钟前
工业图像序列识别实战:基于PyTorch的OCR模型训练与优化
人工智能·pytorch·ocr
ID_1800790547344 分钟前
超详细:Python 调用淘宝商品详情 API 完整教程
开发语言·python
平常心cyk1 小时前
Python基础快速复习——函数的多种传参方式
python
lanboAI1 小时前
基于卷积神经网络的舌苔诊断系统,resnet50,alexnet, shufflenet模型【pytorch框架+python源码】
pytorch·python·cnn
QWsin1 小时前
【Pydantic】Pydantic 是什么?
python
WeeJot嵌入式1 小时前
爬虫对抗:ZLibrary反爬机制实战分析
爬虫·python·网络安全·playwright·反爬机制
Bert.Cai2 小时前
Python input函数作用
开发语言·python
Fairy要carry2 小时前
项目03-手搓Agent之团队协作(发消息/分配任务)
linux·前端·python