pytorch基本学习

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1=nn.Conv2d(1,6,5)
        self.conv2=nn.Conv2d(6,16,5)
        self.fc1=nn.Linear(16*5*5,120)
        self.fc2=nn.Linear(120,84)
        self.fc3=nn.Linear(84,10)
    def forward(self,x):
        x=F.max_pool2d(F.relu(self.conv1(x)),(2,2))
        x=F.max_pool2d(F.relu(self.conv2(x)),2)
        x=x.view(x.size()[0],-1)
        print(x)
        x=F.relu(self.fc1(x))
        x=F.relu(self.fc2(x))
        x=self.fc3(x)
        return x
net=Net()
params=list(net.parameters())

#输出可学习参数的名称和可学习参数的size
for name,parameters in net.named_parameters():
    print(name,':',parameters.size())
print(len(params))#输出可学习参数的个数
print(net)  #输出模型
--------------------- 
作者:zouxiaolv 
来源:CSDN 
原文:https://blog.csdn.net/zouxiaolv/article/details/83032779?utm_source=copy 
版权声明:本文为博主原创文章,转载请附上博文链接!

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1=nn.Conv2d(1,6,5)
        self.conv2=nn.Conv2d(6,16,5)
        self.fc1=nn.Linear(16*5*5,120)
        self.fc2=nn.Linear(120,84)
        self.fc3=nn.Linear(84,10)
    def forward(self,x):
        x=F.max_pool2d(F.relu(self.conv1(x)),(2,2))
        x=F.max_pool2d(F.relu(self.conv2(x)),2)
        x=x.view(x.size()[0],-1)
        print(x)
        x=F.relu(self.fc1(x))
        x=F.relu(self.fc2(x))
        x=self.fc3(x)
        return x
net=Net()
params=list(net.parameters())

#输出可学习参数的名称和可学习参数的size
for name,parameters in net.named_parameters():
    print(name,':',parameters.size())
print(len(params))#输出可学习参数的个数
print(net)  #输出模型

print的结果:

('conv1.weight', ':', torch.Size([6, 1, 5, 5]))
('conv1.bias', ':', torch.Size([6]))
('conv2.weight', ':', torch.Size([16, 6, 5, 5]))
('conv2.bias', ':', torch.Size([16]))
('fc1.weight', ':', torch.Size([120, 400]))
('fc1.bias', ':', torch.Size([120]))
('fc2.weight', ':', torch.Size([84, 120]))
('fc2.bias', ':', torch.Size([84]))
('fc3.weight', ':', torch.Size([10, 84]))
('fc3.bias', ':', torch.Size([10]))
10
Net (
  (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
  (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
  (fc1): Linear (400 -> 120)
  (fc2): Linear (120 -> 84)
  (fc3): Linear (84 -> 10)
)

猜你喜欢

转载自blog.csdn.net/zouxiaolv/article/details/83032779