【发布时间】:2023-03-15 03:21:01
【问题描述】:
有很多关于构建 tensorflow 模型的好教程,我成功地创建了一个准确度很高的模型。但是,还有 2 个问题。
在我的数据集中有很多类,我试着这样说明:
label - text
--------------------
A - this is a A text
B - this is a B text
C - this is a C text
...
Z - this is a Z text
...
ZA - this is a ZA text
...
现在我想建立一个倾向于对文本进行分类的网络。我明白,我必须提供一组固定的标签,因为网络需要有固定数量的“输出神经元”。因此,出于学习目的,我开始为 3 个类 A、B 和 C 构建一个网络。我只为网络提供了相应的行(A、B、C),我得到了一个可以识别 A、B 的模型, C 具有良好的准确性。
现在我想预测新文本并希望得到这样的输出:
input text -> predicted label
----------------------------
this is a B text -> B // successful prediction
this is a xyz text -> ? // cannot be predicted, because not learned
我如何为尚未学习的课程实现“不可预测”?
总之,我获得一个添加了预测列的 csv 文件可能有点笨拙。你能告诉我如何做得更好吗?
import pandas as pd
df = pd.read_parquet(path)
#print(df)
#label = df['kategorie'].fillna("N/A")
text = df['text'].fillna("")
text_padded = tokenize_and_pad(text)
# Predictions
probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
predictions = probability_model.predict(text_padded)
# get the predicted labels
# I only achieved this with this loop - there must be a more elegant way???
predictedLabels = []
for prediction in predictions:
labelID = np.argmax(prediction)
predictedLabel = label_encoder.inverse_transform([labelID])
predictedLabels.append(predictedLabel)
# add the new column to the dataframe
# the prediction is accurate for the learned labels
# but totally wrong for the labels, that I excluded from the learning
df['predictedLabels'] = predictedLabels
# todo: write to file
【问题讨论】:
标签: python tensorflow prediction