print mean_absolute_error(np.array(Y_val), pred) return net ################################################################################ ################################################################################ exp_type = 'natural' movie_type = 'movie' spatial_downsample_factor = 4 # time_downsample_factor = 5 n_lag = 6 split_type = 'even' saved_nns_dir = './temp/nets' responses = load_responses(exp_type) movies = load_movies(exp_type, movie_type, downsample_factor=spatial_downsample_factor) train_test_splits = map(lambda r: train_test_split(r, movies, split_type, train_frac=0.7, to_leave_out=0), responses) nns = [] for i, response in enumerate(responses): name = response.name print 'Mouse %s' % name print 'Splitting out training and test data...' tr_rsp, val_rsp, tr_mov, val_mov = train_test_splits[i] tr_rsp = smooth_responses(tr_rsp) val_rsp = smooth_responses(val_rsp)
def fit_mlp_models(n_hidden=25, learning_rate=0.15, n_epochs=100, batch_size=600, L1_reg=0.0e-9, L2_reg=0.0e-9): exp_type = 'natural' movie_type = 'movie' spatial_downsample_factor = 4 n_lag = 13 saved_models_dir = './temp/mlp-models-%d' % n_hidden predicted_responses_dir = './temp/mlp-predicted-responses-%s' % n_hidden if not os.path.isdir(saved_models_dir): os.makedirs(saved_models_dir) else: shutil.rmtree(saved_models_dir) os.makedirs(saved_models_dir) if not os.path.isdir(predicted_responses_dir): os.makedirs(predicted_responses_dir) else: shutil.rmtree(predicted_responses_dir) os.makedirs(predicted_responses_dir) print '%d hidden layer neurons, %d epochs to train for' % (n_hidden, n_epochs) responses = load_responses(exp_type) movies = load_movies(exp_type, movie_type, downsample_factor=spatial_downsample_factor) mlp_training_errors = [] for i, response in enumerate(responses): name = response.name print 'Mouse %s' % name print 'Splitting out training and test data...' tr_rsp, tst_rsp, tr_mov, tst_mov = train_test_split(response, movies, 'even', train_frac=0.8) print 'Splitting out training and validation data...' tr_rsp, val_rsp, tr_mov, val_mov = train_test_split(tr_rsp, tr_mov, 'even', train_frac=0.9) tr_rsp = smooth_responses(tr_rsp) val_rsp = smooth_responses(val_rsp) tst_rsp = smooth_responses(tst_rsp) train_set_x, train_set_y = window_matrices(tr_rsp, tr_mov, n_lag) valid_set_x, valid_set_y = window_matrices(val_rsp, val_mov, n_lag) test_set_x, test_set_y = window_matrices(tst_rsp, tst_mov, n_lag) model = RegressionModel(model_name=name, n_hidden=n_hidden, learning_rate=learning_rate, n_epochs=n_epochs, batch_size=batch_size, L1_reg=L1_reg, L2_reg=L2_reg) model.setup_with_data([(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]) test_error = model.train() mlp_training_errors.append(test_error) predicted = model.y_pred() np.save(os.path.join(predicted_responses_dir, 'pred_%s' % name), predicted) with open(os.path.join(saved_models_dir, 'mlp_%s' % name), 'wb') as f: pickle.dump(model.regression.params, f) with open(os.path.join(saved_models_dir, 'train_errors'), 'wb') as f: pickle.dump(mlp_training_errors, f)
def fit_mlp_models(n_hidden=25, learning_rate=0.15, n_epochs=100, batch_size=600, L1_reg=0.0e-9, L2_reg = 0.0e-9): exp_type = 'natural' movie_type = 'movie' spatial_downsample_factor = 4 n_lag = 13 saved_models_dir = './temp/mlp-models-%d' % n_hidden predicted_responses_dir = './temp/mlp-predicted-responses-%s' % n_hidden if not os.path.isdir(saved_models_dir): os.makedirs(saved_models_dir) else: shutil.rmtree(saved_models_dir) os.makedirs(saved_models_dir) if not os.path.isdir(predicted_responses_dir): os.makedirs(predicted_responses_dir) else: shutil.rmtree(predicted_responses_dir) os.makedirs(predicted_responses_dir) print '%d hidden layer neurons, %d epochs to train for'%(n_hidden,n_epochs) responses = load_responses(exp_type) movies = load_movies(exp_type, movie_type, downsample_factor=spatial_downsample_factor) mlp_training_errors = [] for i, response in enumerate(responses): name = response.name print 'Mouse %s' % name print 'Splitting out training and test data...' tr_rsp, tst_rsp, tr_mov, tst_mov = train_test_split(response, movies, 'even', train_frac=0.8) print 'Splitting out training and validation data...' tr_rsp, val_rsp, tr_mov, val_mov = train_test_split(tr_rsp, tr_mov, 'even', train_frac=0.9) tr_rsp = smooth_responses(tr_rsp) val_rsp = smooth_responses(val_rsp) tst_rsp = smooth_responses(tst_rsp) train_set_x, train_set_y = window_matrices(tr_rsp, tr_mov, n_lag) valid_set_x, valid_set_y = window_matrices(val_rsp, val_mov, n_lag) test_set_x, test_set_y = window_matrices(tst_rsp, tst_mov, n_lag) model = RegressionModel(model_name=name, n_hidden=n_hidden, learning_rate=learning_rate, n_epochs=n_epochs, batch_size=batch_size, L1_reg=L1_reg, L2_reg=L2_reg) model.setup_with_data([(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]) test_error = model.train() mlp_training_errors.append(test_error) predicted = model.y_pred() np.save(os.path.join(predicted_responses_dir, 'pred_%s' % name), predicted) with open(os.path.join(saved_models_dir,'mlp_%s' % name), 'wb') as f: pickle.dump(model.regression.params, f) with open(os.path.join(saved_models_dir, 'train_errors'), 'wb') as f: pickle.dump(mlp_training_errors, f)
elif exp_type == 'grating': from src.params.grating.datafile_params import PLOTS_DIR from src.params.grating.stimulus_params import CA_SAMPLING_RATE movie_type = 'movie' downsample_factor = 8 n_lag = 13 n_clusters = 4 n_components = 16 split_type = 'loo' model_name = 'linear-regression' model_type = 'reverse' regularisation = None responses = map(smooth_responses, load_responses(exp_type)) movies = load_movies(exp_type, movie_type, downsample_factor=downsample_factor) for i, response in enumerate(responses): if i > 0: break name = response.name print 'Mouse %s' % name print 'Splitting out training and test data...' tr_rsp, te_rsp, tr_mov, te_mov = train_test_split(response, movies, split_type, train_frac=0.7, to_leave_out=0) print 'Fitting model parameters for %s %s...' % (model_type, model_name)
return net ################################################################################ ################################################################################ exp_type = 'natural' movie_type = 'movie' spatial_downsample_factor = 4 # time_downsample_factor = 5 n_lag = 6 split_type = 'even' saved_nns_dir = './temp/nets' responses = load_responses(exp_type) movies = load_movies(exp_type, movie_type, downsample_factor=spatial_downsample_factor) train_test_splits = map( lambda r: train_test_split( r, movies, split_type, train_frac=0.7, to_leave_out=0), responses) nns = [] for i, response in enumerate(responses): name = response.name print 'Mouse %s' % name print 'Splitting out training and test data...' tr_rsp, val_rsp, tr_mov, val_mov = train_test_splits[i] tr_rsp = smooth_responses(tr_rsp) val_rsp = smooth_responses(val_rsp)