def run_MLP(params, trows, vrows): config = config_reader.read_config(utils.abs_path_of("config/default.ini")) if not os.path.isdir(config.get_rel_path("PATHS", "checkpoint_dir")): utils.mkdir_recursive(config.get_rel_path("PATHS", "checkpoint_dir")) iris_runner = mlp.FCNRunner(config, params) # trows, vrows, test_rows, config) iris_runner.bind_training_dataqueue_dataframe(trows, params) iris_runner.bind_validation_dataqueue_dataframe(vrows) if "TEST" in config: test_path = config.get_rel_path("TEST", "test_file") with tf.name_scope("test_data"): # TODO check this with Yanli # test_rows = csv_reader.read_test_csv_dataframe(test_path, int(config["TEST"]["batch_size"])) test_rows = csv_reader.read_csv_dataframe(test_path) iris_runner.bind_test_dataqueue_dataframe(test_rows) iris_runner.initialize() if "TRAINING" in config: valid_acc, train_loss, train_auc, valid_loss = iris_runner.run_training_dataframe( trows, vrows) if "TEST" in config: iris_runner.run_test(test_rows) iris_runner.close_session() return 1 - valid_acc, train_loss, train_auc, valid_loss
def objective(args): config = config_reader.read_config(utils.abs_path_of("config/default.ini")) params = {} params['l1_reg'] = args['l1_reg'] params['l2_reg'] = args['l2_reg'] params['num_layers'] = int(args['num_layers']) params['layer_size'] = int(args['layer_size']) params['learning_rate'] = args['learning_rate'] params['batch_size'] = args['batch_size'] params['dropout_keep_probability'] = args['dropout_keep_probability'] params['validation_window'] = args['validation_window'] trows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "training_file")) vrows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "validation_file")) with open(config.get_rel_path("PATHS", "training_file")) as f: temporary_reader = csv.reader(f, delimiter=',') total_columns = len(next(temporary_reader)) params['total_columns'] = total_columns with tf.Graph().as_default(): loss = run_MLP(params, trows, vrows) return loss
def optimize(): config = config_reader.read_config(utils.abs_path_of("config/default.ini")) space = { 'learning_rate': hyperopt.hp.choice('learning_rate', [0.0001, 0.001]), 'rnn_num_layers': hyperopt.hp.choice('rnn_num_layers', [2, 3, 4]), 'rnn_num_hidden': hyperopt.hp.choice('rnn_num_hidden', [200, 300, 400]), 'rnn_batch_size': hyperopt.hp.choice('rnn_batch_size', [50, 100, 200]), 'dropout_keep_probability': hyperopt.hp.choice('dropout_keep_probability', [0.5, 0.4, 0.6]), 'validation_window': hyperopt.hp.choice('validation_window', [5]) } best_model = hyperopt.fmin(objective, space, algo=hyperopt.tpe.suggest, max_evals=200) print(best_model) print(hyperopt.space_eval(space, best_model))
def get_rel_path(self, *args, **kwargs): raw_get = self.get(*args, **kwargs) if not raw_get: return "" if raw_get.startswith('/'): return raw_get return utils.abs_path_of(raw_get)
def main(): global trows, vrows config = config_reader.read_config(utils.abs_path_of("config/default.ini")) trows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "training_file")) vrows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "validation_file")) while True: optimize()
def run_RNN(params, trainX, trainY, ValidX, ValidY): config = config_reader.read_config(utils.abs_path_of("config/default.ini")) training_iters = config.getint("RNN_SEQUENCE_CLASSIFICATION", "rnn_training_iters") validation_interval = config.getint("PROCESS", "validation_interval") #train_batchsize = config.getint("RNN_SEQUENCE_CLASSIFICATION", "rnn_batch_size") #keep_prob = config.getfloat("TRAINING", "dropout_keep_probability", fallback=1.0) train_batchsize = params['rnn_batch_size'] keep_prob = params['dropout_keep_probability'] validation_window = params['validation_window'] train_len, rows, row_size = trainX.shape batches = batch_iter( zip(trainX, trainY), train_batchsize, training_iters) _, rows, row_size = trainX.shape num_classes = trainY.shape[1] with tf.name_scope('input'): data = tf.placeholder(tf.float32, [None, rows, row_size]) target = tf.placeholder(tf.float32, [None, num_classes]) dropout = tf.placeholder(tf.float32) model = SequenceClassification(data, target, dropout, params) sess = tf.Session() sess.run(tf.global_variables_initializer()) for i,batch in enumerate(batches): batch_x, batch_y = zip(*batch) _, train_accuracy = sess.run([model.optimize, model.accuracy], {data: batch_x, target: batch_y, dropout: keep_prob}) print('Batch {:2d} Train_accuracy {:3.1f}%'.format(i + 1, 100 * train_accuracy)) val_loss = [] v_count = 0 if (i+1) % validation_interval == 0: accuracy, summary_test = sess.run([model.accuracy, model.summary_op_test], {data: ValidX, target: ValidY, dropout: 1}) print("********************************************") print('Validation_accuracy {:3.1f}%'.format(100 * accuracy)) print("********************************************") loss = -1*accuracy val_loss.append(loss) v_count += 1 if v_count > validation_window: Validation_Loss = np.mean(val_loss[-validation_window:]) else: Validation_Loss = np.mean(val_loss) return Validation_Loss #if __name__ == '__main__': #main()
def main(): global trows, vrows, total_columns config = config_reader.read_config(utils.abs_path_of("config/default.ini")) with open(config.get_rel_path("PATHS", "training_file")) as f: temporary_reader = csv.reader(f, delimiter=',') total_columns = len(next(temporary_reader)) trows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "training_file")) vrows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "validation_file")) while True: optimize()
def run_MLP(params): config = config_reader.read_config(utils.abs_path_of("config/default.ini")) if not os.path.isdir(config.get_rel_path("PATHS", "checkpoint_dir")): utils.mkdir_recursive(config.get_rel_path("PATHS", "checkpoint_dir")) iris_runner = mlp.FCNRunner(config, params) # trows, vrows, test_rows, config) if "TRAINING" in config: with tf.name_scope("train_data"): #train_batch_size = config.getint("TRAINING", "batch_size") train_batch_size = params['batch_size'] stratified_task = config.get("TRAINING", "stratified_sampling", fallback="") trows = csv_reader.read_csv( config.get_rel_path("PATHS", "training_file"), train_batch_size, stratified_task, config) with tf.name_scope("validation_data"): vrows = csv_reader.read_csv( config.get_rel_path("PATHS", "validation_file"), config.getint("TRAINING", "validation_batch_size")) iris_runner.bind_training_dataqueue(trows, params) iris_runner.bind_validation_dataqueue(vrows) ''' if "TEST" in config: test_path = config.get_rel_path("TEST","test_file") with tf.name_scope("test_data"): test_rows = csv_reader.read_test_csv(test_path, int(config["TEST"]["batch_size"])) iris_runner.bind_test_dataqueue(test_rows) ''' iris_runner.initialize() if "TRAINING" in config: valid_loss = iris_runner.run_training() #if "TEST" in config: #iris_runner.run_test() return valid_loss
def objective(args): config = config_reader.read_config(utils.abs_path_of("config/default.ini")) params = {} params['l1_reg'] = args['l1_reg'] params['l2_reg'] = args['l2_reg'] params['num_layers'] = args['num_layers'] params['layer_size'] = args['layer_size'] params['learning_rate'] = args['learning_rate'] params['batch_size'] = args['batch_size'] params['dropout_keep_probability'] = args['dropout_keep_probability'] params['validation_window'] = args['validation_window'] trows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "training_file")) vrows = csv_reader.read_csv_dataframe( config.get_rel_path("PATHS", "validation_file")) with tf.Graph().as_default(): loss = run_MLP(params, trows, vrows) return loss
import os import tensorflow as tf import config_reader import csv_reader import mlp import utils import sys, os config = config_reader.read_config(utils.abs_path_of("config/default.ini")) if not os.path.isdir(config.get_rel_path("PATHS", "checkpoint_dir")): utils.mkdir_recursive(config.get_rel_path("PATHS", "checkpoint_dir")) iris_runner = mlp.FCNRunner(config) # trows, vrows, test_rows, config) if "TRAINING" in config: with tf.name_scope("train_data"): train_batch_size = config.getint("TRAINING", "batch_size") stratified_task = config.get("TRAINING", "stratified_sampling", fallback="") trows = csv_reader.read_csv( config.get_rel_path("PATHS", "training_file"), train_batch_size, stratified_task, config) with tf.name_scope("validation_data"): vrows = csv_reader.read_csv( config.get_rel_path("PATHS", "validation_file"), config.getint("TRAINING", "validation_batch_size"))