线性层及其它层介绍

这一节内容做的笔记有些潦草,但内容和代码都与前面的一致

使用步骤

a.代码如下(示例):

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

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

"""class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.linear1=Linear()"""

for data in dataloader:
    imgs,targets=data
    print(imgs.shape)                      #[64,3,32,32]

b.现在我想将图片尺寸改为[1,1,1,任何参数],代码如下:

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

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

"""class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.linear1=Linear()"""

for data in dataloader:
    imgs,targets=data
    print(imgs.shape)                      #[64,3,32,32]
    output=torch.reshape(imgs,[1,1,1,-1])  #将最后一个数让它自己计算
    print(output.shape)

成果如下:

线性层及其它层介绍_第1张图片

 c.当调用函数时,代码如下:

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

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

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.linear1=Linear(196608,10)

    def forward(self,input):
        output=self.linear1(input)
        return output
tudui=Tudui()

for data in dataloader:
    imgs,targets=data
    print(imgs.shape)                      #[64,3,32,32]
    output=torch.reshape(imgs,[1,1,1,-1])  #将最后一个数让它自己计算
    print(output.shape)
    output=tudui(output)
    print(output.shape)

对应的成果如下:

线性层及其它层介绍_第2张图片

 d.当我想用Flatten时,代码书写如下:

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

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

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.linear1=Linear(196608,10)

    def forward(self,input):
        output=self.linear1(input)
        return output
tudui=Tudui()

for data in dataloader:
    imgs,targets=data
    print(imgs.shape)                      #[64,3,32,32]
    output=torch.flatten(imgs)             #将输入层进行展平
    print(output.shape)
    output=tudui(output)
    print(output.shape)

成果展示:

线性层及其它层介绍_第3张图片


总结

参考土堆老师的视频,做的笔记神经网络-线性层及其他层介绍_哔哩哔哩_bilibili

你可能感兴趣的:(神经网络,图像处理,pytorch,计算机视觉,深度学习)