首页 > 其他分享 >学习笔记(b站小土堆)

学习笔记(b站小土堆)

时间:2024-07-29 21:24:46浏览次数:20  
标签:input nn output self torch 笔记 学习 import 土堆

一、torchvision中的数据集使用

CIFAR10

结果,图片展示为一只猫

二、DataLoader的使用

结果

测试集当中的第一个数据是一个三通道(RGB),是个彩色的图片,尺寸为32*32 ,对应的target是3

batch_size=4就是从当中去取test_data[0]、test_data[1]、test_data[2]、test_data[3],把对应的img打包

结果

由于batch_size=4,所以将四个进行打包,torch.Size([4,3,32,32])表示从dataset当中取了4张图片,三通道,32*32,同时还把4个target进行打包,每张图片对应的5、9、0、0

用img.add()查看图片

结果可以发现图片被四个四个打包在一起

当改变batch_size=64时,也许改变tag

发现最后一张图不够64,这是因为drop_last=False的缘故将drop_last=True(注意也要修改tag)则可得到最后一步step155为64张,把step=156舍去 

shuffle=False表示每次抓取的数据是一样的

shuffle=True 【 test_loader = DataLoader(dataset=test_data,batch_size=64,shuffle=True , num_workers=0,drop_last=True) 】时

相同的步骤但图片顺序不同

三、神经网络骨架的基本搭建(torch.nn中containers中module)

定义一个类Model,继承父类nn.Module,在__init__()函数与forward()函数中写必要的条件

神经网络前向传播

自己搭建

调用

四、卷积操作

Conv2d — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d

1、理解stride

代码

import torch
import torch.nn.functional as F

input = torch.tensor([[1,2,0,3,1],   #连续中括号数,几个就是几维矩阵
                      [0,1,2,3,1],
                      [1,2,1,0,0],
                      [5,2,3,1,1],
                      [2,1,0,1,1]])
kernel = torch.tensor([[1,2,1],
                       [0,1,0],
                       [2,1,0]])

input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))

print(input.shape)
print(kernel.shape)

output = F.conv2d(input,kernel,stride=1)
print(output)

output2 = F.conv2d(input,kernel,stride=2)
print(output2)
结果

2、理解padding

output3 = F.conv2d(input,kernel,stride=1,padding=1 )
print(output3)

结果

五、神经网络-卷积层

Conv2d — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#conv2d

  • in_channels (int) – Number of channels in the input image

  • out_channels (int) – Number of channels produced by the convolution

  • kernel_size (int or tuple) – Size of the convolving kernel

  • stride (int or tupleoptional) – Stride of the convolution. Default: 1

  • padding (inttuple or stroptional) – Padding added to all four sides of the input. Default: 0

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

dataset = torchvision.datasets.CIFAR10("./data",train=False ,transform=torchvision.transforms.ToTensor(),
                                       download=True  )#需要测试数据集,把数据类型转换成tensor数据类型
dataloader = DataLoader(dataset,batch_size=64) #把数据集放在dataloader中加载

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui,self).__init__()
        self.conv1 = Conv2d(3,6,3,1,0)

    def forward(self,x):
        x = self.conv1(x)
        return x

tudui = Tudui()

for data in dataloader:
    imgs,targets = data
    output = tudui(imgs)
    print(imgs.shape)
    print(output.shape)

dataloader加载 batch_size=64,in_channels=3(3层彩色图像),32*32大小,经过卷积之后in_channels=6,30*30大小

通过tensorboard查看

tudui = Tudui()

writer = SummaryWriter("./llogs")
step=0
for data in dataloader:
    imgs,targets = data
    output = tudui(imgs)
    print(imgs.shape)
    print(output.shape)
    # torch.Size([64, 3, 32, 32])
    writer.add_images("input",imgs,step)
    # torch.Size([64, 6, 30, 30]) ->[xxx,3,30,30]
    output = torch.reshape(output,(-1,3,30,30))
    writer.add_images("output",output,step)
    step = step+1

由于卷积过后图像变为6通道,无法显示,这里将输出图像从6 通道变为3通道,多余的像素点也显示,故输出有64*2张图

六、神经网络-最大池化的使用

MaxPool2d — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html#maxpool2d

默认padding为kernel_size的大小,即为3

import torch
from torch import nn
from torch.nn import MaxPool2d

input = torch.tensor([[1,2,0,3,1],
                      [0,1,2,3,1],
                      [1,2,1,0,0],
                      [5,2,3,1,1],
                      [2,1,0,1,1]],dtype=torch.float32) #变为浮点数据类型
input = torch.reshape(input,(-1,1,5,5))
print(input.shape)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui,self ).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode= True)

    def forward(self,input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()
output = tudui(input)
print(output)


当ceil_mode= False时

用tensorboard查看

import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

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

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui,self ).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode= False )

    def forward(self,input):
        output = self.maxpool1(input)
        return output

tudui = Tudui()

writer = SummaryWriter("logs_maxpool")
step = 0

for data in dataloader:
    imgs,targets=data
    writer.add_images("input",imgs,step)
    output = tudui(imgs)
    writer.add_images("output",output,step)
    step = step+1

writer.close()

七、神经网络-非线性激活

ReLU — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.ReLU.html#relu

参数inplace的作用

import torch
from torch import nn
from torch.nn import ReLU

input = torch.tensor([[1,-0.5],
                      [-1,3]])

input = torch.reshape(input,(-1,1,2,2))
print(input.shape)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui ,self).__init__()
        self.relu1 = ReLU()

    def forward(self,input):
        output = self.relu1(input)
        return output

tudui = Tudui()
output = tudui(input)
print(output)

可以看出来-0.5 -1都变为0

nn.ReLU在图像显示上不明显,换为nn.LogSigmoid

import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

input = torch.tensor([[1,-0.5],
                      [-1,3]])

input = torch.reshape(input,(-1,1,2,2))
print(input.shape)

dataset = torchvision.datasets.CIFAR10("../data",train=False,download=True,
                                       transform=torchvision.transforms.ToTensor())

dataloader = DataLoader(dataset,batch_size=64)

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui ,self).__init__()
        self.relu1 = ReLU()
        self.sigmoid1 = Sigmoid()

    def forward(self,input):
        output = self.sigmoid1(input)
        return output

tudui = Tudui()
step = 0
writer = SummaryWriter("../loga_relu")
for data in dataloader:
    imgs,targets = data
    writer.add_images("input",imgs,global_step=step)
    output = tudui(imgs)
    writer.add_images("output",output,step)
    step += 1

writer.close()

八、神经网络-线性层及其它层介绍

Linear — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.Linear.html#linear

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

dataset = torchvision.datasets.CIFAR10("../data",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)
    output = torch.reshape(imgs,(1,1,1,-1))
    print(output.shape)
    output = tudui(output)
    print(output.shape)

可用   output = torch.flatten(imgs)   展平

九、神经网络-搭建小实战和sequential的使用

Sequential — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html#sequentialCIFAR10的 网络结构

from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear


class Yebi(nn.Module):
    def __init__(self):
        super(Yebi, self).__init__()
        self.conv1 = Conv2d(3,32,5,padding=2)
        self.maxpool1 = MaxPool2d(2)
        self.conv2 = Conv2d(32,32,5,padding=2)
        self.maxpool2 = MaxPool2d(2)
        self.conv3 = Conv2d(32,64,5,padding=2)
        self.maxpool3 = MaxPool2d(2)
        self.flatten = Flatten()
        self.linear1 = Linear(1024,64)
        self.linear2 = Linear(64,10)

    def forward(self,x):
        x = self.conv1(x)
        x = self.maxpool1(x)
        x = self.conv2 (x)
        x = self.maxpool2(x)
        x = self.conv3 (x)
        x = self.maxpool3 (x)
        x = self.flatten (x)
        x = self.linear1 (x)
        x = self.linear2 (x)
        return x

tudui = Yebi()
print(tudui)

sequential的使用

import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential


class Yebi(nn.Module):
    def __init__(self):
        super(Yebi, self).__init__()
        self.model1 = Sequential(
            Conv2d(3,32,5,padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self,x):
        x = self.model1(x)
        return x

tudui = Yebi()
print(tudui)

十、损失函数与反向传播

L1Loss

L1Loss — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.L1Loss.html#l1loss

import torch
from torch.nn import L1Loss

inputs = torch.tensor([1,2,3],dtype=torch.float32)
targets = torch.tensor([1,2,5],dtype=torch.float32)

inputs = torch.reshape(inputs,(1,1,1,3))  #batch_size=1,1通道,1行3列
targets = torch.reshape(targets,(1,1,1,3))

loss = L1Loss()
result = loss(inputs, targets)

print(result)

 [(1-1)+(2-2)+(5-3)] /3

L1Loss()可以指定计算方法,如  loss = L1Loss(reduction='sum')

(1-1)+(2-2)+(5-3)

则结果为

MSELoss 

MSELoss — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html#mseloss

import torch
from torch import nn
from torch.nn import L1Loss

inputs = torch.tensor([1,2,3],dtype=torch.float32)
targets = torch.tensor([1,2,5],dtype=torch.float32)

inputs = torch.reshape(inputs,(1,1,1,3))  #batch_size=1,1通道,1行3列
targets = torch.reshape(targets,(1,1,1,3))

loss = L1Loss(reduction='sum')
result = loss(inputs, targets)

loss_mse = nn.MSELoss()
result_mse = loss_mse(inputs,targets)

print(result)
print(result_mse)

结果

 CrossEntropyLoss

CrossEntropyLoss — PyTorch 2.4 documentationicon-default.png?t=N7T8https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#crossentropyloss

 

import torch
from torch import nn
from torch.nn import L1Loss

inputs = torch.tensor([1,2,3],dtype=torch.float32)
targets = torch.tensor([1,2,5],dtype=torch.float32)

inputs = torch.reshape(inputs,(1,1,1,3))  #batch_size=1,1通道,1行3列
targets = torch.reshape(targets,(1,1,1,3))

loss = L1Loss(reduction='sum')
result = loss(inputs, targets)

loss_mse = nn.MSELoss()
result_mse = loss_mse(inputs,targets)

print(result)
print(result_mse)

x = torch.tensor([0.1,0.2,0.3])
y = torch.tensor([1])
x = torch.reshape(x,(1,3))
loss_cross = nn.CrossEntropyLoss()
result_cross = loss_cross(x,y)
print(result_cross)

 根据一个网络模型,用CrossEntropyLoss计算实际输出和目标之间的差距

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter


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

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = Sequential(
            Conv2d(3,32,5,padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self,x):
        x = self.model1(x)
        return x

loss = nn.CrossEntropyLoss ()
tudui = Tudui()
for data in dataloader:
    imgs,targets = data
    outputs = tudui(imgs)
    result_loss = loss(outputs,targets)
    print(result_loss)


 计算的结果就是神经网络输出 与真实输出之间的误差

调用损失函数的backwards,会得到反向传播,可以求出每个需要调节的参数,以及每个参数对应的梯度,然后利用优化器,优化器根据梯度,对参数进行调整,达到误差整体降低的目的。

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader


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

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = Sequential(
            Conv2d(3,32,5,padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self,x):
        x = self.model1(x)
        return x

loss = nn.CrossEntropyLoss ()
tudui = Tudui()
optim = torch.optim.SGD(tudui.parameters() ,0.01)
for data in dataloader:
    imgs,targets = data
    outputs = tudui(imgs)
    result_loss = loss(outputs,targets) #计算网络和目标的差距
    optim.zero_grad()         #把网络中每个可调节的参数设为0
    result_loss.backward()     #得到每一个可调节参数对应的梯度
    optim.step()             #调用优化器 对每个参数进行调优


import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader


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

class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        self.model1 = Sequential(
            Conv2d(3,32,5,padding=2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, padding=2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self,x):
        x = self.model1(x)
        return x

loss = nn.CrossEntropyLoss ()
tudui = Tudui()
optim = torch.optim.SGD(tudui.parameters() ,0.01)
for epoch in range(20):
    running_loss = 0.0
    for data in dataloader:
        imgs,targets = data
        outputs = tudui(imgs)
        result_loss = loss(outputs,targets) #计算网络和目标的差距
        optim.zero_grad()         #把网络中每个可调节的参数设为0
        result_loss.backward()     #得到每一个可调节参数对应的梯度
        optim.step()             #调用优化器 对每个参数进行调优
        running_loss = running_loss + result_loss  #每一轮误差的总和
    print(running_loss)

可以看到每一轮的误差总和在减小

标签:input,nn,output,self,torch,笔记,学习,import,土堆
From: https://blog.csdn.net/weixin_57736375/article/details/140668851

相关文章

  • PyTorch深度学习快速入门(中)
    PyTorch深度学习快速入门(中)一、Containers(神经网络的基本骨架)(一)Module的使用(二)Sequential的使用<搭建小实战>二、ConvolutionLayers(卷积层)(一)torch.nn.functional中conv2d的使用(二)torch.nn中Conv2d的使用三、Poolinglayers(池化层)(一)池化层的介绍(二)MaxPool2d的......
  • 函数的学习(一)
    1.定义C语言的函数是一段可被重复调用的代码块,可以执行特定的任务并返回一个值。每个函数由函数头、函数体和函数返回值组成。2.函数的分类C语言中的函数可以根据不同的特性进行分类,常见的分类如下:(1)标准函数(库函数):这些函数是C语言提供的预定义函数,可以直接在程序中调用。标......
  • 02 Go语言开发REST API接口_20240728 课程笔记
    概述如果您没有Golang的基础,应该学习如下前置课程。Golang零基础入门Golang面向对象编程GoWeb基础基础不好的同学每节课的代码最好配合视频进行阅读和学习,如果基础比较扎实,则阅读本教程巩固一下相关知识点即可,遇到不会的知识点再看视频。视频课程最近发现越来越多的......
  • 《Redis设计与实现》读书笔记-客户端
    目录1.Client简介2.客户端属性1)(本文重点)比较通用的属性2)(后续分享)另外一类是和特定功能相关的属性2.1套接字文件描述符2.2名字2.3标志(flag)2.4输入缓冲区2.5命令参数和个数2.6命令函数2.7输出缓冲区3.总结1.Client简介Redis服务器是典型的一对多服务器程序:一个......
  • 2024年必备技能:小红书笔记评论自动采集,零基础也能学会的方法
    摘要:面对信息爆炸的2024年,小红书作为热门社交平台,其笔记评论成为市场洞察的金矿。本文将手把手教你,即便编程零基础,也能轻松学会利用Python自动化采集小红书笔记评论,解锁营销新策略,提升个人竞争力。一、引言:为什么选择小红书数据采集?在小红书这片内容营销的热土上,笔记评论蕴......
  • 小红书笔记评论采集全攻略:三种高效方法教你批量导出
    摘要:本文将深入探讨如何利用Python高效采集小红书平台上的笔记评论,通过三种实战策略,手把手教你实现批量数据导出。无论是市场分析、竞品监测还是用户反馈收集,这些技巧都将为你解锁新效率。一、引言:小红书数据金矿与采集挑战在社交电商领域,小红书凭借其独特的UGC内容模式,积累......
  • 学习教务管理系统的记录
    学习教务管理系统的记录最近学习了基础的c++以及mysql的知识,迫不及待寻找紧密相关的项目来巩固所学知识,经过筛选找了一个教务管理系统的项目,总共一千多行代码。感兴趣可以去GitHub查看原代码很多同学觉得教务系统烂大街了,也觉得简单,但是事实上真正写一个就知道并非想的......
  • JAVA小白学习日记Day12
    CSS定位1.定位属性 在CSS中,position属性用于指定元素在文档流中的定位方式。常用的取值包括:static:默认值,元素遵循正常的文档流布局,不受top、right、bottom、left属性的影响。relative:元素相对于其正常位置进行定位,通过top、right、bottom、left属性可以调整元素相......
  • Trick 学习笔记(1)实数范围内随机
    实数范围内随机学习笔记有一些题目很好玩,它的随机不是在有限整数范围内,而是在实数范围内随机,然后让你算什么什么的期望,而这个期望往往又是并不复杂的分数。在线段上任取\(n\)点就是经典例子。看起来很简单,但是一旦跟无穷相关,感觉不积分不太可做。可惜,我并不会积分,去世!现在......
  • ssm+vue大学生互助学习平台【开题+程序+论文】-计算机毕业设计
    本系统(程序+源码)带文档lw万字以上文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景在当今信息化高速发展的时代,高等教育正经历着前所未有的变革。随着知识更新速度的加快和学习方式的多样化,大学生对于高效、灵活的学习资源及互助机制......