非线性激活函数-ReLU
ReLU的参数
relu只有一个参数 inplace就是替换的意思 输出值是否替换输入值,一般情况下用flase
- inplace=True 替换
- inplace=flase 不替换
ReLU实战
从零实现relu
import torch
from torch import nn
from torch.nn import ReLU
input=torch.tensor([[1,-0.5],
[-1,3]])
input= torch.reshape(input,(-1,1,2,2))
print(input.shape)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.relu1=ReLU()
def forward(self,input):
output=self.relu1(input)
return output
# 网络搭建完毕
# 创建网络
tudui=Tudui()
output=tudui(input)
print(output)
运行结果如下:
引入图片数据,用tensorboard查看
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)
dataset=torchvision.datasets.CIFAR10("../data",train=False,download=True,
transform=torchvision.transforms.ToTensor())
dataloder=DataLoader(dataset,batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.relu1=ReLU()
self.sigmoid1=Sigmoid()
def forward(self,input):
output=self.sigmoid1(input)
return output
#网络搭建完毕
#创建网络
tudui=Tudui()
writer=SummaryWriter("../logs_relu")
step=0
for data in dataloder:
imgs,targets=data
writer.add_images("input",imgs,global_step=step)
output=tudui(imgs)
writer.add_images("output",output,step)
step+=1
writer.close()
运行结果如图: