【问题标题】:next character prediction with GRU gives different results each time使用 GRU 的下一个字符预测每次都会给出不同的结果
【发布时间】:2022-01-02 00:26:38
【问题描述】:
我试图在对话中预测情绪的演变。为此,我使用了 BERT 来获取情绪。然后对于每个电话,我将情绪编码为 P 表示正面,E 表示负面,N 表示中性。考虑到这只是下一个字符预测问题,我已经使用这个https://www.tensorflow.org/text/tutorials/text_generation 教程逐字逐句地在我自己的数据上训练它。问题是每次我运行推理时它都会给出不同的结果。
| call_index |
sentiments |
| 6081bdea52c838000aaa53d3 |
PNNNNPNPNPPENNNNNEPNNE |
| 6081c27bde933a000a4384b0 |
PENNNNNEENNPNPEPNPPNNNNNNNNNNN |
| 6081c54dd12abf000ab3c6f5 |
NNPNNNPNNNPPNNN |
| 6081c666d7a1f7001cecce98 |
NNNNNPP |
| 6081d8576eb5530043e3401f |
NNNNPNNNNNNNNNNNNNNNNNNPPNNNNNNNNNENNNNNNENNNN |
【问题讨论】:
标签:
python
tensorflow
deep-learning
lstm
tf.keras
【解决方案1】:
文本生成最简单的重要方法之一是执行教程中在###-highlighted 行中实现的操作:
@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 上免费观看),否则,好吧,你去吧,算法中存在随机性。