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

问题描述 投票:1回答:1

我正在构建一个序列到标签的分类器,其中输入数据是文本序列,输出标签是二进制。该模型非常简单,具有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;

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

python pytorch lstm tensor torch
1个回答
0
投票

模型的输出为torch.Size([64, 60, 1])形状,即批量大小为64,并且(60,1)符合预期的[n, 1]

假设您正在使用nn.CrossEntropy(input, target),则期望输入为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)

© www.soinside.com 2019 - 2024. All rights reserved.