Skip to content

Instantly share code, notes, and snippets.

@PaulZhangIsing
Forked from urigoren/LSTM_Binary.py
Created December 21, 2018 10:19
Show Gist options
  • Save PaulZhangIsing/01a7899aa61d0dc51d4d3313ff2bd71a to your computer and use it in GitHub Desktop.
Save PaulZhangIsing/01a7899aa61d0dc51d4d3313ff2bd71a to your computer and use it in GitHub Desktop.
LSTM Binary classification with Keras
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
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