小土堆pytorch学习笔记004

目录

1、神经网络的基本骨架-nn.Module的使用

2、卷积操作实例

3、神经网络-卷积层

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

(1)最大池化画图理解:

(2)代码实现:

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

(1)代码实现(调用sigmoid 函数)

6、神经网络-线性层

(1)代码

7、网络搭建-小实战

(1)完整代码 


1、神经网络的基本骨架-nn.Module的使用

官网地址:pytorch里的nn

import torch
from torch import nn


class Tudui(nn.Module):
    def __init__(self):
        super().__init__()
        
        
    def forward(self, input):
        output = input + 1
        return output


tudui = Tudui()
x = torch.tensor(1.0)
output = tudui(x)
print(output)

小土堆pytorch学习笔记004_第1张图片

2、卷积操作实例

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]])

# 转换成要求的格式 shape(N,C,H,W)
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))

print(input.shape)
print(kernel.shape)
# stride=1 的情况
output = F.conv2d(input, kernel, stride=1)
print(output)

# stride=2 的情况
output2 = F.conv2d(input, kernel, stride=2)
print(output2)

# 设置了padding
output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)

 运行结果:

小土堆pytorch学习笔记004_第2张图片

3、神经网络-卷积层

Conv2d:文档地址torch.nn.Conv2d

小土堆pytorch学习笔记004_第3张图片

小土堆pytorch学习笔记004_第4张图片

小土堆pytorch学习笔记004_第5张图片

in_channels 输入的通道数

out_channels 输出的通道数

kernel_size 卷积核大小

stride  默认为移动为1

padding是否在边缘进行填充

例子:

import torch
import torchvision
import ssl
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

ssl._create_default_https_context = ssl._create_unverified_context

dataset = torchvision.datasets.CIFAR10(root='./test11_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.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0 )

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


tudui = Tudui()
writer = SummaryWriter('test11_logs')
step = 0
for data in dataloader:
    imgs, targets = data
    output = tudui(imgs)
    writer.add_images("input", imgs, step)

    output = torch.reshape(output, (-1, 3, 30, 30), )  # 不知道是多少的时候,直接写-1
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

结果输出: 

小土堆pytorch学习笔记004_第6张图片

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

(1)最大池化画图理解:

小土堆pytorch学习笔记004_第7张图片

(2)代码实现:

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)

运行结果:

小土堆pytorch学习笔记004_第8张图片

(3)展示池化的图片(代码)

import torch
import ssl
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
ssl._create_default_https_context = ssl._create_unverified_context

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

# 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

writer = SummaryWriter("test12_logs_maxpool")
tudui = Tudui()
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()



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




运行结果:

小土堆pytorch学习笔记004_第9张图片

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

非线性激活函数

(1)代码实现(调用sigmoid 函数)

import torch
import ssl
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
ssl._create_default_https_context = ssl._create_unverified_context


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

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

dataset = torchvision.datasets.CIFAR10("./test13_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()
writer = SummaryWriter("test13_logs_sigmoid")
step = 0
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 = step + 1

writer.close()

输出结果:

小土堆pytorch学习笔记004_第10张图片

6、神经网络-线性层

(1)代码

import torch
import ssl
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader
ssl._create_default_https_context = ssl._create_unverified_context

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


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))  # torch.Size([1, 1, 1, 196608])
    # output = torch.flatten(imgs)  # 会变成一行  torch.Size([196608])
    print(output.shape)
    output = tudui(output)
    print(output.shape)

结果展示:

小土堆pytorch学习笔记004_第11张图片

7、网络搭建-小实战

小土堆pytorch学习笔记004_第12张图片

(1)完整代码 

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


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        # self.conv1 = Conv2d(in_channels=3, out_channels=32, kernel_size=5, padding=2)  # 卷积
        # self.maxpool1 = MaxPool2d(2)   # 池化
        # self.conv2 = Conv2d(in_channels=32, out_channels=32, kernel_size=5, padding=2)
        # self.maxpool2 = MaxPool2d(2)
        # self.conv3 = Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2)
        # self.maxpool3 = MaxPool2d(2)
        # self.flatten = Flatten()
        # self.linear1 = Linear(in_features=1024, out_features=64)
        # self.linear2 = Linear(in_features=64, out_features=10)

        self.model1 = Sequential(
            Conv2d(in_channels=3, out_channels=32, kernel_size=5, padding=2),
            MaxPool2d(2),
            Conv2d(in_channels=32, out_channels=32, kernel_size=5, padding=2),
            MaxPool2d(2),
            Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(in_features=1024, out_features=64),
            Linear(in_features=64, out_features=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)

        x = self.model1(x)
        return x

tudui = Tudui()
print(tudui)  # 输出网络结构

input = torch.ones((64, 3, 32, 32))
output = tudui(input)
print(output.shape)

writer = SummaryWriter('test15_logs')
writer.add_graph(tudui, input)
writer.close()

运行结果:

小土堆pytorch学习笔记004_第13张图片

 小土堆pytorch学习笔记004_第14张图片

你可能感兴趣的:(深度学习,pytorch,学习,笔记,机器学习,深度学习)