model.predict() 输出对应于 3 类 中的哪一个?
Which of 3 classes does the model.predict() output corresponds to?
当我加载经过训练的模型并为其提供新数据以进行预测时 (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
我如何判断哪个 class(发送,训练值)对应于我得到的输出?上面的 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:
我像这样使用 tokenizer 来创建 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)
如果我没理解错的话,你想要预测 sent
列,它似乎是一个分类变量,其值可能是 0、1 或 2。你的最后一层是 3 个神经元(带有softmax 激活),因此每次推理将有 3 个输出。我假设您对地面实况输出使用了类似 One Hot Encoding 的东西。预测的顺序将与您的 One Hot Encoder 设置的顺序相同。
您是如何准备数据集地面实况输出的?
更新:
这实际上是一个很好的问题,因为没有太多关于它的文档。
我搜索了一下,在 SO 上发现了类似的问题,所以我询问了更多详细信息 :
In case of using sparse_categorical_crossentropy
, the category you
have assigned to number 0 is actually class 0; the category you have
assigned to number 1 is actually class 1; and so on. For example, if
you use LabelEncoder from sklearn, you can find out this mapping
via .classes_ attribute (see the documentation and examples).
希望对您有所帮助!
当我加载经过训练的模型并为其提供新数据以进行预测时 (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
我如何判断哪个 class(发送,训练值)对应于我得到的输出?上面的 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: 我像这样使用 tokenizer 来创建 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)
如果我没理解错的话,你想要预测 sent
列,它似乎是一个分类变量,其值可能是 0、1 或 2。你的最后一层是 3 个神经元(带有softmax 激活),因此每次推理将有 3 个输出。我假设您对地面实况输出使用了类似 One Hot Encoding 的东西。预测的顺序将与您的 One Hot Encoder 设置的顺序相同。
您是如何准备数据集地面实况输出的?
更新:
这实际上是一个很好的问题,因为没有太多关于它的文档。
我搜索了一下,在 SO 上发现了类似的问题,所以我询问了更多详细信息
In case of using
sparse_categorical_crossentropy
, the category you have assigned to number 0 is actually class 0; the category you have assigned to number 1 is actually class 1; and so on. For example, if you use LabelEncoder from sklearn, you can find out this mapping via .classes_ attribute (see the documentation and examples).
希望对您有所帮助!