Pytorch的官方教程学习--神经网络的初步理解

最近在学习Pytorch的官方入门教程,本篇只写了关于神经网络这一小知识点的理解:

附:Pytorch的官方入门60min教程链接<https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html>

NEURAL NETWORKS

神经网络可以通过 torch.nn 包来构建。

现在对于自动梯度(autograd)有一些了解,神经网络是基于自动梯度 (autograd)来定义一些模型。一个 nn.Module 包括若干层和一个方法 forward(input) ,它会返回输出(output)。

例如,看一下数字图片识别的网络:

理论上对它的理解:

代码实现:

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


class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 3x3 卷积核square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 5 * 5, 120)  # 6*6 from image dimension
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
 
    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)),2)
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        print(x.shape)#16*5*5
        x = x.view(-1, self.num_flat_features(x))
        print(x.shape)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


net = Net()
print(net)

输出结果:

1.卷积层函数的理解

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

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,添加偏置

2..池化层函数的理解

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

对于输入信号的输入通道,提供1维最大池化(max pooling)操作

如果输入的大小是(N,C,L),那么输出的大小是(N,C,L_out)的计算方式是: 
$$out(N_i, C_j,k)=max^{kernel_size-1}{m=0}input(N{i},C_j,stride*k+m)$$ 

如果padding不是0,会在输入的每一边添加相应数目0 

dilation用于控制内核点之间的距离,详细描述在这里 

参数:
* 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,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作

3.全连接层函数的理解

class torch.nn.Linear(in_features, out_features, bias=True)

对输入数据做线性变换:\(y = Ax + b\)

参数:
* in_features - 每个输入样本的大小
* out_features - 每个输出样本的大小
* bias - 若设置为False,这层不会学习偏置。默认值:True

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值