使用pytorch库中的model.parameters()可以获得模型的所有参数,然后使用len()函数可以统计参数的数量。下面是一个示例代码:
import torchimport torch.nn as nn# 创建模型model = nn.Linear(10, 5)# 统计参数数量num_parameters = sum(p.numel() for p in model.parameters())print(f"模型参数数量: {num_parameters}")输出结果会显示模型的参数数量。

