PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层

Pooling layers可视化层

相当于1080P—>720P
PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第1张图片
PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第2张图片

Dilation空洞卷积
PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第3张图片
ceil_mode默认是false

PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第4张图片
此时池化核(3 * 3)步长为3

测试代码

# Kyrie Irving
# !/9462...
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

from src.nn_conv2d import net

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

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)
# -1自己torch计算该值 1个通道 5 * 5
input = torch.reshape(input, (-1, 1, 5, 5))
print(input.shape) # torch.Size([1, 1, 5, 5])

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

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

tudui = Tudui()
output = tudui(input)
print(output) # tensor([[[[2., 3.], [5., 1.]]]])

输出

Files already downloaded and verified
Net(
  (conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1))
)
Files already downloaded and verified
torch.Size([1, 1, 5, 5])
tensor([[[[2., 3.],
          [5., 1.]]]])

写入图片测试

# Kyrie Irving
# !/9462...
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(), download=True)

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)
# # -1自己torch计算该值 1个通道 5 * 5
# input = torch.reshape(input, (-1, 1, 5, 5))
# print(input.shape) # torch.Size([1, 1, 5, 5])

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

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

tudui = Tudui()
# output = tudui(input)
# print(output) # tensor([[[[2., 3.], [5., 1.]]]])

writer = SummaryWriter("../logs")
step = 0
for data in dataLoader:
    imgs, targets = data
    writer.add_images("input", imgs, step)
    # (-1, 3, 30, 30) batch-size不知道多少时候弄成-1 会根据后边的值进行计算 通道改为3 不然会报错
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

(pytorch) E:\CodeCodeCodeCode\AI\Pytorch-study>tensorboard --logdir="logs/"
TensorFlow installation not found - running with reduced feature set.
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.7.0 at http://localhost:6006/ (Press CTRL+C to quit)

输出

PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第5张图片

PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第6张图片

非线性激活

inplace是否在原位进行操作
PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第7张图片

# Kyrie Irving
# !/9462...
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

from src.nn_conv2d import dataLoader

input = torch.tensor([
    [1, -0.5],
    [-1, 3]
])
input = torch.reshape(input, (-1, 1, 2, 2))
print(input.shape) # torch.Size([1, 1, 2, 2])

class Tudui(nn.Module):
    def __init__(self):
        super().__init__()
        self.relu1 = ReLU() # 大于0使用input, 小于0用0
        self.sigmoid1 = Sigmoid()

    def forward(self, input):
        output = self.relu1(input)
        return output
tudui = Tudui()
output = tudui(input)
print(output) # tensor([[[[1., 0.], [0., 3.]]]])

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

dataLoader = DataLoader(dataset, batch_size=64)

writer = SummaryWriter("../logs")
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 += 1

writer.close()

输出

Files already downloaded and verified
Net(
  (conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1))
)
torch.Size([1, 1, 2, 2])
tensor([[[[1., 0.],
          [0., 3.]]]])
Files already downloaded and verified

PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第8张图片

线性模型

主要改变是在搭建神经网络里边
PyTorch深度学习入门笔记(七)Pooling layers可视化层 非线性激活 线性层_第9张图片

全部代码

# Kyrie Irving
# !/9462...
import torch
import torchvision
from torch import nn
from torch.nn import ReLU, Sigmoid, Linear
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

# 每张图片都转为ToTensor类型
dataset_transform = torchvision.transforms.Compose([
    torchvision.transforms.ToTensor()
])
test_set = torchvision.datasets.CIFAR10(root="../dataset", train=False, transform=dataset_transform, download=True)
test_loader = DataLoader(dataset=test_set, batch_size=64)


class Tudui(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear1 = Linear(196608, 10)

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


for data in test_loader:
    imgs, targets = data
    # output = torch.reshape(imgs, (1, 1, 1, -1))
    output = torch.flatten(imgs)
    print(output.shape)
    output = tudui(output)
    print(output.shape)

你可能感兴趣的:(pytorch,深度学习,pytorch,人工智能)