Skip to content

Instantly share code, notes, and snippets.

@urigoren
Last active June 22, 2023 19:37
Show Gist options
  • Save urigoren/b7cd138903fe86ec027e715d493451b4 to your computer and use it in GitHub Desktop.
Save urigoren/b7cd138903fe86ec027e715d493451b4 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)
@hasanisaeed
Copy link

If the output was string value, Is it possible that classify our data?

@neelratanguria
Copy link

I tried:
y_pred = model.predict(X_test)

But Im not getting the target

@TejasHonmode
Copy link

would it work if inputs are string values, like date - '03/07/2012' ?Thanks.

@guysoft
Copy link

guysoft commented Oct 4, 2018

Hey, this example does not learn, it only returns 0, no matter what sequence.

@getamu
Copy link

getamu commented Oct 8, 2018

@guysoft, Did you find the solution to the problem? I am also having the same issue. I tried to print out the gradients to see if there was any gradient flow as described : https://gist.github.com/mickypaganini/a2291691924981212b4cfc8e600e52b1 , but was having issue with that as well.

@abirjameel
Copy link

abirjameel commented Oct 18, 2018

what to do if the sequences have negative values as well?

@mohammadsohaib
Copy link

mohammadsohaib commented Aug 13, 2019

what to do if the sequences have negative values as well?

If you are still looking for a solution,
1)Replace every negative sign with a 0. Eg- 2-31=2031 or 12-6=1206. This will work correctly if your sequence itself does not involve zeros.
2) or alternatively, convert the sequence into a binary representation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment