首页 > 解决方案 > 如何使用 tensorflow 模型从 h5 中获得预测

问题描述

所以我无法弄清楚如何从这个矩阵中获得百分比或标签的概率。

这个模型背后的故事:

- 该模型应该检测是否应该删除评论(/文本)。

-标签是真或假

有什么方法可以让它工作或者我可以如何转换那个向量?

我的代码:

model = models.load_model(h5_model)
string = "sh!t"
x_test = string.lower()
tok = text.Tokenizer(num_words=max_features, lower=True)
tok.fit_on_texts(list(x_test))
x_test = tok.texts_to_sequences(x_test)
text_preprocessing_for_single_comments(string)
prediction = model.predict(to_predict)
print(prediction)

>>[[0.5180945 ][0.5354299 ][0.47555092] [0.5636673]]

标签: pythontensorflowkerasnlpprediction

解决方案


预测是类概率。对于百分比转换:

prediction *= 100

如果 1 表示真,0 表示假,那么高于 0.5 的概率表示真,低于 0.5 的概率表示假。

prediction = np.round( prediction )
# Get an array of 0s and 1s
class = np.argmax( prediction )

推荐阅读