线性层及其pytorch使用

查看官方的文档:Linear — PyTorch 1.10 documentation

CLASS torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)

使用线性层是一般只需要三个参数:in_features,out_features,bias

线性层及其pytorch使用_第1张图片
 

 其中x1,x2.....xd x的个数就是in_features,在这里就是d

g1,g2......gL,  g的个数就是out_features,在这里就是L

g1为例:g1=k1*x1+b1

                       +k2*x2+b2

                       +k3*x3+b3

              +……+kd*xd+bd

其中bias偏置为ture就意味着有后缀b。

而至于k与b的取值问题,则不需要我们考虑。

因为  :pytorch已经为我们求好了,我们只需要提供in_features和out_features和bias即可线性层及其pytorch使用_第2张图片

 下面进行代码实践:

首先计算in_features的大小

import torch
import torchvision.datasets
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader

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



for data in dataloader:
    imgs,targets = data
    print(imgs.shape)#运行的尺寸为torch.Size([64, 3, 32, 32])
    output = torch.reshape(imgs,(1,1,1,-1))#要想使用线形层,我们需要变成为(1,1,1,?)这样格式的。
    # 即 batch_size=1,in_channels=1,图片是1*?的格式
    print(output.shape)#经过运行结果为torch.Size([1, 1, 1, 196608]),那么我们使用Linear的in_features=196608

下面使用线性层:

import torch
import torchvision.datasets
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader

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

class sza_(nn.Module):
    def __init__(self):
        super(sza_, self).__init__()
        self.linear1 = Linear(196608,10,bias=True)#bias默认为ture,可以不写
    def forward(self,input):
        output = self.linear1(input)
        return output


sza__ = sza_()

for data in dataloader:
    imgs,targets = data
    print(imgs.shape)#运行的尺寸为torch.Size([64, 3, 32, 32])
    output = torch.reshape(imgs,(1,1,1,-1))#要想使用线形层,我们需要变成为(1,1,1,?)这样格式的。
    # 即 batch_size=1,in_channels=1,图片是1*?的格式
    # print(output.shape)#经过运行结果为torch.Size([1, 1, 1, 196608]),那么我们使用Linear的in_features=196608
    output = sza__(output)
    print(output.shape)#运行结果为torch.Size([1, 1, 1, 10])
    #此时代码可以运行,但是到最后一次会报错

可以运行,但是到循环的最后一次会出现错误:

线性层及其pytorch使用_第3张图片

 

 问题解决:由于我们设置的in_features是固定的,然而在我们的数据集中,由于dataloader的batch_size为64,因而in_features大部分为196608,然而最后dataloader的drop_last默认为false,因而最后一组为16张,故而不满足in_features。解决办法就是将drop_last设置为true

接下来我们使用torch.flatten

t = torch.tensor([[[1, 2],
                   [3, 4]],
                  [[5, 6],
                   [7, 8]]])
torch.flatten(t)
#tensor([1, 2, 3, 4, 5, 6, 7, 8])
torch.flatten(t, start_dim=1)
#tensor([[1, 2, 3, 4],
#       [5, 6, 7, 8]])

 

import torch
import torchvision.datasets
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("./DXAA",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader =DataLoader(dataset,batch_size=64,drop_last=True)

class sza_(nn.Module):
    def __init__(self):
        super(sza_, self).__init__()
        self.linear1 = Linear(196608,10,bias=True)#bias默认为ture,可以不写
    def forward(self,input):
        output = self.linear1(input)
        return output


sza__ = sza_()

for data in dataloader:
    imgs,targets = data
    print(imgs.shape)#运行的尺寸为torch.Size([64, 3, 32, 32])
    # output = torch.reshape(imgs,(1,1,1,-1))#要想使用线形层,我们需要变成为(1,1,1,?)这样格式的。
    # 即 batch_size=1,in_channels=1,图片是1*?的格式
    output1 = torch.flatten(imgs)
    # print(output.shape)#经过运行结果为torch.Size([1, 1, 1, 196608]),那么我们使用Linear的in_features=196608
    print(output1.shape)#torch.Size([196608])
    output1 = sza__(output1)
    print(output1.shape)#运行结果为torch.Size([1, 1, 1, 10])#output1.shape = torch.Size([10])
    #此时代码可以运行,但是到最后一次会报错

你可能感兴趣的:(啥也不会的pytorch,pytorch,深度学习,机器学习)