python - Tensorflow 在预测序列的类别时对模型的(输入)维度存疑

标签 python tensorflow machine-learning keras

我是 Tensorflow 新手,我正在尝试实现讽刺检测模型。 我的数据集由标有 1 或 0 的推文组成,以指示它们是否具有讽刺意味。

在预处理、标记化和填充阶段之后,我留下了固定长度的序列和关联的标签向量,以分割训练和测试集并作为模型的输入。序列采用以下形式:

>>> data
array([[    1,   677,   348, ...,     0,     0,     0],
       [    1,   677,   348, ...,     0,     0,     0],
       [    1,   825,     1, ...,     0,     0,     0],
       ...,
       [  908,  1376,   686, ...,     0,     0,     0],
       [    8,   158, 14579, ...,     0,     0,     0],
       [    1,     1,    35, ...,     0,     0,     0]], dtype=int32)

>>> data.shape
(3977, 50)
>>> data[0].shape
(50,)

模型如下:

num_words = len(tok.word_index) + 1 # tok is a Tokenizer which I fit on the data

import tensorflow as tk
from tensorflow import keras
early_stopping = keras.callbacks.EarlyStopping(monitor='val_loss', patience=2)

# The model
model = keras.Sequential()
model.add(keras.layers.Embedding(num_words, 64, input_length=Config.SEQUENCE_LENGTH, mask_zero=True))
model.add(keras.layers.GRU(64, return_sequences=True))
model.add(keras.layers.GRU(64))
model.add(keras.layers.Dense(1, activation='sigmoid'))

使用 model.compile(loss='binary_crossentropy', optimizationr='adam',metrics=['accuracy']) 编译模型并使用 sklearn 分割数据集后code> 的实用函数我称之为模型的 fit 方法:

model.fit(x_train, y_train, batch_size=10, epochs=10, validation_split=0.1, callbacks=[early_stopping])

训练模型后,evaluate方法按预期工作,将x_testy_test作为输入,但如果我调用 model.predict_classes(x_test[0]) (或 (model.predict(x_test[0]) > 0.5).astype("int32")) 而不是我得到的单个预测(50,1) 形的预测数组。 我尝试以这种方式 reshape x_test[0] model.predict_classes(x_test[0].reshape(1,50)) 并且我在数组中得到了一个预测:array ([[1]],dtype=int32)

所以现在我剩下以下问题(也是因为我在调用 evaluate(x_test, y_test) 时得到了 0.6 的准确度:

  • 为什么如果我将数据集作为数组数组 (x_train) 传递给模型,我不能只将测试集的一个元素传递给预测函数(例如 x_test[0]),但是我必须 reshape 它吗?
  • 这是正常的还是有什么错误?我是否将模型的输入尺寸设置错误?在将序列输入模型之前,我是否应该 reshape 序列?

最佳答案

Why if I pass the dataset to the model as an array of arrays (x_train), I cannot just pass an element of the test set to the predict function (e.g. x_test[0]) but I have to reshape it instead?

因为model.predict只能接受一组示例而不是单个示例,如果您想在其中提供单个示例,则必须将其重新调整为 (1,50) ,因此它是一组示例,其中集合的大小为 1。

但是,不要将示例一一输入 model.predict ,将一组示例输入 model.predict 会更有效。 ,即pred_test = model.predict(x_test)然后如果你想知道第 i 个例子的预测,请执行 prediction_of_the_ith_example = pred_test[i]

Is it normal or is there some error? Am I setting the input dimensions of the model wrong? Should I reshape the sequences also before feeding them to the model?

模型定义和训练部分正确。

关于标签的形状( y_trainy_test ),正确使用的形状是 (data_size,label_length),即 (3181,1)(796,1)分别。您在这里进行单标签分类,因此标签长度为1。但即使您使用(3181,)(796,) ,它之所以能正常工作,只是因为它为你做了自动广播。

关于python - Tensorflow 在预测序列的类别时对模型的(输入)维度存疑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68729136/

相关文章:

python - 重新运行程序时出现问题

python查找列表中数组的索引

python - 如何获得 1 :1 corresponding matches using sklearn KNearest neighbors

python - 使用其中一个数据帧作为键将 Python 中的数据帧组合到字典中

python - Pyspark 数据框 OrderBy 分区级别还是整体?

python - 如何将数组作为 Keras 模型的答案传递

validation - 评估 CNN 训练效果的最佳指标是什么?验证错误或训练损失?

arrays - 具有 RGB 输入和 BW 二进制输出的 CNN

tensorflow - 如何使用Tflearn构建词嵌入模型?

python - 在不同长度的音频文件上使用 sklearn