【问题标题】:How to train NLP classification using keras library?如何使用 keras 库训练 NLP 分类?
【发布时间】:2018-04-17 04:18:39
【问题描述】:

这是我的训练数据,我想使用 keras 库用 X_data 预测“y”。我经常遇到错误,我知道它关于数据形状,但我被困了一段时间。希望大家帮忙。

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)

在这个模型中使用这些数据有什么问题?我想使用输入 X_data 预测“y”?

【问题讨论】:

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


    【解决方案1】:

    您需要将您的 pandas 数据帧转换为 numpy 数组,这些数组将是参差不齐的,因此您需要填充它们。您还需要设置一个词向量字典,因为您不能直接将词传递到神经网络中。一些示例是hereherehere。您需要在这里进行自己的研究,您提供的数据样本无法做很多事情

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

    所以你对网络的输入就是你有多少列

    #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'])
    

    【讨论】:

    • 我成功了!但我无法再次预测任何东西,它显示出一些形状错误。老实说,这真的很令人沮丧。你能说出需要什么样的数据才能做出正确的预测吗?
    • 我不能不知道错误,或者您使用的新设置到底是什么。所以你说模型训练但是当你运行 model.predict() 时你得到一些错误?
    • 我按照文本的填充和编码步骤得到了预测,但输出看起来很奇怪对于输入:jelly = '服务费。六月的咨询服务'我得到输出:数组([[0.48915482],[0.48839182],[0.49011096],[0.48880994],[0.4904303],[0.48839182],[0.48839182],...对于我尝试过的所有输入,输入接近 0.48-0.495
    • 我个人不相信我可以在 cmets 中调试这个问题,因为我已经被输出弄糊涂了,还有 15 个左右的 cmets 才能真正知道这个问题。我建议,你试一试调试(我们通过一些努力学得最好),然后问一个新问题,如果它变得太多,因为这已经成为一个单独的问题
    猜你喜欢
    • 2018-02-13
    • 1970-01-01
    • 2017-06-11
    • 2018-08-28
    • 2021-01-20
    • 1970-01-01
    • 2013-05-05
    • 2018-05-13
    • 2017-06-08
    相关资源
    最近更新 更多