如何使用keras库训练NLP分类?

时间:2017-11-04 16:00:26

标签: python machine-learning nlp keras training-data

这是我的培训数据,我想预测' y'与X_data使用keras库。我在很长一段时间内都遇到了错误,我知道它的数据形状,但我已经停留了一段时间。希望你们能帮忙。

X_data =
0     [construction, materials, labour, charges, con...
1     [catering, catering, lunch]
2     [passenger, transport, local, transport, passe...
3     [goods, transport, road, transport, goods, inl...
4     [rental, rental, aircrafts]
5     [supporting, transport, cargo, handling, agenc...
6     [postal, courier, postal, courier, local, deli...
7     [electricity, charges, reimbursement, electric...
8     [facility, management, facility, management, p...
9     [leasing, leasing, aircrafts]
10    [professional, technical, business, selling, s...
11    [telecommunications, broadcasting, information...
12    [support, personnel, search, contract, tempora...
13    [maintenance, repair, installation, maintenanc...
14    [manufacturing, physical, inputs, owned, other...
15    [accommodation, hotel, accommodation, hotel, i...
16    [leasing, rental, leasing, renting, motor, veh...
17    [real, estate, rental, leasing, involving, pro...
18    [rental, transport, vehicles, rental, road, ve...
19    [cleaning, sanitary, pad, vending, machine]
20    [royalty, transfer, use, ip, intellectual, pro...
21    [legal, accounting, legal, accounting, legal, ...
22    [veterinary, clinic, health, care, relation, a...
23    [human, health, social, care, inpatient, medic...
Name: Data, dtype: object

这是我的训练预测器

y = 

0      1
1      1
2      1
3      1
4      1
5      1
6      1
7      1
8      1
9      1
10     1
11     1
12     1
13     1
14     1
15    10
16     2
17    10
18     2
19     2
20    10
21    10
22    10
23    10

我正在使用这个模型:

top_words = 5000
length= len(X_data)
embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(embedding_vecor_length, top_words, input_length=length))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_data, y, epochs=3, batch_size=32)

ValueError: Error when checking input: expected embedding_8_input to have shape (None, 24) but got array with shape (24, 1)

在此模型中使用此数据有什么问题?我想预测' y'使用输入X_data?

1 个答案:

答案 0 :(得分:3)

您需要将您的pandas数据帧转换为numpy数组,这些数组将变得粗糙,因此您需要填充它们。您还需要设置单词向量字典,因为您不能直接将单词传递到神经网络中。一些示例包括hereherehere。您需要在此处进行自己的研究,不可能对您提供的数据样本做很多事情

length = len(X_data)是你有多少数据样本,keras不关心这个,它想知道你有多少单词作为输入,(每个单词都必须相同,这就是为什么填充已在前面说过)

所以您对网络的输入是您拥有的列数

#assuming you converted X_data correctly to numpy arrays and word vectors
model.add(Embedding(embedding_vecor_length, top_words, input_length=X_data.shape[1]))

您的分类值必须是二进制值。

from keras.utils import to_categorical

y = to_categorical(y)

你的上一个密集层现在是10,假设你有10个类别,并且正确的激活是softmax的多重问题

model.add(Dense(10, activation='softmax'))

你的损失现在必须是categorical_crossentropy,因为这是多类

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