【发布时间】:2021-05-17 23:18:31
【问题描述】:
我是使用 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])
【问题讨论】:
标签: computer-vision pytorch conv-neural-network flatten cross-entropy