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])
相关推荐
孟健3 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞5 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽7 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程11 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪12 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
程序员打怪兽12 小时前
详解Visual Transformer (ViT)网络模型
深度学习
databook12 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img