2016-05-18 7 views
11

मैं अपेक्षाकृत नया तंत्रिका जाल के लिए नया हूं इसलिए कृपया मेरी अज्ञानता से क्षमा करें। मैं keras BLSTM उदाहरण here को अनुकूलित करने की कोशिश कर रहा हूं। उदाहरण ग्रंथों में पढ़ता है और उन्हें 0 या 1 के रूप में वर्गीकृत करता है। मुझे एक बीएलएसटीएम चाहिए जो पीओएस टैगिंग की तरह कुछ करता है, हालांकि लेमैमैटिंग या अन्य उन्नत सुविधाओं जैसे अतिरिक्त नहीं हैं, मैं बस एक मूल मॉडल चाहता हूं। मेरा डेटा वाक्यों की एक सूची है और प्रत्येक शब्द को श्रेणी 1-8 दिया जाता है। मैं एक बीएलएसटीएम को प्रशिक्षित करना चाहता हूं जो इस डेटा का उपयोग किसी भी शब्द के लिए एक अदृश्य वाक्य में श्रेणी की भविष्यवाणी करने के लिए कर सकता है।अनुक्रम लेबलिंग के लिए बीएलएसटीएम

उदा। इनपुट = ['द', 'कुत्ता', 'है', 'लाल'] आउटपुट = [2, 4, 3, 7]

यदि कैमरा उदाहरण सबसे अच्छा मार्ग नहीं है, तो मैं खुला हूं अन्य सुझाव

'''Train a Bidirectional LSTM.''' 

from __future__ import print_function 
import numpy as np 
from keras.preprocessing import sequence 
from keras.models import Model 
from keras.layers import Dense, Dropout, Embedding, LSTM, Input, merge 
from prep_nn import prep_scan 


np.random.seed(1337) # for reproducibility 
max_features = 20000 
batch_size = 16 
maxlen = 18 

print('Loading data...') 
(X_train, y_train), (X_test, y_test) = prep_scan(nb_words=max_features, 
               test_split=0.2) 
print(len(X_train), 'train sequences') 
print(len(X_test), 'test sequences') 

print("Pad sequences (samples x time)") 
# type issues here? float/int? 
X_train = sequence.pad_sequences(X_train, value=0.) 
X_test = sequence.pad_sequences(X_test, value=0.) # pad with zeros 

print('X_train shape:', X_train.shape) 
print('X_test shape:', X_test.shape) 

# need to pad y too, because more than 1 ouput value, not classification? 
y_train = sequence.pad_sequences(np.array(y_train), value=0.) 
y_test = sequence.pad_sequences(np.array(y_test), value=0.) 

print('y_train shape:', X_train.shape) 
print('y_test shape:', X_test.shape) 

# this is the placeholder tensor for the input sequences 
sequence = Input(shape=(maxlen,), dtype='int32') 

# this embedding layer will transform the sequences of integers 
# into vectors of size 128 
embedded = Embedding(max_features, 128, input_length=maxlen)(sequence) 

# apply forwards LSTM 
forwards = LSTM(64)(embedded) 
# apply backwards LSTM 
backwards = LSTM(64, go_backwards=True)(embedded) 

# concatenate the outputs of the 2 LSTMs 
merged = merge([forwards, backwards], mode='concat', concat_axis=-1) 
after_dp = Dropout(0.5)(merged) 
# number after dense has to corresponse to output matrix? 
output = Dense(17, activation='sigmoid')(after_dp) 

model = Model(input=sequence, output=output) 

# try using different optimizers and different optimizer configs 
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy']) 

print('Train...') 
model.fit(X_train, y_train, 
      batch_size=batch_size, 
      nb_epoch=4, 
      validation_data=[X_test, y_test]) 

X_test_new = np.array([[0,0,0,0,0,0,0,0,0,12,3,55,4,34,5,45,3,9],[0,0,0,0,0,0,0,1,7,65,34,67,34,23,24,67,54,43,]]) 

classes = model.predict(X_test_new, batch_size=16) 
print(classes) 

मेरे उत्पादन सही आयाम है, लेकिन मुझे 0-1 तैरता दे रहा है:

मैं वर्तमान में इस किया है। मुझे लगता है कि ऐसा इसलिए है क्योंकि यह अभी भी बाइनरी वर्गीकरण की तलाश में है। क्या किसी को पता है कि इसे किस प्रकार ठीक किया जा सकता है?

हल बस सुनिश्चित करें कि लेबल हर द्विआधारी सरणियों कर रहे हैं:

(X_train, y_train), (X_test, y_test), maxlen, word_ids, tags_ids = prep_model(
    nb_words=nb_words, test_len=75) 

W = (y_train > 0).astype('float') 

print(len(X_train), 'train sequences') 
print(int(len(X_train)*val_split), 'validation sequences') 
print(len(X_test), 'heldout sequences') 

# this is the placeholder tensor for the input sequences 
sequence = Input(shape=(maxlen,), dtype='int32') 

# this embedding layer will transform the sequences of integers 
# into vectors of size 256 
embedded = Embedding(nb_words, output_dim=hidden, 
        input_length=maxlen, mask_zero=True)(sequence) 

# apply forwards LSTM 
forwards = LSTM(output_dim=hidden, return_sequences=True)(embedded) 
# apply backwards LSTM 
backwards = LSTM(output_dim=hidden, return_sequences=True, 
       go_backwards=True)(embedded) 

# concatenate the outputs of the 2 LSTMs 
merged = merge([forwards, backwards], mode='concat', concat_axis=-1) 
after_dp = Dropout(0.15)(merged) 

# TimeDistributed for sequence 
# change activation to sigmoid? 
output = TimeDistributed(
    Dense(output_dim=nb_classes, 
      activation='softmax'))(after_dp) 

model = Model(input=sequence, output=output) 

# try using different optimizers and different optimizer configs 
# loss=binary_crossentropy, optimizer=rmsprop 
model.compile(loss='categorical_crossentropy', 
       metrics=['accuracy'], optimizer='adam', 
       sample_weight_mode='temporal') 

print('Train...') 
model.fit(X_train, y_train, 
      batch_size=batch_size, 
      nb_epoch=epochs, 
      shuffle=True, 
      validation_split=val_split, 
      sample_weight=W) 
+0

क्या आप कृपया अपने डेटा का एक छोटा सा नमूना अपलोड कर सकते हैं? यह बेहतर ढंग से समझने में मदद करता है कि मॉडल कैसे काम करता है। यदि यह संभव नहीं है, तो क्या आप मॉडल को खिलाने से पहले डेटा प्रीप्रोकैसिंग के लिए अपना कच्चा डेटा कैसा दिखता है और आपने क्या किया है, इस बारे में थोड़ा सा समझा सकते हैं? –

उत्तर

4

हल। मुख्य मुद्दा वर्गीकरण श्रेणियों के लिए बाइनरी सरणी के रूप में डेटा को दोबारा बदल रहा था। टाइम डिस्ट्रिब्यूटेड का भी उपयोग किया जाता है और True_sequences को True पर सेट करता है।

+0

क्या यह संभव है कि आप कार्य कोड जोड़ दें? thx – PSNR

+0

निश्चित रूप से, बस उपरोक्त संपादन देखें। – ChrisDH

+0

धन्यवाद! जब हम एम्बेडिंग परत में 'mask_zero = True' का उपयोग करते हैं, तो मुझे लगता है कि निम्न सभी परतें इस वर्ग को हानि, सटीकता इत्यादि की गणना के लिए अनदेखा कर देगी? और मुझे लगता है कि आउटपुट परत में 'nb_classes' कक्षा + 1 का होना चाहिए (यानी बाइनरी वर्गीकरण समस्या के लिए यह 3 है)? एक बार फिर धन्यवाद! – PSNR

संबंधित मुद्दे