GRU的下一个字符预测每次都给出不同的结果



我试图预测对话中情感的演变。为此,我使用BERT来获得情感。然后,对于每个呼叫,我都将情感编码为P表示积极,E表示消极,N表示中立。考虑到这只是下一个字符预测问题,我使用了这个https://www.tensorflow.org/text/tutorials/text_generation逐字逐句的教程,在我自己的数据上训练它。问题是,每次我进行推理时,它都会给出不同的结果。

call_index 情感
6081bdae52c838000aaa53d3 PNNNNPNPPENNNNNPNNE
6081c27bde933a000a4384b0 宾夕法尼亚州
6081c54dd12abf000ab3c6f5 NNPNNNNNPPNNN
6081c666d7a1f7001cecce98 NNNNN PP
6081d8576eb5530043e3401f

文本生成的最简单的方法之一是执行教程中###突出显示的行中实现的操作:

@tf.function
  def generate_one_step(self, inputs, states=None):
    input_chars = tf.strings.unicode_split(inputs, 'UTF-8')
    input_ids = self.ids_from_chars(input_chars).to_tensor()
    predicted_logits, states = self.model(inputs=input_ids, states=states,
                                          return_state=True)
    predicted_logits = predicted_logits[:, -1, :]
    predicted_logits = predicted_logits/self.temperature
    predicted_logits = predicted_logits + self.prediction_mask
    ### THE FOLLOWING LINE IS IMPORTANT ###
    predicted_ids = tf.random.categorical(predicted_logits, num_samples=1)
    predicted_ids = tf.squeeze(predicted_ids, axis=-1)
    predicted_chars = self.chars_from_ids(predicted_ids)
    return predicted_chars, states

您可能被提醒了,因为您希望生成是确定性的,这对于天真的方法来说是正确的:始终返回最可能的字符/单词/令牌/等。,但这根本不是语言的工作方式。如果你对细节感兴趣,可以去看斯坦福大学的NLP课程(它可以在YouTube上免费观看(,否则,好吧,就这样吧,算法中存在随机性。

最新更新