PyTorch快速入门教程【小土堆】之非线性激活
视频地址神经网络-非线性激活_哔哩哔哩_bilibili、
非线性变换主要目的是向网络当中引入非线性特征,非线性特征越多才能训练出符合各种曲线,符合更多特征的模型,如果都是直接表现的话泛化能力不好
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))
dataset = torchvision.datasets.CIFAR10("CIFAR10", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.relu1 = ReLU()# 若inplace为true 会直接对input进行操作,为false会在生成一个output进行变换,原来input不变,默认为false
self.sigmoid1 = Sigmoid()
def forward(self, input):
output = self.sigmoid1(input)
return output
tudui = Tudui()
# output = tudui(input)
# print(output)
# # tensor([[[[1., 0.],
# # [0., 3.]]]]) 负数置为0
writer = SummaryWriter("logs_relu")
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()