哔哩大学的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)