ReLU
python
import torch
from torch import nn
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]) .shape = .size()
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.relu1 = nn.ReLU()
def forward(self, input):
output = self.relu1(input)
return output
tudui = Tudui()
output = tudui(input)
print(output) # tensor([[[[1., 0.],[0., 3.]]]])
Result
Sigmoid
python
import torch
import torchvision
from torch import nn
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) # torch.Size([1, 1, 2, 2]) .shape = .size()'''
dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.relu1 = nn.ReLU()
self.sigmoid1 = nn.Sigmoid()
def forward(self, input):
output = self.sigmoid1(input)
return output
tudui = Tudui()
writer = SummaryWriter('./logs_relu')
step = 0
for data in dataloader:
imgs, targets = data
writer.add_images('input', imgs, step)
output = tudui(imgs)
writer.add_images('output', output, step)
step += 1
writer.close()
目的
:引入非线性特征,非线性越多,才能训练出符合各种曲线,符合各种特征的模型,泛化能力好