-
-
Save urigoren/b7cd138903fe86ec027e715d493451b4 to your computer and use it in GitHub Desktop.
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) |
I tried:
y_pred = model.predict(X_test)
But Im not getting the target
would it work if inputs are string values, like date - '03/07/2012' ?Thanks.
Hey, this example does not learn, it only returns 0, no matter what sequence.
@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.
what to do if the sequences have negative values as well?
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.
If the output was string value, Is it possible that classify our data?