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}")
相关推荐
金銀銅鐵7 小时前
用 Python 实现 Take-Away 游戏
python·游戏
IT_陈寒7 小时前
SpringBoot自动配置这个坑,我踩进去又爬出来了
前端·人工智能·后端
copyer_xyf8 小时前
Agent 流程编排
后端·python·agent
copyer_xyf8 小时前
Agent RAG
后端·python·agent
copyer_xyf8 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf8 小时前
Agent 记忆管理
后端·python·agent
冬奇Lab19 小时前
Agent 系列(23):Web Agent——让 Agent 真正浏览网页
人工智能·llm·agent
冬奇Lab19 小时前
每日一个开源项目(第135篇):codebase-memory-mcp - 给 AI Agent 一张代码库的知识图谱
人工智能·开源·llm