哔哩大学的PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】
的P22讲讲述了神经网络搭建小实战和sequential的使用。
一开始的代码如下:但是他不能运行,为什么不能?不知道!!!
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.conv1 = Conv2d(3, 32, 5, padding=2) # 前三个参数见图片,图中是32*32变成32*32,padding的算法为图片
self.maxpool1 = MaxPool2d(2)
self.conv2 = Conv2d(3, 32, 5, padding=2)
self.maxpool2 = MaxPool2d(2)
self.conv3 = Conv2d(32, 64, 5, padding=2)
self.maxpool3 = MaxPool2d(2)
self.flatten = Flatten()
self.linear1 = Linear(1024, 64)
self.linear2 = Linear(64, 10)
def forward(self, x):
x = self.conv1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.maxpool2(x)
x = self.conv3(x)
x = self.maxpool3(x)
x = self.flatten(x)
self.linear1(x)
self.linear2(x)
return x
tudui = Tudui()
print(tudui)
input = torch.ones((64, 3, 32, 32))# 创建一个输入,来检查上边网络的正确性
output = tudui(input)
print(output.shape)
报错:
F:\Users\86133\anaconda3\envs\pytorch\python.exe C:/Users/86133/Desktop/pythonProject/shiyan.py
Tudui(
(conv1): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv2): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv3): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(maxpool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(flatten): Flatten(start_dim=1, end_dim=-1)
(linear1): Linear(in_features=1024, out_features=64, bias=True)
(linear2): Linear(in_features=64, out_features=10, bias=True)
)
Traceback (most recent call last):
File "C:/Users/86133/Desktop/pythonProject/shiyan.py", line 34, in <module>
output = tudui(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 "C:/Users/86133/Desktop/pythonProject/shiyan.py", line 22, in forward
x = self.conv2(x)
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\conv.py", line 443, in forward
return self._conv_forward(input, self.weight, self.bias)
File "F:\Users\86133\anaconda3\envs\pytorch\lib\site-packages\torch\nn\modules\conv.py", line 440, in _conv_forward
self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size [32, 3, 5, 5], expected input[64, 32, 16, 16] to have 3 channels, but got 32 channels instead
Process finished with exit code 1
好像看的懂,又不知道咋改,而且和视频中的一摸一样啊!!@!
直到后边有了另一种一样的简单方法,然后可以运行了
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
# self.conv1 = Conv2d(3, 32, 5, padding=2) # 前三个参数见图片,图中是32*32变成32*32,padding的算法为图片
# self.maxpool1 = MaxPool2d(2)
# self.conv2 = Conv2d(3, 32, 5, padding=2)
# self.maxpool2 = MaxPool2d(2)
# self.conv3 = Conv2d(32, 64, 5, padding=2)
# self.maxpool3 = MaxPool2d(2)
# self.flatten = Flatten()
# self.linear1 = Linear(1024, 64)
# self.linear2 = Linear(64, 10)
self.model1 = Sequential(
Conv2d(3, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
# x = self.conv1(x)
# x = self.maxpool1(x)
# x = self.conv2(x)
# x = self.maxpool2(x)
# x = self.conv3(x)
# x = self.maxpool3(x)
# x = self.flatten(x)
# self.linear1(x)
# self.linear2(x)
x = self.model1(x)
return x
tudui = Tudui()
print(tudui)
input = torch.ones((64, 3, 32, 32))# 创建一个输入,来检查上边网络的正确性
output = tudui(input)
print(output.shape)
# 用tensorboard输出
writer = SummaryWriter("logs_seq")
writer.add_graph(tudui, input)
writer.close()
经过我老乡的指点!第一次报错是因为
self.conv2 = Conv2d(3, 32, 5, padding=2)写错了
应该是:
self.conv2 = Conv2d(32, 32, 5, padding=2)
完蛋玩意
还有
self.linear1(x)
self.linear2(x)
改成
x = self.linear1(x)
x = self.linear2(x)