我有一个图像输入340px * 340px,我想将它分类为2个类 . 我想创建卷积神经网络(PyTorch框架) . 我有输入和输出图层的问题 .
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 3 channels (RGB), kernel=5, but i don't understand why 6.
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
#why 16?
self.conv2 = nn.Conv2d(6, 16, 5)
#why 107584 = 328*328
self.fc1 = nn.Linear(107584, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 2)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
# i dont understand this line
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
网络是否正确?
1 回答
Conv2d的第二个参数是
out_channels
. 在卷积层中,您可以任意定义多个输出通道 . 所以它被设置为6,因为有人将其设置为6 .与上面相同 .
和
Tensor.view()返回一个新的张量,其数据与自张量相同,但大小不同 .
x = x.view(x.size(0), -1)
:-1表示"infer from other dimensions"因此,您强制Tensor为[1,15 * 164 * 164] => [1,403440] .403440也是
self.fc1 = nn.Linear(107584, 120)
的正确值,而不是107584 .