pytorch学习笔记系列(5):卷积神经网络Conv2d&&Conv1d

卷积层原理

  • 卷积层是用一个固定大小的矩形区去席卷原始数据,将原始数据分成一个个和卷积核大小相同的小块,然后将这些小块和卷积核相乘输出一个卷积值(注意这里是一个单独的值,不再是矩阵了).
  • 卷积的本质就是用卷积核的参数来提取原始数据的特征,通过矩阵点乘的运算,提取出和卷积核特征一致的值,如果卷积层有多个卷积核,则神经网络会自动学习卷积核的参数值,使得每个卷积核代表一个特征.

关于 nn.torch.Conv2d 函数

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

假设 Conv2d 的输入 input 尺寸为 ( N , C i n , H i n , W i n ) (N,C_{in},H_{in},W_{in}) (N,Cin,Hin,Win),输出 output 尺寸为 ( N , C o u t , H o u t , W o u t ) (N,C_{out},H_{out},W_{out}) (N,Cout,Hout,Wout),有:
o u t ( N i , C o u t j ) = b i a s ( C o u t j ) + ∑ k = 0 C i n − 1 w e i g h t ( C o u t , k ) ∗ i n p u t ( N i , k ) out(N_i,C_{out_j})=bias(C_{out_j})+\sum_{k=0}^{C_{in}-1}weight(C_{out},k)*input(N_i,k) out(Ni,Coutj)=bias(Coutj)+k=0Cin1weight(Cout,k)input(Ni,k)
其中,*是2D cross-correlation操作,N为batch-size,C为channels数,H为Height,W为Width.

  • [1]-kernel_size
  • [2]-stride-步长
  • [3]-padding-每一维补零的数量
  • [4]-dilation-控制 kernel 点之间的空间距离(the spacing between the kernel points). 带孔卷积(atrous conv). 参考dilated-convolution-animations.
  • [5]-groups-控制 inputs 和 outputs 间的关联性(分组). 其中,要求 in_channels out_channels必须都可以被groups整除.

H o u t = H i n + 2 × p a d d i n g [ 0 ] − d i l a t i o n [ 0 ] × ( k e r n e l s i z e [ 0 ] − 1 ) − 1 s t r i d e [ 0 ] + 1 H_{out}=\frac{H_{in}+2×padding[0]-dilation[0]×(kernel_size[0]-1)-1}{stride[0]}+1 Hout=stride[0]Hin+2×padding[0]dilation[0]×(kernelsize[0]1)1+1
W o u t = W i n + 2 × p a d d i n g [ 1 ] − d i l a t i o n [ 1 ] × ( k e r n e l s i z e [ 1 ] − 1 ) − 1 s t r i d e [ 1 ] + 1 W_{out}=\frac{W_{in}+2×padding[1]-dilation[1]×(kernel_size[1]-1)-1}{stride[1]}+1 Wout=stride[1]Win+2×padding[1]dilation[1]×(kernelsize[1]1)1+1

  • Demo
import torch
import torch.nn as nn

### 输入通道为16,输出通道为33,kernel_size为3(长和宽相同),stride为2
m = nn.Conv2d(16, 33, 3, stride=2)
# non-square kernels and unequal stride and with padding
### 输入通道为16,输出通道为33,kernel_size为(3,5),对应的padding为(4,2)
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为4维向量,第一维为batch_size,第二维为in_chanels,第三维和第四维为50和100
input = torch.randn(20, 16, 50, 100)
output = m(input)

卷积神经网络CNN的PyTorch实现

  • 使用Mnist数据集实现的CNN。
  • 导入包
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
  • 下载数据
# Device configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

# Hyper parameters
num_epochs = 5
num_classes = 10
batch_size = 100
learning_rate = 0.001

# MNIST dataset
train_dataset = torchvision.datasets.MNIST(root='../../data/',
                                           train=True,
                                           transform=transforms.ToTensor(),
                                           download=True)

test_dataset = torchvision.datasets.MNIST(root='../../data/',
                                          train=False,
                                          transform=transforms.ToTensor())
  • 处理数据,使用 DataLoader 进行batch训练
# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False)
  • 建立计算图模型
# Convolutional neural network (two convolutional layers)
class ConvNet(nn.Module):
    def __init__(self, num_classes=10):
        super(ConvNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.fc = nn.Linear(7 * 7 * 32, num_classes)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1) # out.size(0)为batch_size
        out = self.fc(out)
        return out

model = ConvNet(num_classes).to(device)
  • 定义优化器optimizer和损失
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
  • 进行batch训练
# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):  
        # Move tensors to the configured device
        images = images.reshape(-1, 28*28).to(device)
        labels = labels.to(device)
        
        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, labels)
        
        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        if (i+1) % 100 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
                   .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
  • 测试模型
# Test the model
# In test phase, we don't need to compute gradients (for memory efficiency)
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.reshape(-1, 28*28).to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))

# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')

关于conv1d

  • conv1d是一维卷积,它和conv2d的区别在于只对宽度进行卷积,对高度不卷积.
    函数定义:
torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

参数说明:

  • input:输入的Tensor数据,格式为(batch, channels, W),三维数组,第一维度是样本数量,第二维度是通道数或者记录数,三维度是宽度.
  • weight:卷积核权重,也就是卷积核本身. 是一个三维数组,(out_channels, in_channels/groups, kW). out_channels 是卷积核输出层的神经元个数,也就是这层有多少个卷积核;in_channels是输入通道数;kW是卷积核的宽度.
  • bias:位移参数,可选项,一般也不用管.
  • stride:滑动窗口,默认为1,指每次卷积对原数据滑动1个单元格.
  • padding:是否对输入数据填充0. Padding可以将输入数据的区域改造成是卷积核大小的整数倍,这样对不满足卷积核大小的部分数据就不会忽略了. 通过padding参数指定填充区域的高度和宽度,默认0(填充区域为0,不填充).
  • dilation:卷积核之间的空格,默认1.
  • groups:将输入数据分组,通常不用管这个参数.
Demo
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

print("conv1d sample")

a=range(16)
x = Variable(torch.Tensor(a))
x=x.view(1,1,16)
print("x variable:", x)

b=torch.ones(3)
b[0]=0.1
b[1]=0.2
b[2]=0.3
weights = Variable(b) #torch.randn(1,1,2,2)) #out_channel*in_channel*H*W
weights=weights.view(1,1,3)
print ("weights:",weights)

y=F.conv1d(x, weights, padding=0)
print ("y:",y)
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值