神经网络构造

一、神经网络骨架:

import torch
from torch import nn


#神经网络
class CLH(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input):
        output=input+1
        return output

clh = CLH()
x = torch.tensor(1.0)
output = clh(x)
print(output)

二、卷积操作:

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]
])
#变化为卷积规定输入格式的维度:这里二维(x,y)转四维(t,z,x,y)
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))

#对输入矩阵的上下左右进行分别padding扩充1列0,执行一次stride步长为1的卷积
output = F.conv2d(input, kernel, stride=1, padding=1)
print(output)

运行结果:
在这里插入图片描述

执行过程:
在这里插入图片描述

三、卷积层:

卷积在神经网络中用于提取输入数据的特征,通过与卷积核进行卷积操作来实现特征的提取和学习。

import torchvision
from torch import nn
from torch.utils.data import DataLoader
import torch.nn.functional as F

test_data = torchvision.datasets.CIFAR10("./dataset",train=False,transform=torchvision.transforms.ToTensor(),download=False)
#将数据划分为batch,每个batch有64个样本
dataloader = DataLoader(test_data,batch_size=64)

#神经网络
class CLH(nn.Module):
    def __init__(self):
        super(CLH,self).__init__()
        #神经网络中设置一个卷积层,in_channels表示输入通道数,out_channels表示输出通道数,并且卷积核尺寸为3×3的随机矩阵
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
    def forward(self,input):
        #对输入数据执行一次二维卷积
        return self.conv1(input)

clh = CLH()

#data是一个batch
for data in dataloader:
    imgs,targets = data
    output = clh(imgs)
    #torch.Size([64, 3, 32, 32])表示[batchs大小,每个batch的通道数,每个通道x轴像素数,每个通道y轴像素数]
    print(imgs.shape)
    #torch.Size([64, 6, 30, 30])表示[batchs大小,每个batch的通道数,每个通道x轴像素数,每个通道y轴像素数]
    #其中每个通道由32×32像素变为30×30像素,其余的像素点组合成该batch的其他通道
    print(output.shape)

在这里插入图片描述

四、池化层:

最大池化的作用是为了保留特征同时将数据量缩小。
例如:1080p图像经过最大池化层变为720p。

import torch
from torch import nn
from torch.nn import MaxPool2d
#输入像素变为tensor类型
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)
#变化为池化规定输入格式的维度:这里二维(x,y)转四维(t,z,x,y)
input = torch.reshape(input,(1,1,5,5))

#神经网络
class CLH(nn.Module):
    def __init__(self):
        super(CLH,self).__init__()
        #神经网络中设置一个池化层,ceil_mode表示池化合覆盖输入数据不够时是否计算
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)
    def forward(self,input):
        #对输入数据执行一次最大池化操作
        return self.maxpool1(input)

#创建神经网络
clh = CLH()

output = clh(input)
print(output)

运行结果:
在这里插入图片描述

执行过程:
1

五、激活函数(以ReLU为例):

import torch
from torch import nn
from torch.nn import MaxPool2d, ReLU

#输入像素变为tensor类型
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)
#变化为池化规定输入格式的维度:这里二维(x,y)转四维(t,z,x,y)
input = torch.reshape(input,(1,1,5,5))

#神经网络
class CLH(nn.Module):
    def __init__(self):
        super(CLH,self).__init__()
        #神经网络中设置一个激活函数
        self.relu = ReLU()
    def forward(self,input):
        #对输入数据执行一次最大池化操作
        return self.relu(input)

#创建神经网络
clh = CLH()

output = clh(input)
print(output)

运行结果:
在这里插入图片描述

相关推荐

  1. pytorch基础 神经网络构建

    2024-07-17 22:04:04       42 阅读
  2. 深度学习 - 构建神经网络

    2024-07-17 22:04:04       25 阅读
  3. 构建神经网络的流程是什么?

    2024-07-17 22:04:04       48 阅读
  4. 机器学习神经网络由哪些构成?

    2024-07-17 22:04:04       34 阅读
  5. 干货分享|TensorFlow构建神经网络

    2024-07-17 22:04:04       37 阅读
  6. 使用torch.nn.Sequential构建神经网络

    2024-07-17 22:04:04       28 阅读
  7. 使用torch.nn.ModuleList构建神经网络

    2024-07-17 22:04:04       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 22:04:04       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 22:04:04       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 22:04:04       62 阅读
  4. Python语言-面向对象

    2024-07-17 22:04:04       72 阅读

热门阅读

  1. 【C++】C++中的堆和栈介绍和区别

    2024-07-17 22:04:04       25 阅读
  2. httpClient传输文件

    2024-07-17 22:04:04       22 阅读
  3. 关于Apache Iceberg

    2024-07-17 22:04:04       24 阅读
  4. lightgbm

    lightgbm

    2024-07-17 22:04:04      31 阅读
  5. ansible报错--‘when‘ is not a valid attribute for a Play

    2024-07-17 22:04:04       26 阅读
  6. [C/C++入门][for]24、菲波那契数列

    2024-07-17 22:04:04       27 阅读
  7. Linux C++ 060-设计模式之单例模式

    2024-07-17 22:04:04       22 阅读
  8. 使用axios实现vue web前端无痕刷新

    2024-07-17 22:04:04       23 阅读
  9. 调试和测试PER语言代码的最佳实践

    2024-07-17 22:04:04       23 阅读
  10. 不同行业的私域引流策略有何差异?

    2024-07-17 22:04:04       21 阅读
  11. 算法训练营day72

    2024-07-17 22:04:04       25 阅读