【发布时间】:2020-03-07 17:40:13
【问题描述】:
当我加载经过训练的模型并为其提供新数据以进行预测时 (model.predict(textstr)) 我得到:
[[0.3345264 0.33339804 0.33207548]]
使用如下所示的数据框训练模型:
text sent
1 Textstring1... 1
2 Textstring2... 2
3 Textstring3... 0
4 Textstring4... 0
5 Textstring5... 2
我如何判断哪个类(发送,训练值)对应于我得到的输出?上面的 0.3345264 值是否对应模型发送的 0 作为答案?
以下是模型及其配置的一些细节:
model = tf.keras.Sequential([
tf.keras.layers.Embedding(VOC, EMB_SIZE),
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.Dense(node1,activation='relu'),
tf.keras.layers.Dropout(dropout),
tf.keras.layers.Dense(3, activation='softmax')])
model.compile(optimizer='adadelta',
loss='sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'])
提前致谢。
更新编辑2: 我像这样使用标记器来创建 train_seqs:
tokenizer = tf.keras.preprocessing.text.Tokenizer(
num_words=WORDS
, oov_token='<UNK>')
tokenizer.fit_on_texts(train_df['text'])
#convert text data to numerical indexes
train_seqs=tokenizer.texts_to_sequences(train_df['text'])
test_seqs=tokenizer.texts_to_sequences(test_df['text'])
#pad data up to SEQ_LEN (note that we truncate if there are more than SEQ_LEN tokens)
train_seqs=tf.keras.preprocessing.sequence.pad_sequences(
train_seqs
, maxlen=SEQ_LEN
, padding="post")
test_seqs=tf.keras.preprocessing.sequence.pad_sequences(
test_seqs
, maxlen=SEQ_LEN
, padding="post")
train_seqs
Out[12]:
array([[ 144, 8, 46, ..., 42, 3, 1734],
[ 6, 315, 277, ..., 44, 2247, 2095],
[ 5, 18, 162, ..., 159, 56, 1483],
...,
[ 9, 132, 76, ..., 194, 234, 1628],
[ 660, 66, 7, ..., 0, 0, 0],
[ 514, 879, 126, ..., 6, 68, 590]], dtype=int32)
train_df['sent'].values
Out[13]: array([1, 0, 2, ..., 0, 1, 0])
history = model.fit(train_seqs, train_df['sent'].values
, batch_size=BATCH_SIZE
, epochs=EPOCHS
, validation_split=0.2
, callbacks=callbacks)
【问题讨论】:
-
请提供您如何准备数据集
-
我的意思是当你运行 model.fit(...) 时,你必须传递 X 和 Y 作为输入和输出。能否请您发布用于生成 X 和 Y 的代码,以及使用 fit 方法的代码?
-
@alan.elkin 我更新了问题......