在PyTorch中,最后一层的张量输出形状错误

时间:2020-03-05 03:01:00

标签: python pytorch lstm tensor torch

我正在构建一个序列到标签的分类器,其中输入数据是文本序列,输出标签是二进制。该模型非常简单,具有GRU隐藏层和Word Embeddings输入层。我希望输入[n, 60]输出一个[n, 1]标签,但是Torch模型返回一个[n, 60]输出。

我的模型,具有最少的层数:

class Model(nn.Module):
    def __init__(self, weights_matrix, hidden_size, num_layers):
        super(Model, self).__init__()
        self.embedding, num_embeddings, embedding_dim = create_emb_layer(weights_matrix, True)
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.gru = nn.GRU(embedding_dim, hidden_size, num_layers, batch_first=True)
        self.out = nn.Linear(hidden_size, 1)
    def forward(self, inp, hidden):
        emb = self.embedding(inp);
        out, hidden = self.gru(emb, hidden)
        out = self.out(out);
        return out, hidden;

    def init_hidden(self, batch_size):
        return torch.zeros(self.num_layers, batch_size, self.hidden_size).to(device);

模型层:

Model(
  (embedding): Embedding(184901, 100)
  (gru): GRU(100, 60, num_layers=3, batch_first=True)
  (out): Linear(in_features=60, out_features=1, bias=True)
)

我的数据的输入形状为Xtorch.Size([64, 60])Ytorch.Size([64, 1]),用于单批大小为64的

当我在模型中运行X张量时,它应该输出单个标签,但是,分类器的输出为torch.Size([64, 60, 1])。要运行模型,请执行以下操作:

for epoch in range(1):
    running_loss = 0.0;

    batch_size = 64;
    hidden = model.init_hidden(batch_size)
    for ite, data in enumerate(train_loader, 0):
        x, y = data[:,:-1], data[:,-1].reshape(-1,1)

        optimizer.zero_grad();

        outputs, hidden = model(x, hidden);

        hidden = Variable(hidden.data).to(device);
        loss = criterion(outputs, y);

        loss.backward();
        optimizer.step();

        running_loss = running_loss + loss.item();
        if ite % 2000 == 1999:
            print('[%d %5d] loss: %.3f'%(epoch+1, ite+1, running_loss / 2000))
        running_loss = 0.0;

当我打印shape的{​​{1}}时,它是outputs而不是64x60x1。我还没有得到的是64x1函数如何在输出和标签的形状不一致时计算损失。使用Tensorflow,这总是会引发错误,但是使用Torch则不会。

1 个答案:

答案 0 :(得分:1)

模型的输出为torch.Size([64, 60, 1])形状,即批处理大小为64,而(60,1)对应于[n, 1]

假设您使用的是nn.CrossEntropy(input, target),则期望输入为(N,C),目标为(N),其中C是类数。

您的输出是一致的,因此会评估损失。

例如,

outputs = torch.randn(3, 2, 1)
target = torch.empty(3, 1, dtype=torch.long).random_(2)

criterion = nn.CrossEntropyLoss(reduction='mean')
print(outputs)
print(target)
loss = criterion(outputs, target)
print(loss)

# outputs
tensor([[[ 0.5187],
         [ 1.0320]],

        [[ 0.2169],
         [ 2.4480]],

        [[-0.4895],
         [-0.6096]]])
tensor([[0],
        [1],
        [0]])
tensor(0.5731)

了解更多here