【pytorch学习】神经网络基本骨架

文档地址:Module — PyTorch 2.4 documentation

1 nn.Module

新建一个名为test_nnMudule.py的文件

继承父类nn.Module

import torch
from torch import nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

    def forward(self, x):
        y = 2*x +1
        return y

net = Net()
x1 = torch.tensor(1.0)
y1 = net.forward(x1)

x2 = torch.tensor([1.0,2])
y2 = net.forward(x2)
print(y1)
print(y2)

运行结果

2 卷积层conv

针对二维图像的卷积:Conv2d — PyTorch 2.4 documentation

相关参数:

  • in_channels (int) – 输入图像中的通道数(例如RGB图像的通道数为3)

  • out_channels (int) – 卷积后产生的通道数(跟卷积核的数量相关)

  • kernel_size (int or tuple) – 卷积核大小

  • stride (int or tupleoptional) – 卷积的步长 (Default: 1)

  • padding (inttuple or stroptional) – 对输入图像四个边的填充 Default: 0

 实例:

import torchvision
from torch import nn
from torch.utils.data import dataloader

data_transform = torchvision.transforms.ToTensor()#将PIL.Image转换为torch.FloatTensor
test_data = torchvision.datasets.CIFAR10('../dataset/cifar10', train=False, download=False, transform=data_transform)
dataloade = dataloader.DataLoader(test_data, batch_size=64, shuffle=True)
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 3,stride=1)

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

net = Net()

for data in dataloade:
    imgs, targets = data
    print(imgs.shape)
    output = net.forward(imgs)
    print(output.shape)
    break

假如,当前输入是大小为 n X n 的RGB图像(3, n ,n ),通道 C = 3

卷积核大小为 F X F,有 M 个卷积核,卷积的步长sterde为 S,padding为 P 

计算公式为 A = [(n + 2P - F) / S] +1

经过卷积后的输出为:(M , A , A)

3 池化pooling

文档地址:MaxPool2d — PyTorch 2.4 documentation

相关参数:

  • kernel_size (Union[intTuple[intint]]) – the size of the window to take a max over

  • stride (Union[intTuple[intint]]) – the stride of the window. Default value is kernel_size

  • padding (Union[intTuple[intint]]) – Implicit negative infinity padding to be added on both sides

  • ceil_mode (bool) – 当为True时,将使用ceil来计算输出形状,而不是floor

举例:

 当一个5X5的矩阵使用3X3的池化核来进行池化时,ceil_mode 的取值不同则对应的输出可能也会不同

 实例:

import torch
from torch import nn
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)
print(input.shape)
input = input.reshape(-1, 1, 5, 5)#-1表示自动计算
# 这四个数字分别代表batch_size, channel, height, width
print(input.shape)

class net(nn.Module):
    def __init__(self):
        super(net, self).__init__()
        self.maxpool1 = nn.MaxPool2d(2, 2)

    def forward(self, x):
        y = self.maxpool1(x)
        return y

net = net()
output = net.forward(input)

4 非线性激活

文档地址:torch.nn — PyTorch 2.4 documentation

常见的激活函数:relu,sigmoid

实例:

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

input = torch.tensor([[1, -0.5],
                      [-1, 3]])
data_transform = torchvision.transforms.ToTensor()
dataset = torchvision.datasets.CIFAR10('../dataset/cifar10', train=False, download=False, transform=data_transform)

dataloader = DataLoader(dataset, batch_size=64, shuffle=True)

class net(torch.nn.Module):
    def __init__(self):
        super(net, self).__init__()
        self.relu = torch.nn.ReLU()

    def forward(self, x):
        y = self.relu(x)
        return y

net = net()

 

  • 22
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神通广大白居易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值