简体   繁体   English

如何将 map RNN output 转换为 class 大小的张量?

[英]How to map RNN output to tensor of class size?

I'm building a binary text classifier using GLOVE embeddings and RNN.我正在使用 GLOVE 嵌入和 RNN 构建二进制文本分类器。 The output of nn.RNN is torch.Size([1, 12, 150]). nn.RNN 的 output 是 torch.Size([1, 12, 150])。 I need to map this to a dimension of size 2 so that i can calculate loss against the actual class.我需要将 map 调整为尺寸 2,以便我可以根据实际 class 计算损失。 Im feeding tensor of size 'words' and 100 for glove embeddings of size 100d.我为大小为 100d 的手套嵌入提供大小为“单词”和 100 的张量。 Eg a 12 word sentence would be of size [1,12,100]例如,一个 12 字的句子大小为 [1,12,100]

How is this done?这是怎么做到的?

def forward(self, input, hidden):

    embeds = self.embedding(input) # glove embedding
    embeds = embeds.unsqueeze(0)
    embeds = embeds.float()
    
    output, hidden = self.rnn(embeds, hidden)
    # output is size [1, 12, 150]

    return output, hidden

If I understand correctly, you're attempting to classify a series of words (12 in your example) with a binary class.如果我理解正确,您正在尝试使用二进制 class 对一系列单词(在您的示例中为 12 个)进行分类。 There are many ways to do this.有很多方法可以做到这一点。 Essentially, you just need to reduce the size of your output with differentiable operations so that meaningful parameters are learned during training.本质上,您只需要通过可微分运算减小 output 的大小,以便在训练期间学习有意义的参数。 In another sense, the RNN outputs a set of features (created from the originally input features) which are ideally more useful in classification of the text, so you essentially need to build a binary classifier which uses these features of size [number of words x 100].在另一种意义上,RNN 输出一组特征(从原始输入特征创建),这些特征在文本分类中更有用,因此您基本上需要构建一个二进制分类器,使用这些大小为 [词数 x 100]。

For instance, you could simply average or sum all the features:例如,您可以简单地对所有特征进行平均或求和:

# done in two steps to avoid averaging across batch examples
pred = torch.average(output,dim = 2)
pred = torch.average(pred,dim = 1) 

You could use a fully-connected neural network layer or layers:您可以使用一个或多个完全连接的神经网络层:

# in __init__()
...
self.fc = nn.Linear(100,1)


# in forward()
...
output = torch.average(output,dim = 1)
output = self.fc(output)
output = torch.nn.functional.sigmoid(output)

return output,hidden

Essentially, there's no wrong way to build the final classification module, as whatever structure you define, training the RNN will produce features that are discriminative with respect to the classification module.本质上,构建最终分类模块没有错误的方法,因为无论您定义什么结构,训练 RNN 都会产生与分类模块相关的特征。 However, you'll likely find that some things work better than others so it's probably best to try a few things and see what works.但是,您可能会发现有些事情比其他事情更有效,因此最好尝试一些事情,看看什么有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM