参考网址
import torch
import torchvision
from torchsummary import summary #使用 pip install torchsummary
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
vgg = torchvision.models.vgg16().to(device)
# summary(your_model, input_size=(channels, H, W))
summary(vgg, input_size=(3, 224, 224))
for name, parameters in your_model.named_parameters():
print(name, ':', parameters.size())
pip install hiddenlayer
import hiddenlayer as h
vis_graph = h.build_graph(MyConvNet, torch.zeros([1 ,1, 28, 28])) # 获取绘制图像的对象
vis_graph.theme = h.graph.THEMES["blue"].copy() # 指定主题颜色
vis_graph.save("./demo1.png") # 保存图像的路径
pip install torchviz
from torchviz import make_dot
x = torch.randn(1, 1, 28, 28).requires_grad_(True) # 定义一个网络的输入值
y = MyConvNet(x) # 获取网络的预测值
MyConvNetVis = make_dot(y, params=dict(list(MyConvNet.named_parameters()) + [('x', x)]))
MyConvNetVis.format = "png"
# 指定文件生成的文件夹
MyConvNetVis.directory = "data"
# 生成文件
MyConvNetVis.view()
params = list(model.parameters())
k = 0
for i in params:
l = 1
print("该层的结构:" + str(list(i.size())))
for j in i.size():
l *= j
print("该层参数和:" + str(l))
k = k + l
print("总参数数量和:" + str(k))