卷积神经网络基础篇笔记——B站:刘二大人《PyTorch深度学习实践》

目录

1 卷积运算方法

 2.padding

3.stride

 4.Max Pooling Layer 最大池化层

5.一个简单的卷积神经网络


1 卷积运算方法

        kernel:卷积核

        从Input里面从左到右从上到下依次移动选取与kernel同样大小的块与其作数乘放入output中

 input有几个通道,卷积核就得有几个通道

 3*5*5的input通过3*3*3的卷积获得1*3*3的output

输入有n个通道,卷积核就有n个通道,输出有m个通道,就有m个卷积核

 例子:

import torch
in_channels,out_channels = 5,10
width,height = 100,100
kernel_size = 3
batch_size = 1

input = torch.randn(batch_size,in_channels,width,height)
conv_layer = torch.nn.Conv2d(in_channels, out_channels, kernel_size = kernel_size)

output = conv_layer(input)
print(input.shape)
print(output.shape)#3*3的卷积,图像大小减2
print(conv_layer.weight.shape)

m*n*w*h

 2.padding

如果本来输入的是5*5,kernel是3*3,输出是3*3,如果想要输出不变也是5*5,由于kernel是3*3,那么就需要将input填充一圈0

import torch

input = [3,4,6,5,7,
         2,4,6,8,2,
         1,6,7,8,4,
         9,7,4,6,2,
         3,7,5,4,1]

input = torch.Tensor(input).view(1,1,5,5)
conv_layer = torch.nn.Conv2d(1,1,kernel_size=3,padding=1,bias=False)
kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1,1,3,3)
conv_layer.weight.data = kernel.data
output = conv_layer(input)
print(output)

 

3.stride

之前的例子都是stride等于1,改成等于2后,相当于往后移动的时候跳一格

import torch

input = [3,4,6,5,7,
         2,4,6,8,2,
         1,6,7,8,4,
         9,7,4,6,2,
         3,7,5,4,1]

input = torch.Tensor(input).view(1,1,5,5)
conv_layer = torch.nn.Conv2d(1,1,kernel_size=3,stride=2,bias=False)
kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1,1,3,3)
conv_layer.weight.data = kernel.data
output = conv_layer(input)
print(output)

 4.Max Pooling Layer 最大池化层

2*2的maxpooling默认stride = 2,它将原input分成4个2*2的小块,从每个小块里面选取最大值放入output中,大小缩小一半

import torch

input = [3,4,6,5,
         2,4,6,8,
         1,6,7,8,
         9,7,4,6]

input = torch.Tensor(input).view(1,1,4,4)
maxpooling= torch.nn.MaxPool2d(kernel_size=2)
output = maxpooling(input)
print(output)

 

5.一个简单的卷积神经网络

模型:

class Net(torch.nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = torch.nn.Conv2d(1,10,kernel_size=5)
        self.conv2 = torch.nn.Conv2d(10,20,kernel_size=5)
        self.pooling = torch.nn.MaxPool2d(2)
        self.fc = torch.nn.Linear(320,10)

    def forward(self,x):
        batch_size = x.size(0)
        x = F.relu(self.pooling(self.conv1(x)))
        x = F.relu(self.pooling(self.conv2(x)))
        x = x.view(batch_size,-1)
        x = self.fc(x)
        return x#不做激活,因为做交叉熵损失算softmax

model = Net()

 本文为B站刘二大人pytorch学习视频的笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值