• pytorch初学笔记(十二):神经网络基本结构之线性层


    目录

    一、Linear线性层

    1.1 线性层简介

    1. 2 使用参数介绍

    二、代码实战

    2.1 输入数据集

    2.2 展平输入向量为1*1*x

    2.2.1 使用torch.reshape方法展平 

      2.2.2 使用torch.flatten方法展平

    2.3 构建神经网络

    2.4 完整代码


    一、Linear线性层

    官方文档:Linear — PyTorch 1.13 documentation

    1.1 线性层简介

     CLASS  torch.nn.Linear(in_featuresout_featuresbias=Truedevice=Nonedtype=None)

    每一层的某个神经元的值都为前一层所有神经元的值的sum和。 

    1. 2 使用参数介绍

     基于公式y=wx+b计算,w为权重,x为输入,b为偏置值。 

     Parameters:

    • in_features,输入特征 (int) – size of each input sample,输入的tensor大小

    • out_features,输出特征 (int) – size of each output sample,输出的tensor大小

    • bias (bool) – If set to False, the layer will not learn an additive bias. Default: True

     

    Variables: 

    • weight (torch.Tensor) – the learnable weights of the module of shape

    • bias – the learnable bias of the module of shape 

     

     

    二、代码实战

     

     我们想要像上图中vgg model那样,输入一个 1*1*x的向量,线性变换后输出一个1*1*y的向量,因此我们需要获取到输入向量的宽数值,然后自己设定想要输出的宽值y即可。

     由于我们使用的数据集是cifar10,设置的batchsize=64,因此我们首先需要把图片集的(64,3,32,32)的大小使用reshape方法展平为(1,1,1,x)的大小,再作为输入向量输入神经网络中,x的大小可以在参数中设置为-1,让电脑为我们计算具体为多少值。

     

    2.1 输入数据集

    1. dataset = torchvision.datasets.CIFAR10(root="./CIFAR10", train=False, transform=torchvision.transforms.ToTensor(),
    2. download=True)
    3. dataloader = DataLoader(dataset, batch_size=64)

     

    2.2 展平输入向量为1*1*x

    输入的数据集的size参数为(64,3,32,32), torch.size()中的四个参数分别代表:(batch size, channel, height, width)。即一批图片共64张,每张图片三个通道,长*宽为32*32。

    有两种方法可以将向量展平,reshape和flatten,reshape比flatten更加灵活,因为可以自己指定尺寸大小,flatten只能把向量拉平成一条直线。

    2.2.1 使用torch.reshape方法展平 

    我们想要将其转换为batchsize=1,每张图片一个通道,长为1,宽为未知的图片;因为未知宽,因此在使用reshape方法设置参数时即可把宽的数值设置为-1,让电脑自己进行计算。

    1. for data in dataloader:
    2. imgs, lables = data
    3. print(imgs.shape)
    4. out = torch.reshape(imgs, (1, 1, 1, -1))
    5. print(out.shape)

     

     

      2.2.2 使用torch.flatten方法展平

    1. for data in dataloader:
    2. imgs, lables = data
    3. print(imgs.shape)
    4. input = torch.flatten(imgs)
    5. print(input.shape)
    6. output = maweiyi(input)
    7. print(output.shape)

     可以看到调整后的宽长度为196608。 

     

    2.3 构建神经网络

    设定Linear类中的in_feature和out_feature参数,前者为196608,后者我们想要输出一个宽为10的张量,因此设定为10。

    1. class Maweiyi(torch.nn.Module):
    2. def __init__(self):
    3. super(Maweiyi, self).__init__()
    4. self.linear1 = Linear(196608, 10)
    5. def forward(self, input):
    6. output = self.linear1(input)
    7. return output

    2.4 完整代码

    1. import torch
    2. import torchvision.datasets
    3. from torch.nn import Linear
    4. from torch.utils.data import DataLoader
    5. dataset = torchvision.datasets.CIFAR10(root="./CIFAR10", train=False, transform=torchvision.transforms.ToTensor(),
    6. download=True)
    7. dataloader = DataLoader(dataset, batch_size=64)
    8. class Maweiyi(torch.nn.Module):
    9. def __init__(self):
    10. super(Maweiyi, self).__init__()
    11. self.linear1 = Linear(196608, 10)
    12. def forward(self, input):
    13. output = self.linear1(input)
    14. return output
    15. maweiyi = Maweiyi()
    16. for data in dataloader:
    17. imgs, lables = data
    18. print(imgs.shape)
    19. input = torch.flatten(imgs)
    20. print(input.shape)
    21. output = maweiyi(input)
    22. print(output.shape)

     可以看到输入的向量大小为64*3*32*32,展平后为1*1*1*196608,线性变化后为1*1*1*10,实现变换。

  • 相关阅读:
    acwing算法基础之基础算法--快速排序
    人大女王大学金融硕士项目——披星戴月走过的路,一定可以繁花满地
    人工智能人脸识别系统,人工智能应用人脸识别
    Excel 函数教程之如何提取字符串部分内容特殊字符,六套完整解决方案 (教程含源码)
    滚雪球学Java(09-6):Java中的条件运算符,你真的掌握了吗?
    父亲节 | 10位名家笔下的父亲,读懂那份孤独而深沉的父爱
    00|漫展人物备注
    VUE 笔记 基础语法篇
    超级详细 的 Redis 安装教程
    全排列的代码
  • 原文地址:https://blog.csdn.net/weixin_45662399/article/details/128000955