我是初学者使用pytorch。我想将2d二进制数组(17 * 20)分类为8类,我使用交叉熵作为损失函数。我有512个批大小。输入大小为512批(17 * 20),最终输出大小为512批(8)。我应用了下面的模型,我希望得到的最终输出只有长度为8的列表。比如[512,8],但我得到了模糊的[512,680,8](我在代码之后从模型中打印了尺寸)。我怎样才能从网络中得到[512,8]作为最终输出呢?
def __init__(self, M=1):
super(PPS, self).__init__()
#input layer
self.layer1 = nn.Sequential(
nn.Conv2d(17, 680, kernel_size=1, stride=1, padding=0),
nn.ReLU())
self.drop1 = nn.Sequential(nn.Dropout())
self.batch1 = nn.BatchNorm2d(680)
self.lstm1=nn.Sequential(nn.LSTM(
input_size=20,
hidden_size=16,
num_layers=1,
bidirectional=True,
batch_first= True))
self.gru = nn.Sequential(nn.GRU(
input_size=16*2,
hidden_size=16,
num_layers=2,
bidirectional=True,
batch_first=True))
self.fc1 = nn.Linear(16*2,8)
def forward(self, x):
out = self.layer1(x)
out = self.drop1(out)
out = self.batch1(out)
out = out.squeeze()
out,_ = self.lstm1(out)
out,_ = self.gru(out)
out = self.fc1(out)
return out
cov2d torch.Size([512, 680, 20, 1])
drop torch.Size([512, 680, 20, 1])
batch torch.Size([512, 680, 20])
lstm1 torch.Size([512, 680, 32])
lstm2 torch.Size([512, 680, 32])
linear1 torch.Size([512, 680, 8])
如果你想要输出为(512, 8)
,那么你必须将最后一个线性层更改为如下内容:
def __init__(self, M=1):
...
self.gru = nn.Sequential(nn.GRU(
input_size=16*2,
hidden_size=16,
num_layers=2,
bidirectional=True,
batch_first=True))
self.fc1 = nn.Linear(680 * 16*2, 8)
def forward (self, x):
...
out, _ = self.gru(out)
out = self.fc1(out.reshape(-1, 680 * 16*2))
return out
目标是将680 * 16 * 2
的特征数量减少到8
。你可以(也许应该)添加更多的最终线性层来为你做这个还原。