gpt4 book ai didi

keras - 检查语言模型的复杂度

转载 作者:行者123 更新时间:2023-12-03 08:58:09 27 4
gpt4 key购买 nike

我使用 Keras LSTM 创建了一个语言模型,现在我想评估它是否良好,因此我想计算困惑度。

在 Python 中计算模型复杂度的最佳方法是什么?

最佳答案

我提出了两个版本并附上了相应的源码,请随意查看链接。

def perplexity_raw(y_true, y_pred):
"""
The perplexity metric. Why isn't this part of Keras yet?!
https://stackoverflow.com/questions/41881308/how-to-calculate-perplexity-of-rnn-in-tensorflow
https://github.com/keras-team/keras/issues/8267
"""
# cross_entropy = K.sparse_categorical_crossentropy(y_true, y_pred)
cross_entropy = K.cast(K.equal(K.max(y_true, axis=-1),
K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
K.floatx())
perplexity = K.exp(cross_entropy)
return perplexity

def perplexity(y_true, y_pred):
"""
The perplexity metric. Why isn't this part of Keras yet?!
https://stackoverflow.com/questions/41881308/how-to-calculate-perplexity-of-rnn-in-tensorflow
https://github.com/keras-team/keras/issues/8267
"""
cross_entropy = K.sparse_categorical_crossentropy(y_true, y_pred)
perplexity = K.exp(cross_entropy)
return perplexity

关于keras - 检查语言模型的复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53515547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com