Skip to content

Instantly share code, notes, and snippets.

@cnh
Last active November 29, 2017 15:30
Show Gist options
  • Save cnh/53d387df65501969858e55538cb22ca1 to your computer and use it in GitHub Desktop.
Save cnh/53d387df65501969858e55538cb22ca1 to your computer and use it in GitHub Desktop.
#import data manipulation modules
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import train_test_split
from keras.models import Sequential, load_model #to save , load and persist models
from keras.layers import Activation
from keras.optimizers import SGD
from keras.layers import Dense, Sequential
from keras.utils import np_utils
#10nov2017, 0951ist
from keras.util import to_categorical
#10nov2017, 1357ist
from keras.callbacks import EarlyStopping
from keras.callbacks import History
from imutils import paths
# for saving model params, weights, persisting model across training & predict runs, /
# cumulative, blah blah
import h5py
#read data and data wrangling
#metf_n_data = pd.read_csv("./data/METF_normalized.csv")
#added a row for response
#metf = metformin, n = normalize, r= raw dat, from csv, before doing transpose(T) & other manipulations
metf_n_r_data = pd.read_csv("./data/metf_n_resp.csv")
thiog_n_data = pd.read_csv("./data/THIOG_normalized.csv")
#to get transposed data(swap rows & cols), call .T
metf_n_r_t_data = metf_n_r_data.T
#drop the target column, and save rest of the sample data, as
predictors = metf_n_r_t_data.drop(['response'], axis=1).as_matrix()
target = to_categorical(metf_n_r_t_data.response)
#build the model
#todo : in v1, explore more complex models other than sequential
model = Sequential()
#input layer
# pick top 50/100 features, not all 1600
model.add(Dense(100, activation='relu'))
#2 hidden layers
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
#output layer, 2 classes, softmax activation function
model.add(Dense(2,activation='softmax'))
#compile the model
#since our usecase is classification (0===nr, 1===responder)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])
#fit the model
early_stopping_monitor = EarlyStopping(patience=5)
#comment todo : might have to tune the validation_split param, esply till we get more training data
model.fit(predictors, target, validation_split=0.1, epochs=100, callbacks=[history, early_stopping_monitor])
#v1 & later optimization code fragments below :
#===================================================================================
#added by ashish, 10nov2017, 1613ist
#from https://keunwoochoi.wordpress.com/2016/07/16/keras-callbacks/
'''
class My_Callback(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
return
def on_train_end(self, logs={}):
return
def on_epoch_begin(self, logs={}):
return
def on_epoch_end(self, epoch, logs={}):
return
def on_batch_begin(self, batch, logs={}):
return
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
return
'''
#added by ashish, 10nov2017, 1257ist
'''
def get_new_model(input_shape = input_shape):
model = Sequential()
#input layer,pick top 50/100 features, not all 1600
model.add(Dense(100, activation='relu'))
#2 hidden layers
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
#output layer, 2 classes, softmax activation function
model.add(Dense(2,activation='softmax'))
return model
'''
'''
model.add(Dense(units=1000, input_dim=100))
model.add(Activation('relu'))
#add hidden layer1
model.add(Dense(units=10))
model.add(Activation('softmax'))
#add hidden layer2
model.add(Dense(units=10))
model.add(Activation('softmax'))
'''
#model.compile(loss=keras.losses.categorical_crossentropy,
# optimizer=keras.optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True))
#for loop, over lr(learning rate), to generate models
'''
lr_to_test = [0.000001, 0.01,1]
for lr in lr_to_test:
model = get_new_model()
my_optimizer = SGD(lr=lr)
model.compile(optimizer=my_optimizer, loss='categorical_crossentropy')
model.fit(predictors,target)
'''
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
#model.fit(x_train, y_train, epochs=5, batch_size=32)
#model.train_on_batch(x_batch, y_batch)
#loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
#classes = model.predict(x_test, batch_size=128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment