(pytorch實戰 菜鳥入門)使用Pytorch實現小型卷積神經網絡網絡


卷積層

卷積神經網絡中每層卷積層(Convolutional layer)由若干卷積單元組成,每個卷積單元的參數都是通過反向傳播算法最佳化得到的。卷積運算的目的是提取輸入的不同特征,第一層卷積層可能只能提取一些低級的特征如邊緣、線條和角等層級,更多層的網路能從低級特征中迭代提取更復雜的特征。
·
pytorch的卷積層:

class torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

一維卷積層,輸入的尺度是(N, C_in,L),輸出尺度( N,C_out,L_out)的計算方式:

out(N_i, C_{out_j})=bias(C {out_j})+\sum^{C{in}-1}{k=0}weight(C{out_j},k)\bigotimes input(N_i,k)
bigotimes: 表示相關系數計算
stride: 控制相關系數的計算步長
dilation: 用於控制內核點之間的距離,詳細描述在[這里](https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md)
groups: 控制輸入和輸出之間的連接, group=1,輸出是所有的輸入的卷積;group=2,此時相當於有並排的兩個卷積層,每個卷積層計算輸入通道的一半,並且產生的輸出是輸出通道的一半,隨后將這兩個輸出連接起來。

參數說明如下:

Parameters:

in_channels(int) – 輸入信號的通道
out_channels(int) – 卷積產生的通道
kerner_size(int or tuple) - 卷積核的尺寸
stride(int or tuple, optional) - 卷積步長
padding (int or tuple, optional)- 輸入的每一條邊補充0的層數
dilation(int or tuple, `optional``) – 卷積核元素之間的間距
groups(int, optional) – 從輸入通道到輸出通道的阻塞連接數
bias(bool, optional) - 如果bias=True,添加偏置

舉例:

m = nn.Conv1d(16, 33, 3, stride=2)
input = Variable(torch.randn(20, 16, 50))
output = m(input)
print(output.size())
#torch.Size([20, 33, 24])

二維卷積層:

class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

例子:

>>> # With square kernels and equal stride
>>> m = nn.Conv2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
>>> # non-square kernels and unequal stride and with padding and dilation
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
>>> input = autograd.Variable(torch.randn(20, 16, 50, 100))
>>> output = m(input)

池化層

class torch.nn.MaxPool1d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)

對於輸入信號的輸入通道,提供1維最大池化(max pooling)操作

參數:

kernel_size(int or tuple) - max pooling的窗口大小
stride(int or tuple, optional) - max pooling的窗口移動的步長。默認值是kernel_size
padding(int or tuple, optional) - 輸入的每一條邊補充0的層數
dilation(int or tuple, optional) – 一個控制窗口中元素步幅的參數
return_indices - 如果等於True,會返回輸出最大值的序號,對於上采樣操作會有幫助
ceil_mode - 如果等於True,計算輸出信號大小的時候,會使用向上取整,代替默認的向下取整的操作

例子:

>>> # pool of size=3, stride=2
>>> m = nn.MaxPool1d(3, stride=2)
>>> input = autograd.Variable(torch.randn(20, 16, 50))
>>> output = m(input)

全連接層

class torch.nn.Linear(in_features, out_features, bias=True)
參數:
in_features - 每個輸入樣本的大小
out_features - 每個輸出樣本的大小
bias - 若設置為False,這層不會學習偏置。默認值:True

卷積神經網絡

卷積神經網絡(Convolutional Neural Network,CNN)是一種前饋神經網絡,它的人工神經元可以響應一部分覆蓋范圍內的周圍單元,對於大型圖像處理有出色表現。


pytorch實現ConvNet(注釋詳解)

import torch
from torch.autograd import Variable
#torch.autograd提供了類和函數用來對任意標量函數進行求導。
import torch.nn as nn
import torch.nn.functional as F
class MNISTConvNet(nn.Module):
    def __init__(self):
        super(MNISTConvNet, self).__init__()
        '''
這是對繼承自父類的屬性進行初始化。而且是用父類的初始化方法來初始化繼承的屬性。
也就是說,子類繼承了父類的所有屬性和方法,父類屬性自然會用父類方法來進行初始化。
        '''
#定義網絡結構
        self.conv1 = nn.Conv2d(1, 10, 5)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(10, 20, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(320, 50)
        self.fc2 = nn.Linear(50, 10)
    def forward(self, input):
        x = self.pool1(F.relu(self.conv1(input)))
        x = self.pool2(F.relu(self.conv2(x))).view(320)
        x = self.fc1(x)
        x = self.fc2(x)
        return x

net = MNISTConvNet()
print(net)
input = Variable(torch.randn(1, 1, 28, 28))
out = net(input)
print(out.size())


pytorch卷積層與池化層輸出的尺寸的計算公式詳解

要設計卷積神經網絡的結構,必須匹配層與層之間的輸入與輸出的尺寸,這就需要較好的計算輸出尺寸
我在這里詳細講了如何計算尺寸,請瀏覽


torch.nn.functional詳解

· Convolution 函數

torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

對幾個輸入平面組成的輸入信號應用1D卷積。

參數: 
-  -input – 輸入張量的形狀 (minibatch x in_channels x iW)
-  - weight – 過濾器的形狀 (out_channels, in_channels, kW) 
- - bias – 可選偏置的形狀 (out_channels) 
- - stride – 卷積核的步長,默認為1

例子:

>>> filters = autograd.Variable(torch.randn(33, 16, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50))
>>> F.conv1d(inputs, filters)

· Pooling 函數

torch.nn.functional.avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)

對由幾個輸入平面組成的輸入信號進行一維平均池化。

參數: 
- kernel_size – 窗口的大小 
- - stride – 窗口的步長。默認值為kernel_size 
- - padding – 在兩邊添加隱式零填充 
- - ceil_mode – 當為True時,將使用ceil代替floor來計算輸出形狀 
- - count_include_pad – 當為True時,這將在平均計算時包括補零

例子:

>>> # pool of square window of size=3, stride=2
>>> input = Variable(torch.Tensor([[[1,2,3,4,5,6,7]]]))
>>> F.avg_pool1d(input, kernel_size=3, stride=2)
Variable containing:
(0 ,.,.) =
  2  4  6
[torch.FloatTensor of size 1x1x3]

· 非線性激活函數

torch.nn.functional.relu(input, inplace=False)

· Normalization 函數

torch.nn.functional.batch_norm(input, running_mean, running_var, weight=None, bias=None, training=False, momentum=0.1, eps=1e-05)

· 線性函數

torch.nn.functional.linear(input, weight, bias=None)

· Dropout 函數

torch.nn.functional.dropout(input, p=0.5, training=False, inplace=False)

· 距離函數(Distance functions)

torch.nn.functional.pairwise_distance(x1, x2, p=2, eps=1e-06)

計算向量v1、v2之間的距離

x1:第一個輸入的張量
x2:第二個輸入的張量
p:矩陣范數的維度。默認值是2,即二范數。

例子:

>>> input1 = autograd.Variable(torch.randn(100, 128))
>>> input2 = autograd.Variable(torch.randn(100, 128))
>>> output = F.pairwise_distance(input1, input2, p=2)
>>> output.backward()

· 損失函數(Loss functions)

torch.nn.functional.nll_loss(input, target, weight=None, size_average=True)

負的log likelihood損失函數.

參數:
 - input - (N,C) C 是類別的個數 
 - - target - (N) 其大小是 0 <= targets[i] <= C-1 
 - - weight (Variable, optional) – 一個可手動指定每個類別的權重。如果給定的話,必須是大小為nclasses的Variable 
 - - size_average (bool, optional) – 默認情況下,是mini-batchloss的平均值,然而,如果size_average=False,則是mini-batchloss的總和。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM