Pytorch關於神經網絡的包都在torch.nn裏
forward是前向傳播(backward是反向傳播)
1 卷積基礎
# 學習卷積層基礎 —— 卷積
input = torch.tensor([[1, 2, 0, 3, 1],
[0, 1, 2, 3, 1],
[1, 2, 1, 0, 0],
[5, 2, 3, 1, 1],
[2, 1, 0, 1, 1]])
kernel = torch.tensor([[1, 2, 1],
[0, 1, 0],
[2, 1, 0]])
# 由於TORCH.NN.FUNCTIONAL.CONV2D 要求參數input和weight是四維的,所以這裏做個升維
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
print(input.shape)
print(kernel.shape)
# 開始卷積
# 指定stride卷積
output = F.conv2d(input, kernel, stride=1)
print(output)
# 指定padding卷積
output = F.conv2d(input, kernel, stride=1, padding=1)
print(output)
2 卷積層
2.1 卷積層使用案例
import torch
import torchvision
from torch.utils.data import DataLoader
from torch import nn
from torch.nn import Conv2d
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10(root="D:/Learn/dataSet/Pytorch", train=False, transform=torchvision.transforms.ToTensor(), download=False)
dataloader = DataLoader(dataset, batch_size=64)
# 創建卷積層
class Test_NN(nn.Module):
def __init__(self):
super(Test_NN, self).__init__()
self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
# 實例化
testNN = Test_NN()
print(testNN)
writer = SummaryWriter("logs")
step = 0
# 看結果
for data in dataloader:
imgs, targets = data
output = testNN(imgs)
# 輸入圖像
print(imgs.shape) # torch.Size([64, 3, 32, 32])
writer.add_images("input", imgs, step)
# 輸出圖像
print(output.shape) # torch.Size([64, 6, 30, 30])
# 這裏輸出爲6通道,tensorboard不知道怎麼顯示,需要reshape尺寸變換再顯示
# torch.Size([64, 6, 30, 30]) -> torch.Size([xxx, 3, 30, 30])
output = torch.reshape(output, (-1, 3, 30, 30)) # 第一個參數不知道寫多少,就寫-1
writer.add_images("output", output, step)
step += 1
writer.close()
得到結果
根據下面公司進行計算輸入輸出shape