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}")
相关推荐
ccLianLian几秒前
计算机基础·cs336·推理和训练
人工智能·深度学习
鹅是开哥几秒前
Spring AI Alibaba + DashScope 调用超时彻底解决(SocketTimeoutException / read timeout)
java·人工智能·spring
网易伏羲2 分钟前
以数据驱动工程机械智能化,网易灵动入选杭州国家语料库首批高质量数据集榜单
人工智能·具身智能·网易伏羲·网易灵动
够快云库2 分钟前
2026信创架构实战:制造业非结构化数据的深度治理之道
人工智能·架构·企业文件管理
cuber膜拜3 分钟前
Tenacity 原理与基本使用
服务器·网络·python·装饰器模式·tenacity
lisw053 分钟前
机器人系统:化学研究的超空间引擎——从自动化到智能化的范式革命
大数据·人工智能·科技·机器学习·机器人
Myosotis5133 分钟前
作业 第三次
开发语言·python
cuber膜拜4 分钟前
PyBreaker 原理与基本使用
服务器·网络·python·pybreaker
DR56475 分钟前
第 1 章 绪论
人工智能
学Linux的语莫6 分钟前
模型转为RKNN格式
python·深度学习·机器学习