pytorch中numel()函数用于获取张量中元素数目

pytorch中,numel()函数用于获取张量中元素数目 ,其中 numel() 可以理解为是 number of elements 的缩写。

例如:

python 复制代码
import torch

a = torch.randn(2,3)
b = a.numel()
print(a,b)

# tensor([[-0.4062, -0.8251, -2.2294],
#         [ 0.5109, -1.4237,  0.8322]]) 6

比如实际应用,numel()函数可用于获取模型参数的总数目:

python 复制代码
import logging

# model = ...

num_param = sum([p.numel() for p in model.parameters()])
logging.info(f"Number of model parameters: {num_param}")
相关推荐
MobotStone37 分钟前
从“听得懂”到“干得了”:工业大模型落地工厂的三层进化路线
人工智能
wangruofeng1 小时前
GLM-5.3-Flash 发布:追平 Opus 4.8 的智力,1/40 的价格,跑在国产芯片上
人工智能·aigc·chatglm (智谱)
皮皮虾❀3 小时前
钉钉AI服务商×教育行业:AI现代产业学院共建一体化实战——从“高校AI教学缺平台”到“校企共建AI产业学院培养数字化人才
人工智能·钉钉
kobe_OKOK_4 小时前
DRF接口幂等操作
python·django
火山引擎开发者社区4 小时前
【议程来了】火山引擎开发者社区技术日・NVIDIA 独家赞助-成都站
人工智能
三声三视4 小时前
从崩溃日志到 AI 技能:tri-god 蒸馏实战
人工智能·ai·aigc·agent
ZGIAI4 小时前
ZGI Workflow 并发控制:保护下游接口
人工智能·架构
richard_first4 小时前
Transformer 与大语言模型:第9章 LayerNorm 归一化层
人工智能·深度学习·机器学习·transformer
ZGIAI4 小时前
知识库有结果,不等于召回率合格
人工智能·架构
东小西4 小时前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来
java·人工智能·spring