首页 > 解决方案 > PyTorch:张量的大小必须在 2 个输入神经网络上匹配

问题描述

我正在尝试从这篇文章中重新创建一个 2 输入神经网络:https ://towardsdatascience.com/moving-from-keras-to-pytorch-f0d4fff4ce79

我复制了帖子中描述的网络并对其进行了调整,以使其适合我的数据。第一个输入来自 GloVe Word 嵌入,而另一个输入是关于文本数据的数字特征。

class Net(nn.Module):
    def __init__(self,hidden_size,lin_size, embedding_matrix=embedding_weights):
        super(Alex_NeuralNet_Meta, self).__init__()

        # Initialize some parameters for your model
        self.hidden_size = hidden_size
        drp = 0.1

        # Layer 1: Embeddings.
        self.embedding = nn.Embedding(size_of_vocabulary, pretrained_embedding_dim)
        self.embedding.weight = nn.Parameter(torch.tensor(embedding_matrix, dtype=torch.float32))
        self.embedding.weight.requires_grad = False

        # Layer 2: Dropout1D(0.1)
        self.embedding_dropout = nn.Dropout2d(0.1)

        # Layer 3: Bidirectional CuDNNLSTM
        self.lstm = nn.LSTM(pretrained_embedding_dim, hidden_size, bidirectional=True, batch_first=True)

        # Layer 4: Bidirectional CuDNNGRU
        self.gru = nn.GRU(hidden_size*2, hidden_size, bidirectional=True, batch_first=True)

        # Layer 7: A dense layer
        self.linear = nn.Linear(hidden_size*6 + X2_train.shape[1], lin_size)
        self.relu = nn.ReLU()

        # Layer 8: A dropout layer 
        self.dropout = nn.Dropout(drp)

        # Layer 9: Output dense layer with one output for our Binary Classification problem.
        self.out = nn.Linear(lin_size, 1)

    def forward(self, x):
        
        '''
        here x[0] represents the first element of the input that is going to be passed. 
        We are going to pass a tuple where first one contains the sequences(x[0])
        and the second one is a additional feature vector(x[1])
        '''
        h_embedding = self.embedding(x[0].long())
        h_embedding = torch.squeeze(self.embedding_dropout(torch.unsqueeze(h_embedding, 0)))
        #print("emb", h_embedding.size())
        h_lstm, _ = self.lstm(h_embedding)
#         print("lst",h_lstm.size())
        h_gru, hh_gru = self.gru(h_lstm)
        hh_gru = hh_gru.view(-1, 2*self.hidden_size )
        print("gru", h_gru.size())
        print("h_gru", hh_gru.size())

        # Layer 5: is defined dynamically as an operation on tensors.
        avg_pool = torch.mean(h_gru, 1)
        max_pool, _ = torch.max(h_gru, 1)
        print("avg_pool", avg_pool.size())
        print("max_pool", max_pool.size())

        # the extra features you want to give to the model
        f = torch.tensor(x[1], dtype=torch.float).cuda()
        print("f", f.size())

        # Layer 6: A concatenation of the last state, maximum pool, average pool and 
        # additional features
        conc = torch.cat(( hh_gru, avg_pool, max_pool, f), 1)
        #print("conc", conc.size())

        # passing conc through linear and relu ops
        conc = self.relu(self.linear(conc))
        conc = self.dropout(conc)
        out = self.out(conc)
        # return the final output
        return out

在运行时,我在连接行上收到错误:

RuntimeError: Sizes of tensors must match except in dimension 0. Got 33164 and 20 (The offending index is 0)

从输出的维度,我可以看到问题出在哪里,但我不确定如何解决它


网络的数据输入为:

torch.Size([20, 150])
torch.Size([33164, 40])

每层输出的大小为:

gru torch.Size([20, 150, 80])
h_gru torch.Size([20, 80])
avg_pool torch.Size([20, 80])
max_pool torch.Size([20, 80])
f torch.Size([33164, 40])

对于上面的示例,batch size 为 20,hidden_​​size 为 40,数值数据特征的行数为 33164,其特征大小为 40。

感谢您提前提供任何帮助

标签: pythondeep-learningpytorch

解决方案


推荐阅读