Pytorch入門
簡單容易上手,感受比keras好理解多了,和mxnet很像(彷佛mxnet有點借鑑pytorch),記一記。python
直接從例子開始學,基礎知識咱已經看了不少論文了。。。ide
import torch import torch.nn as nn import torch.nn.functional as F # Linear 層 就是全鏈接層 class Net(nn.Module): # 繼承nn.Module,只用定義forward,反向傳播會自動生成 def __init__(self): # 初始化方法,這裏的初始化是爲了forward函數能夠直接調過來 super(Net,self).__init__() # 調用父類初始化方法 # (input_channel,output_channel,kernel_size) self.conv1 = nn.Conv2d(1,6,5) # 第一層卷積 self.conv2 = nn.Conv2d(6,16,5)# 第二層卷積 self.fc1 = nn.Linear(16*5*5,120) # 這裏16*5*5是前向算的 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)) # 卷積一次激活一次而後2*2池化一次 x = F.max_pool2d(F.relu(self.conv2(x)),2) # (2,2)與直接寫 2 等價 x = x.view(-1,self.num_flatten_features(x)) # 將x展開成向量 x = F.relu(self.fc1(x)) # 全鏈接 + 激活 x = F.relu(self.fc2(x)) # 全鏈接+ 激活 x = self.fc3(x) # 最後再全鏈接 return x def num_flatten_features(self,x): size = x.size()[1:] # 除了batch_size之外的維度,(batch_size,channel,h,w) num_features = 1 for s in size: num_features*=s return num_features # ok,模型定義完畢。 net = Net() print(net) ''' 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(in_features=400, out_features=120, bias=True) (fc2): Linear(in_features=120, out_features=84, bias=True) (fc3): Linear(in_features=84, out_features=10, bias=True) ) ''' params = list(net.parameters()) print(len(params)) print(params[0].size()) ''' 10 torch.Size([6, 1, 5, 5]) ''' inpt = torch.randn(1,1,32,32) out = net(inpt) print(out) ''' tensor([[-0.0265, -0.1246, -0.0796, 0.1028, -0.0595, 0.0383, 0.0038, -0.0019, 0.1181, 0.1373]], grad_fn=<AddmmBackward>) ''' target = torch.randn(10) criterion = nn.MSELoss() loss = criterion(out,target) print(loss) ''' tensor(0.5742, grad_fn=<MseLossBackward>) ''' net.zero_grad()# 梯度歸零 print(net.conv1.bias.grad) loss.backward() print(net.conv1.bias.grad) ''' None tensor([-0.0039, 0.0052, 0.0034, -0.0002, 0.0018, 0.0096]) ''' import torch.optim as optim optimizer = optim.SGD(net.parameters(),lr = 0.01) optimizer.zero_grad() output = net(inpt) loss = criterion(output,target) loss.backward() optimizer.step() # 一個step完成,多個step就寫在循環裏
pytorch簡直太好理解了。。繼續蓄力!!函數