Pytorch 计算量FLOPs和参数量Parameter的统计

from torchvision.models import resnet34
import torch
#需要下面这个头文件
from thop import profile
if __name__ == "__main__":
	#模型
    model = resnet34()
    #初始化输入参数 batchsize channel height width
    input = torch.randn(1, 3, 256, 512)
    Flops, params = profile(model, inputs=(input,))#inputs根据model的输入来定
    print('Flops: % .4fG'%(Flops / 1000000000))# 计算量
    print('params参数量: % .4fM'% (params / 1000000)) #参数量:等价与上面的summary输出的Total params值

PyTorch中,计算模型的参数(Parameter浮点运算数(FLOP)可以帮助我们了解模型的复杂度资源消耗。以下是如何在PyTorch中计算ParameterFLOP的步骤: ### 计算模型参数(Parameter) 要计算模型的参数数量,可以使用`torch.nn.Module`提供的`parameters()`方法。这个方法会返回模型中所有的参数,然后我们可以使用`numel()`方法计算每个参数的元素数量,并求得到总参数数量。 ```python import torch.nn as nn def count_parameters(model): return sum(p.numel() for p in model.parameters() if p.requires_grad) # 假设我们有一个模型 model = nn.Sequential( nn.Linear(10, 50), nn.ReLU(), nn.Linear(50, 1) ) # 计算参数数量 num_params = count_parameters(model) print(f'The model has {num_params} trainable parameters') ``` ### 计算浮点运算数(FLOP) 计算FLOP稍微复杂一些,因为需要根据模型的架构手动计算每层的计算量。以下是一个简单的例子,展示了如何计算一个包含线性层ReLU激活函数的模型的FLOP。 ```python import torch import torch.nn as nn def count_flops(model, input_size): flops = 0 input_shape = (1, *input_size) device = next(model.parameters()).device x = torch.randn(input_shape).to(device) hooks = [] def hook_fn(module, input, output): if isinstance(module, nn.Linear): out_features, in_features = module.weight.shape flops_per_sample = in_features * out_features flops += flops_per_sample * x.size(0) elif isinstance(module, nn.Conv2d): # 简单的卷积层FLOP计算 out_h = output.size(2) out_w = output.size(3) flops_per_sample = module.in_channels * module.out_channels * module.kernel_size[0] * module.kernel_size[1] * out_h * out_w flops += flops_per_sample * x.size(0) for m in model.modules(): if isinstance(m, (nn.Linear, nn.Conv2d)): hooks.append(m.register_forward_hook(hook_fn)) with torch.no_grad(): model(x) for hook in hooks: hook.remove() return flops # 假设我们有一个模型 model = nn.Sequential( nn.Linear(10, 50), nn.ReLU(), nn.Linear(50, 1) ) # 计算FLOP flops = count_flops(model, input_size=(10,)) print(f'The model requires {flops} FLOPs') ``` ### 总结 - **参数计算**:使用`model.parameters()``numel()`方法。 - **FLOP计算**:需要手动计算每层的计算量,并使用`register_forward_hook`方法注册钩子函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AplusX

踩坑不易,打个赏呗~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值