如何计算模型的运算复杂度(FLOPS, Params)

📅 发布时间:2026/7/7 21:19:40 👁️ 浏览次数:
如何计算模型的运算复杂度(FLOPS, Params)
如何计算模型的运算复杂度FLOPS, Params参考博客Python实战用thop库快速计算模型参数量(Params)和FLOPs附完整代码import torch from thop import profile # 1. 实例化模型 model torch.load(你的模型地址, map_locationcpu) model.eval() # 2. 构造一个模拟输入 # 维度是 (batch_size, channels, height, width) # AlexNet的标准输入是224x224的RGB图像 input_tensor torch.randn(1, 3, 224, 224) # 3. 使用profile函数进行计算 # profile函数会“运行”一次模型统计各层的运算和参数 flops, params profile(model, inputs(input_tensor,)) # 4. 格式化输出 print(f原始FLOPs: {flops}) print(f原始参数量: {params}) print(- * 40) print(fFLOPs: {flops / 1e9:.2f} G) # 除以10^9转换为G单位 print(fParams: {params / 1e6:.2f} M) # 除以10^6转换为M单位