[英]Multi-class for sentence classification with pytorch (Using nn.LSTM)
I have this network, that I took from this tutorial, and I want to have sentences as input (Which is already done) and just a one line tensor as a result.我有这个网络,我从本教程中获取,并且我希望将句子作为输入(已经完成),结果只有一个单行张量。
From the tutorial, this sentence “John's dog likes food”, gets a 1 column tensor returned:从教程中,这句话“John's dog likes food”,得到一个 1 列的张量返回:
tensor([[-3.0462, -4.0106, -0.6096],
[-4.8205, -0.0286, -3.9045],
[-3.7876, -4.1355, -0.0394],
[-0.0185, -4.7874, -4.6013]])
...and class list: ...和班级名单:
tag_list[ “name”, “verb”, “noun”]
Each line has the probability of a tag being associated with the word.每行都有一个标签与该词相关联的概率。 (The first word has [-3.0462, -4.0106, -0.6096 ] vector where the last element corresponds to the maximum scoring tag, "noun") (第一个词有[-3.0462, -4.0106, -0.6096 ]向量,其中最后一个元素对应于最大得分标签,“名词”)
The tutorial's dataset looks like this:本教程的数据集如下所示:
training_data = [
("The dog ate the apple".split(), ["DET", "NN", "V", "DET", "NN"]),
("Everybody read that book".split(), ["NN", "V", "DET", "NN"])
]
And I want mine to be of this format:我希望我的是这种格式:
training_data = [
("Hello world".split(), ["ONE"]),
("I am dog".split(), ["TWO"]),
("It's Britney glitch".split(), ["THREE"])
]
The parameters are defined as:参数定义为:
class LSTMTagger(nn.Module):
def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size):
super(LSTMTagger, self).__init__()
self.hidden_dim = hidden_dim
self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim)
self.hidden2tag = nn.Linear(hidden_dim, tagset_size)
def forward(self, sentence):
embeds = self.word_embeddings(sentence)
lstm_out, _ = self.lstm(embeds.view(len(sentence), 1, -1))
tag_space = self.hidden2tag(lstm_out.view(len(sentence), -1))
tag_scores = F.log_softmax(tag_space, dim=1)
return tag_scores
As of now, the sizes from input and output are not matching and i get: ValueError: Expected input batch_size (2) to match target batch_size (1).截至目前,输入和输出的大小不匹配,我得到:ValueError: Expected input batch_size (2) to match target batch_size (1)。
The criterion function doesn't accept the input due to size missmatch it seems:由于大小不匹配,标准函数似乎不接受输入:
loss = criterion(tag_scores, targets)
I've read the last layer could be defined as nn.Linear in order to squash the outputs but i can't seem to get any results.我读过最后一层可以定义为 nn.Linear 以压缩输出,但我似乎无法得到任何结果。 Tried other loss functions尝试其他损失函数
How can I change it in order for the model to classify the sentence , and not each word, as in the original tutorial?如何更改它以便模型对句子进行分类,而不是像原始教程中那样对每个单词进行分类?
我通过简单地获取最后一个的隐藏状态解决了这个问题
tag_space = self.hidden2tag(lstm_out[-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.