import torch
import torch.nn as nn
import torch.nn.functional as F
#定义一个网络
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
# print("----卷积核----")
# print(self.conv1)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
if __name__ == "__main__":
net = Net()
print(net)
学习torch框架中的卷积神经网络
一,nn.Conv2d
二维卷积可以处理二维数据
1,nn.Conv2d(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True))
参数:
self:
in_channels:输入数据的通道数,例RGB图片通道数为3;
out_channel:输出数据的通道数,这个根据模型调整;
kernel_size:卷积核大小,可以是int,或tuple;kennel_size=5,意味着卷积大小(5,5).
stride: 步长,默认为1,与kennel_size类似,stride=2,意味着步长上下左右扫描皆为2, tride=(2,3),左右扫描步长为2,上下为3;
padding: 零填充
dilation:
groups:
bias:
2例子 输入数据X[10,16,30,32],其分别代表:10组数据,通道数为16,高度为30,宽为32
import torch
import torch.nn as nn
import torch.nn.functional as F
x = torch.randn(10, 16, 30, 32, 34)
# batch, channel , height , width
print(x.shape)
class Net_1D(nn.Module):
def __init__(self):
super(Net_1D, self).__init__()
self.layers = nn.Sequential(
nn.Conv1d(in_channels=16, out_channels=16, kernel_size=(3, 2, 2), stride=(2, 2, 1), padding=[2,2,2]),
nn.ReLU()
)
def forward(self, x):
output = self.layers(x)
log_probs = F.log_softmax(output, dim=1)
return log_probs
n = Net_1D() # in_channel,out_channel,kennel,
print(n)
y = n(x)
print(y.shape)
结果:
torch.Size([10, 16, 30, 32, 34])
Net_1D(
(layers): Sequential(
(0): Conv1d(16, 16, kernel_size=(3, 2, 2), stride=(2, 2, 1), padding=[2, 2, 2])
(1): ReLU()
)
)
torch.Size([10, 16, 16, 18, 37])
3.卷积计算
d = (d - kennel_size + 2 * padding) / stride + 1
x = ([10,16,30,32,34]),其中第一维度:30,第一维度,第二维度:32,第三维度:34,对于卷积核长分别是;对于步长分别是第一维度:2,第二维度:,2,第三维度:1;对于padding分别是:第一维度:2,第二维度:,2,第三维度:2;
d1 = (30 - 3 + 22)/ 2 +1 = 31/2 +1 = 15+1 =16
d2 = (32 - 2 + 22)/ 2 +1 = 34/2 +1 = 17+1 =18
d3 = (34 - 2 + 2*2)/ 1 +1 = 36/1 +1 = 36+1 =37
batch = 10, out_channel = 16
故:y = [10, 16, 16, 18, 37]
二,nn.Linear()
1,PyTorch的nn.Linear()是用于设置网络中的全连接层的,需要注意的是全连接层的输入与输出都是二维张量,一般形状为[batch_size, size],不同于卷积层要求输入输出是四维张量。其用法与形参说明如下:
torch.nn.Linear(in_features,out_features,bias=True)
in_features:指的是输入的二维张量的大小,即输入的[batch_size, size]中的size。
out_features:指的是输出的二维张量的大小,即输出的二维张量的形状为[batch_size,output_size],当然,它也代表了该全连接层的神经元个数。从输入输出的张量的shape角度来理解,相当于一个输入为[batch_size, in_features]的张量变换成了[batch_size, out_features]的输出张量。
2,用法示例:
import torch as t
from torch import nn
# in_features由输入张量的形状决定,out_features则决定了输出张量的形状
connected_layer = nn.Linear(in_features = 64*64*3, out_features = 1)
# 假定输入的图像形状为[64,64,3]
input = t.randn(1,64,64,3)
# 将四维张量转换为二维张量之后,才能作为全连接层的输入
input = input.view(1,64*64*3)
print(input.shape)
output = connected_layer(input) # 调用全连接层
print(output.shape)
这段代码运行结果为:
input shape is %s torch.Size([1, 12288])
output shape is %s torch.Size([1, 1])