-
-
Save ph3b/c1171a90693912693025b348f000361e to your computer and use it in GitHub Desktop.
LSTM Binary classification with Keras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sequence | target | |
---|---|---|
1 2 3 | 1 | |
2 3 1 | 0 | |
2 3 4 | 1 | |
4 2 1 | 0 | |
4 3 1 | 0 | |
3 2 1 | 0 | |
1 2 4 | 1 | |
2 2 3 | 1 | |
2 1 3 | 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.layers import Dense, Dropout, LSTM, Embedding | |
from keras.preprocessing.sequence import pad_sequences | |
from keras.models import Sequential | |
import pandas as pd | |
import numpy as np | |
input_file = 'input.csv' | |
def load_data(test_split = 0.2): | |
print ('Loading data...') | |
df = pd.read_csv(input_file) | |
df['sequence'] = df['sequence'].apply(lambda x: [int(e) for e in x.split()]) | |
df = df.reindex(np.random.permutation(df.index)) | |
train_size = int(len(df) * (1 - test_split)) | |
X_train = df['sequence'].values[:train_size] | |
y_train = np.array(df['target'].values[:train_size]) | |
X_test = np.array(df['sequence'].values[train_size:]) | |
y_test = np.array(df['target'].values[train_size:]) | |
return pad_sequences(X_train), y_train, pad_sequences(X_test), y_test | |
def create_model(input_length): | |
print ('Creating model...') | |
model = Sequential() | |
model.add(Embedding(input_dim = 188, output_dim = 50, input_length = input_length)) | |
model.add(LSTM(output_dim=256, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True)) | |
model.add(Dropout(0.5)) | |
model.add(LSTM(output_dim=256, activation='sigmoid', inner_activation='hard_sigmoid')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(1, activation='sigmoid')) | |
print ('Compiling...') | |
model.compile(loss='binary_crossentropy', | |
optimizer='rmsprop', | |
metrics=['accuracy']) | |
return model | |
X_train, y_train, X_test, y_test = load_data() | |
model = create_model(len(X_train[0])) | |
print ('Fitting model...') | |
hist = model.fit(X_train, y_train, batch_size=64, nb_epoch=10, validation_split = 0.1, verbose = 1) | |
score, acc = model.evaluate(X_test, y_test, batch_size=1) | |
print('Test score:', score) | |
print('Test accuracy:', acc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment