Created
June 26, 2016 14:01
-
-
Save pablodecm/dcf740c3143153702ae910dd4e16ce4d to your computer and use it in GitHub Desktop.
This file contains 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
import numpy as np | |
from sklearn.cross_validation import train_test_split | |
from sklearn import preprocessing | |
data = np.load("data/data.npz") | |
X_train, X_test, y_train, y_test, w_train, w_test = train_test_split(data['X'], data['y'], data['w'], test_size=0.33, random_state=42) | |
import logging | |
from sklearn.metrics import roc_auc_score | |
from keras.callbacks import Callback | |
class IntervalEvaluation(Callback): | |
def __init__(self, validation_data=(), interval=1): | |
super(Callback, self).__init__() | |
self.interval = interval | |
self.X_val, self.y_val, self.w_val = validation_data | |
def on_epoch_end(self, epoch, logs={}): | |
if epoch % self.interval == 0: | |
y_pred = self.model.predict_proba(self.X_val, batch_size=16000,verbose=0) | |
score = roc_auc_score(self.y_val, y_pred, sample_weight=self.w_val) | |
print "interval evaluation - epoch: {:d} - score: {:.6f} \n".format(epoch, score) | |
ival = IntervalEvaluation(validation_data=(X_test, y_test,w_test), interval=1) | |
from keras.models import Sequential | |
from keras.layers.core import Masking | |
from keras.layers import LSTM,Dense, Dropout, Activation | |
model = Sequential() | |
model.add(Masking(mask_value=0.,input_shape=X_train[0].shape)) | |
model.add(LSTM(256)) | |
model.add(Dense(1024, activation="relu")) | |
model.add(Dense(1024, activation="relu")) | |
model.add(Dense(1)) | |
model.add(Activation('sigmoid')) | |
model.compile(loss='binary_crossentropy', | |
optimizer='adam', | |
metrics=['accuracy']) | |
model.fit(X_train, y_train, batch_size=256, nb_epoch=10, validation_data = (X_test, y_test),callbacks=[ival], class_weight=[13.585,1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment