如何训练模型来对以下句子进行逻辑或不逻辑分类?
“他有两条腿”——符合逻辑 “他有六条腿”——不合逻辑
我尝试过的解决方案:
1:通过cnn训练分类器
我以前做过,如果你有足够的数据,效果很好。问题是我没有一个巨大的数据集来为这种情况贴上“逻辑”或“不逻辑”的标签。
2:使用语言模型
在wiki等数据集上训练gluonnlp引入的语言模型,用它来找出句子的概率。如果句子的概率很高,则将其标记为逻辑,反之亦然。问题是结果不好。
我估计概率的方式
def __predict(self):
lines = self.__text_edit_input.toPlainText().split("\n")
result = ""
for line in lines:
result += str(self.__sentence_prob(line, 10)) + "\n"
self.__text_edit_output.setPlainText(result)
def __prepare_sentence(self, text, max_len):
result = mx.nd.zeros([max_len, 1], dtype='float32')
max_len = min(len(text), max_len)
i = max(max_len - len(text), 0)
j = 0
for index in range(i, max_len):
result[index][0] = self.__vocab[text[j]]
j = j + 1
return result
def __sentence_prob(self, text, max_len):
hiddens = self.__model.begin_state(1, func=mx.nd.zeros, ctx=self.__context)
tokens = self.__tokenizer(text)
data = self.__prepare_sentence(tokens, max_len)
output, _ = self.__model(data, hiddens)
prob = 0
for i in range(max_len):
total_prob = mx.nd.softmax(output[i][0])
prob += total_prob[self.__vocab[i]].asscalar()
return prob / max_len
语言模型可能出现的问题:
1. Do not use correct way to split the sentences(I am using jieba to split the Chinese senteces)
2. Number of vocab is too small/big(test 10000, 15000 and 30000)
3. Loss too high(ppl around 190) after 50 epochs?
4. Number of sentences length should be larger/smaller(tried 10,20,35)
5. The data I use do not meet my requirements(not every sentences are logical)
6. Language model is not appropriate for this task?
有什么建议吗?
问题 6. 语言模型不适合此任务? 是主要问题。构建语言模型是为了在语言使用(语法、语义等)方面理解输入文本,而不是得出逻辑结论。因此,即使使用大量数据或非常深的模型,您也可能无法获得良好的结果。 您要解决的问题极其困难。您可能想查看的是Symbolic AI。该领域有很多正在进行的研究。