【问题标题】:How use custom features in Keras for text classification如何在 Keras 中使用自定义功能进行文本分类
【发布时间】:2018-12-30 22:06:23
【问题描述】:

我正在使用 Keras 为 Python 中的文本分类器工作。目前,我尝试使用 词袋 仅使用我的数据集的词制作模型。现在我将在我的分类器中使用其他自定义功能(如极性),但我不知道如何在我的代码中添加。我的数据集是这样的:

 Text                    | Polarity | Number of words | Classification 

 Hello my name is John   |    0,05  |        5        |        0
 How old are you?        |    0,00  |        4        |        1
 I'm very hungry         |   -0,05  |        4        |        0

中间两列是我想添加到分类器中的自定义功能,但我不知道如何添加。

train_x = tokenizer.sequences_to_matrix(allWordIndices, mode='binary')
train_x2 = train_x

train_x = train_x[1000:]
test_x = train_x2[:1000]
train_y = keras.utils.to_categorical(train_y, 2)
train_y2 = train_y
train_y = train_y[1000:]
test_y = train_y2[:1000]


from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation

model = Sequential()
model.add(Dense(30, input_shape=(max_words,), activation='relu'))
model.add(Dropout(0.45)) 
model.add(Dense(100, activation='softplus'))
model.add(Dropout(0.45))
model.add(Dense(2, activation='softmax'))

model.compile(loss='categorical_crossentropy',optimizer='RMSProp',metrics=['accuracy'])

history = model.fit(train_x,train_y,batch_size=32,epochs=10,verbose=1,validation_split=0.1,shuffle=True)

score = model.evaluate(test_x,test_y, batch_size=128)

在此示例中,我仅使用内容 f 第一列的词袋特征,并且我想添加其他 2 列类似特征(极性、词数)。有人知道如何添加这些吗?提前致谢。

【问题讨论】:

    标签: python keras classification data-analysis text-classification


    【解决方案1】:

    对于 Bag of words,您可以将您的数字特征连接到您的 BoW 向量之上。因此,您可以只使用 numpy,甚至更简单的 pandas。然后你有一个维度为 max_words + custom_numerical_features 的向量。

    不管怎样,我做了一些类似的事情,并且使用了几种方法,比如 BoW 和嵌入。

    在您的网络中分离文本特征和数字特征是个好主意。为此,您可以使用多个输入模型。我刚刚写了一篇关于它的博客,你可以看看here。使用了嵌入,但总的来说它也适用于 BoW。

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2016-12-27
      • 2020-07-29
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      相关资源
      最近更新 更多