神经网络基本使用

1. 卷积层 convolution layers

python 复制代码
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

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

class Mynn(nn.Module):
    def __init__(self):
        super(Mynn, 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


mynn = Mynn()

writer = SummaryWriter('logs')
step = 0

for data in dataloader:
    imgs, targets = data
    output = mynn(imgs)
    print(imgs.shape)
    #torch.Size([64, 3, 32, 32])
    print(output.shape)
    #torch.Size([64, 6, 30, 30])

    output = torch.reshape(output, (-1, 3, 30, 30)) #-1即通道数改变后网络自动计算新的batchsize数量

    writer.add_images('nn_conv2d', output,step) #output为6维 不能直接显示 需要先转换维
    step += 1

writer.close()

2. 最大池化 maxpooling layers

python 复制代码
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('./dataset',train=False,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 Mynn(nn.Module):
    def __init__(self):
        super(Mynn,self).__init__()
        self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)

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

mynn = Mynn()

# output=mynn(input)
# print(output)

writer =  SummaryWriter('logs')

for epoch in range(3):
    step = 0
    for data in dataloader:
        imgs, targets = data
        writer.add_images('epoch_imgs:{} '.format(epoch),imgs,step)
        output = mynn(imgs)
        # print(output.shape)
        writer.add_images('epoch_maxpool:{} '.format(epoch), output, step)
        step = step+1

writer.close()

3. 非线性激活

python 复制代码
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)
#
# class NnRelu(nn.Module):
#     def __init__(self):
#         super(NnRelu, self).__init__()
#         self.relu1 = ReLU(inplace=False)
#
#     def forward(self, input):
#         output = self.relu1(input)
#         return  output
#
# nnrelu = NnRelu()
#
# output = nnrelu(input)
# print(output.shape)
# print(output)

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


class NnSigmoid(nn.Module):
    def __init__(self):
        super(NnSigmoid, self).__init__()
        self.sigmoid1 = Sigmoid()

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

nnsig = NnSigmoid()

writer = SummaryWriter('logs')

step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images('input', imgs, step)
    output = nnsig(imgs)
    writer.add_images('output', output, step)
    step = step+1
    
writer.close()

4. 线性层和其他

python 复制代码
import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader

#仿照VGG16最后的展平操作  1X1X4096->1X1x1000

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

class NnLinear(nn.Module):
    def __init__(self):
        super(NnLinear, self).__init__()
        self.linear1 = Linear(196608, 10) #这里的inpt_fetures为下方提前算出的196608 输出10自己设定的

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

nnreliear = NnLinear()

for data in dataloader:
    imgs, targets = data
    print(imgs.shape)
    # input = torch.reshape(imgs,(1,1,1,-1))
    # print(input.shape)
    # # input = imgs.reshape(1, 1, 1, -1)
    input = torch.flatten(imgs)
    print(input.shape)
    output = nnreliear(input)
    print(output.shape)
相关推荐
hyshhhh16 小时前
【算法岗面试题】深度学习中如何防止过拟合?
网络·人工智能·深度学习·神经网络·算法·计算机视觉
薛定谔的猫-菜鸟程序员16 小时前
零基础玩转深度神经网络大模型:从Hello World到AI炼金术-详解版(含:Conda 全面使用指南)
人工智能·神经网络·dnn
Listennnn17 小时前
优雅的理解神经网络中的“分段线性单元”,解剖前向和反向传播
人工智能·深度学习·神经网络
誉鏐17 小时前
PyTorch复现线性模型
人工智能·pytorch·python
牙牙要健康19 小时前
【目标检测】【深度学习】【Pytorch版本】YOLOV3模型算法详解
pytorch·深度学习·目标检测
Scc_hy19 小时前
强化学习_Paper_1988_Learning to predict by the methods of temporal differences
人工智能·深度学习·算法
誉鏐20 小时前
从零开始设计Transformer模型(1/2)——剥离RNN,保留Attention
人工智能·深度学习·transformer
神经星星20 小时前
无需预对齐即可消除批次效应,东京大学团队开发深度学习框架STAIG,揭示肿瘤微环境中的详细基因信息
人工智能·深度学习·机器学习
Xiaok101820 小时前
解决 Hugging Face SentenceTransformer 下载失败的完整指南:ProxyError、SSLError与手动下载方案
开发语言·神经网络·php
程序员Linc20 小时前
写给新人的深度学习扫盲贴:向量与矩阵
人工智能·深度学习·矩阵·向量