2017-11-04 11 views
2

私のトレーニングデータは、kerasライブラリを使ってX_dataで 'y'を予測したいと思っています。私は多くの時間エラーが発生している、私はデータの形について知っているが、私はしばらく固執している。あなたたちが助けてくれることを願っています。kerasライブラリを使用してNLP分類を訓練する方法は?

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'を予測したいのですか?

答えて

3

パンダのデータフレームをnumpy配列に変換する必要があります。配列は不揃いになり、埋め込む必要があります。また、単語をニューラルネットワークに直接渡すことはできないため、単語ベクトルの辞書を設定する必要があります。いくつかの例は、here,hereおよびhereです。あなた自身の研究をここで行う必要があります。あなたが提供したデータサンプルで多くを行うことは不可能です。

length = len(X_data)あなたが持っているデータのサンプル数は、ケラスはこれを気にしません。あなたが入力として持っているどのように多くの単語を知っている、(パディングが先に述べた理由である、それぞれに対して同じである必要があります)

ので、ネットワークへの入力は、あなたが持っているどのように多くの列である

#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個のカテゴリーがあり、正しい活性化がmulitclass問題のsoftmax

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

これはマルチクラスであるので、あなたの損失は今、categorical_crossentropyなければならないとすると、今10です

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

私はそれを動作させました!私は再び何かを予測することはできませんいくつかの形状のエラーを示しています。正直言って、本当にイライラしています。適切な予測を得るためには、どのようなデータが必要なのかを教えてください。 – user169772

+0

私はエラーを知らずに、またはあなたが使った新しい設定が何であるかを知ることができません。だからあなたの言うモデルは訓練しますが、 'model.predict()'を実行するとエラーが出ますか? – DJK

+0

テキストのパディングとエンコードのステップに従うことで予測が得られましたが、出力が奇妙に見えます 入力用:jelly = 'サービス料。 6月「 のためのコンサルティングサービス私取得出力: 配列([[0.48915482]、 [0.48839182]、 [0.49011096]、 [0.48880994]、 [0.4904303]、 [0.48839182]、 [0.48839182]、 .... このパターンは、試したすべての入力に対して0.48-0.495に近いすべての入力で似ています – user169772

関連する問題