神经网络非线性激活举例————PyTorch

哔哩大学的PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】
的P20讲讲述了神经网络的非线性激活函数的使用。

代码如下:

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("../dataset", train=False, download=True, transform=torchvision.transforms.ToTensor())

dataloader = 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 dataloader:
    imgs, targets = data
    writer.add_images("input", imgs, global_step=step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step = step + 1

writer.close()

又他么报错:

F:\Users\86133\anaconda3\envs\pytorch\python.exe C:/Users/86133/Desktop/pythonProject/nn_relu.py
torch.Size([1, 1, 2, 2])
Files already downloaded and verified
Traceback (most recent call last):
  File "C:/Users/86133/Desktop/pythonProject/nn_relu.py", line 36, in <module>
    output = tudui(imgs)
TypeError: __init__() takes 1 positional argument but 2 were given

Process finished with exit code 1

应该运行结果:
神经网络非线性激活举例————PyTorch_第1张图片

————————————————————————————————

更新
换了个dataset地址又能运行了,离谱!!!

dataset = torchvision.datasets.CIFAR10("dataset", train=False, download=True, transform=torchvision.transforms.ToTensor())

你可能感兴趣的:(pytorch,pytorch,神经网络,深度学习)