2017-11-04 4 views
2

내 훈련 데이터입니다. keras 라이브러리를 사용하여 X_data로 'y'를 예측하고 싶습니다. 많은 시간 동안 오류가 발생했습니다. 데이터 모양에 대해 알고 있지만 잠시 멈춰 있습니다. 당신들이 도울 수 있기를 바랍니다.keras 라이브러리를 사용하여 NLP 분류를 훈련시키는 방법은 무엇입니까?

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 = 
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 

나는이 모델을 사용하고 내 훈련 예측입니까? 입력 X_data를 사용하여 'y'를 예측하고 싶습니까?

답변

3

팬더 데이터 프레임을 numpy 배열로 변환해야 할 필요가 있으므로 배열을 거칠게 패딩해야합니다. 신경망에 단어를 직접 전달할 수 없기 때문에 단어 벡터 사전을 설정해야합니다. 몇 가지 예는 here, herehere입니다. 여기에 자신의 연구를 할 필요가 있습니다. 제공 한 데이터 샘플로 그다지 많은 일을 할 수 없습니다.

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 개 개의 카테고리를 가지고 올바른 활성화가 mulitclass 문제에 대한 softmax라고 가정하면, 지금 10

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

이 멀티 클래스이기 때문에 당신의 손실은 지금 categorical_crossentropy 수있다

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

그러나 나는 어떤 것을 다시 예측할 수 없다. 그것은 어떤 모양 오류를 보여준다. 솔직히 말해서 정말 실망 스럽습니다. 올바른 예측을하기 위해 어떤 종류의 데이터가 필요한지 말할 수 있습니까? – user169772

+0

오류를 알지 못했거나 정확히 새로운 설정이 무엇인지 알 수 없습니다. 그래서 당신의 모델이 말하길 말하는데'model.predict()'를 실행하면 에러가 나옵니까? – DJK

+0

필자는 텍스트의 패딩 및 인코딩 단계를 수행하여 예측을 얻었지만 출력이 이상하게 보입니다. 입력 : jelly = '서비스 요금. 6월 ' 위한 컨설팅 I 얻을 출력 : 어레이 ([0.48915482, [0.48839182, [0.49011096, [0.48880994, [0.4904303, [0.48839182, [0.48839182, .... 이 패턴은 시도한 모든 입력에 대해 0.48-0.495에 가까운 모든 입력에 대해 비슷합니다. – user169772

관련 문제