我想确定我的火把模型的模型参数。不幸的是,我不能使用model.parameters()
方法,因为不是每个模型都使用torch.nn
。
我可以从torch.load(model_path)
获得的字典中计算模型参数吗?我会把这个和矩阵的列表的长度相加,把维度加起来。
model_parameter += len(list) # add 5 if list is 5 long
model_parameter += x.shape[0] * x.shape[1] # x is matrix with dimension 3x4, then model_parameter gets 12 added to it
我不需要模型参数的确切数量。我需要这些数字来粗略比较模型。A型是B型的两倍大吗?
或者,您知道更好的方法吗?我可以不使用model.load_state_dict()
和model.parameters()
来计算
发布于 2022-10-03 20:44:26
我不知道为什么不能使用model.parameters()
,但是如果您不关心可训练/不可训练的参数,可以尝试使用torch.save
来获得模型大小:
import io
import torch
from torchvision.models import resnet18
model = resnet18()
buffer = io.BytesIO()
torch.save(model, buffer)
print(f"Size of the model: {buffer.tell()} bytes")
https://stackoverflow.com/questions/73927295
复制相似问题