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}")
相关推荐
深蓝AI1 小时前
不调模型不改壳:CMU 开源 Hermit,给 Agent 外挂一个能训练的适配器
人工智能·agent
汤姆宝库3 小时前
世界模型深拆:视频生成、潜在模拟与物理直觉
人工智能
weixin_468466857 小时前
从“看懂”到“做到”:视觉-语言-动作模型如何让机器人真正执行指令
人工智能·ai·机器人·大语言模型·前沿科技
朋友圈自动点赞工具7 小时前
科学AI插件化:从蛋白质结构到古籍修复
人工智能
龙粤GEO7 小时前
实体Entity在GEO中代表什么含义
人工智能
前端精髓8 小时前
结构化输出with_structured_output(抽卡)
python·langchain
听风吹等浪起8 小时前
第26章:ViT+InceptionDW+FocalLoss改进|CT骨折影像二分类完整Pytorch实现(含PyQt5推理GUI)
pytorch·qt·分类
尹人入圣9 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
剑雨巍巍9 小时前
AI Agent面试之基础概念
人工智能
微帧Visionular9 小时前
AI视频单帧清晰,播放时却闪烁,原因是什么?
人工智能·深度学习·音视频