PyTorch深度学习——非线性激活

官方文档传送:ReLU
PyTorch深度学习——非线性激活_第1张图片对一个tensor经过ReLU优化:

import torch
from torch import nn
from torch.nn import ReLU

input = torch.tensor([[1,-1],
                      [-4,5]])

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

print(input.shape)


class Siri(nn.Module):
    def __init__(self):
        super(Siri, self).__init__()
        self.relu1 = ReLU()

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

siri = Siri()
output = siri(input)
print(output)

运行结果:

torch.Size([1, 1, 2, 2])
tensor([[[[1, 0],
          [0, 5]]]])

下面是sigmoid函数对图片的处理:

import torch
import torchvision
from torch import nn
from torch.nn import Sigmoid
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

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

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

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


siri = Siri()

writer = SummaryWriter("./logs_relu")
step = 0
for data in dataloader:
    imgs,targets = data
    writer.add_images("input",imgs,step)
    output = siri(imgs)
    writer.add_images("output",output,step)
    step += 1

writer.close()

处理效果:
PyTorch深度学习——非线性激活_第2张图片

你可能感兴趣的:(PyTorch)