一、DataLoader 的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import torchvision

from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())

test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=True)
# 每次取的个数 取完后是否打乱 最后如果因为数量无法分配是否舍去

writer = SummaryWriter("dataloader")
for epoch in range(2):
step = 0
for data in test_loader:
imgs, target = data
writer.add_images(f"Epoch:{epoch}", imgs, step)
step = step + 1

writer.close()

!!二、python补充: call函数

__call__ 可以将类名变为可执行函数

比如在下面的代码中,nn.Module 中包含了__call__函数,使得test(x)直接调用forward函数并得到返回值

call函数举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Test():
# def __init__(self): 没内容时可省略不写

def forward(self, input):
output = input + 1
return output

def __call__(self, input):
return self.forward(input) # 在类的方法内部调用另一个方法时,需使用 self 关键字来指向它


test = Test()
x = 1
output = test(x)
print(output)

三、神经网络的基本骨架 nn.Module 的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import torch
from torch import nn


class Test(nn.Module): # 表示Test继承于Module,它为所有神经网络提供基本的骨架
def __init__(self):
super().__init__()

def forward(self, input):
output = input + 1
return output


test = Test()
x = torch.tensor(1.0)
output = test(x)
print(output)

*四、卷积操作 CONV2D (convolution)(nn.functional.conv2d 为具体方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import torch
import torch.nn.functional as F

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]])
print(input.shape)

input = torch.reshape(input, (1, 1, 5, 5))
# conv2d中要求输入和卷积层的尺寸中有4个属性,而tensor创建出来的只有2个,因此需要reshape增加他们的属性。四个属性分别为:batch_size(每次喂给神经网络多少个数据)填-1的话可以让系统根据后面三个数自动计算,通道数(1为灰度图像,rgb通道数为3),高度,宽度。
kernel = torch.reshape(kernel, (1, 1, 3, 3))

print(input.shape)

output = F.conv2d(input, kernel, stride=1)
print(output)

Tips: num_workers >0 时可能会出现broken pipe error ,此时把它设置为0试试

举例





padding 举例

stride举例

动图:

https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md

五、卷积操作 CONV2D (nn.convv2d 为封装后的函数)(实际使用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader

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


class Test(nn.Module):
def __init__(self):
super(Test, 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


test = Test()

for data in DataLoader:
imgs, targets = data
output = test(imgs)
print(imgs.shape)
print(output.shape)

in_channels=3: imput的通道为3

out_channels=6:output的通道为6 =卷积核的个数

kernel_size=3:卷积核的高和宽为3 深度由系统自动计算

stride=1:步径为1

padding=0:边缘不需要加行/列

注:卷积核中的数值应是自动生成的

训练模型就是训练卷积核中自动生成的数值

*找到的资源

对知名模型的代码进行逐行解读的网站 https://nn.labml.ai/

图神经网络的实现基本都有 Deep Graph Library https://www.dgl.ai