环境配置请看这里
1、ReLU()
1、ReLU相关简介
以ReLU为例ReLU官方文档
2、代码
import torch
from torch import nn
from torch.nn import ReLU
input = torch.tensor([[1,-0.5],
[-1,3]], dtype=torch.float32)
input = torch.reshape(input,(-1, 1, 2, 2))
class Tian(nn.Module):
def __init__(self):
super(Tian, self).__init__()
self.relu_1 = ReLU()
def forward(self,input):
output = self.relu_1(input)
return output
ren = Tian()
output = ren(input)
print("经过ReLU后=", output)
3、运行结果
2、Sigmoid()
1、Sigmoid相关简介
2、代码
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
dataset = torchvision.datasets.CIFAR10("E:/PycharmProjects/Pytoch_learning/dataset/CIFAR10",
train=False, transform=torchvision.transforms.ToTensor(), download=False)
dataloader = DataLoader(dataset, batch_size=64)
input = torch.tensor([[1,-0.5],
[-1,3]], dtype=torch.float32)
input = torch.reshape(input,(-1, 1, 2, 2))
class Tian(nn.Module):
def __init__(self):
super(Tian, self).__init__()
self.sigmoid_1 = Sigmoid()
def forward(self,input):
output = self.sigmoid_1(input)
return output
ren = Tian()
writer = SummaryWriter(log_dir="E:/PycharmProjects/runs/flower_experiment")
step = 0
for data in dataloader:
imgs, targets =data
output = ren(imgs)
print(output.shape)
writer.add_images("day10_input", imgs, global_step=step)
writer.add_images("day10_output", output, global_step=step)
step += 1
writer.close()
3、运行结果
4、tensorboard可视化
tensorboard详细教程tensorboard新手友好 在terminal中输入
tensorboard --logdir="E:/PycharmProjects/runs/flower_experiment"
回车后点击蓝色链接即可显示 网页显示结果
|