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

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

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10("data", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)
# 数据集操作
dataloader = DataLoader(dataset, batch_size=64)

# 从Moudule中继承,建立叫土堆的神经网络
class Tudui(nn.Module):
    # 先初始化
    def __init__(self):
        super(Tudui, self).__init__()
        # 定义一个卷积层,self后的变量,在其他函数中也可以使用。
        # 彩色图像输出为三层in_channels=3,输出为六层out_channels=6,kernel_size=3为3*3的卷积。
        self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
        # 现在网络中有了一个卷积层
    def forword(self, x):
        # 定义forword函数
        x = self.conv1(x)
        # 先让x进行一次上边的conv1卷积,结果仍为x
        return x
        # 输出x

# 初始化这个网络
tudui = Tudui()

writer = SummaryWriter("logs")

step = 0
# 想看见dataloader中的每一个数据
for data in dataloader:
    imgs, targets = data
    output = tudui(imgs)
    # output是把dataloader中的数据放到神经网络中,进行forword的卷积操作,返回为叫output
    print(imgs.shape)
    print(output.shape)
    writer.add_images("input", imgs, step)
    torch.reshape(output, (-1, 3, 30, 30))
    writer.add_images("output", output, step)

    step = step + 1

报错报错:

torch.Size([10])
torch.Size([64, 3, 32, 32])
torch.Size([196608])
torch.Size([10])
torch.Size([64, 3, 32, 32])
torch.Size([196608])
torch.Size([10])
torch.Size([16, 3, 32, 32])
torch.Size([49152])
Traceback (most recent call last):
  File "C:/Users/86133/Desktop/pythonProject/nn_linear.py", line 30, in <module>
    output = tudui(output)
  File "F:\Users\86133\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:/Users/86133/Desktop/pythonProject/nn_linear.py", line 19, in forward
    output = self.linear1(input)
  File "F:\Users\86133\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "F:\Users\86133\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\linear.py", line 96, in forward
    return F.linear(input, self.weight, self.bias)
  File "F:\Users\86133\anaconda3\envs\pytorch\lib\site-packages\torch\nn\functional.py", line 1847, in linear
    return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x49152 and 196608x10)

Process finished with exit code 1

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

更新!!!!

dataloader = DataLoader(dataset, batch_size=64, drop_last=True)

batch里最后只有16个图片,总之是后边加了个drop_last=True就不报错了!
dataloader = DataLoader(dataset, batch_size=64)
改为:
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值