[关于构建张量流模型,有很多很好的教程,而我成功地创建了一个精度很高的模型。但是,还有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
根据您的问题,我了解您需要在两个方面提供帮助:
How do I achieve the "not predictable" for the not yet learned classes?
:a。因为您只考虑3个类,所以不必删除与其他类相对应的行,而是可以将这些列的名称替换为“不可预测”,即,将'D', 'E', 'F', etc..
替换为"Not Predictable"
。
b。在最终的Dense Layer
中,将Neurons
的编号从3更改为4,将表示"Not Predictable"
的第4类
Predictions
写入CSV
文件:现在Predictions
已作为Column
添加到DataFrame, df
中,您可以使用以下命令将其写入CSV文件,
df.to_csv('My_Predictions.csv')
有关此命令的更多信息,请参考this link。
访问Labels
的方式看起来很优雅。
[如果您遇到其他任何error
,请告诉我,我们将很乐意为您提供帮助。
希望这会有所帮助。祝您学习愉快!