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}")
相关推荐
Chuck.li5 分钟前
AI 长篇创作工具选型:工作流对照与自测清单
人工智能·大模型·ai写作·网文作者·ai网文·豆包写作
aleimu16 分钟前
Windows 11 右键打开 Cmder 2.0:从找不到菜单到默认使用 Cmder
前端·人工智能·后端
LK_0720 分钟前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
wuyk55521 分钟前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
SwanArc技术笔记31 分钟前
读技术文档,先用哪个 AI?模型指南筛选与 GPT-6.2 Astra 实操
人工智能
深海鱼在掘金32 分钟前
深入浅出RAG——第21章:回顾与展望
人工智能
羲云网络33 分钟前
GPT Image 2.6 GIF 交付排障:上线后为什么只剩第一帧?
图像处理·人工智能·前端开发·gif
安爹前沿AI41 分钟前
Seedance 有个文档里没写的参数:开了码率涨 3.9 倍,费用一分没多
人工智能
深海鱼在掘金43 分钟前
深入浅出RAG——第20章:端到端实战:企业级知识库问答系统
人工智能