def main(run_string): ###### load training data data = 'bh_50' data_suffix = '_tr_1.csv' data_dir = '../../data/uci/' data_prefix = data_dir + data x_tr, y_tr = input_tools.get_x_y_tr_data(data_prefix, data_suffix) x_tr = np.genfromtxt('../../data/linear_input_data.txt', delimiter=',') y_tr = x_tr batch_size = x_tr.shape[0] ###### get weight information weights_dir = '../../data/' #for forward test a1_size = 0 num_inputs = tools.get_num_inputs(x_tr) num_outputs = tools.get_num_outputs(y_tr) layer_sizes = [1, num_inputs] * 2 m_trainable_arr = [True, True] * 2 + [False] b_trainable_arr = [True, True] * 2 + [False] num_weights = tools.calc_num_weights3(num_inputs, layer_sizes, num_outputs, m_trainable_arr, b_trainable_arr) ###### check shapes of training data x_tr, y_tr = tools.reshape_x_y_twod(x_tr, y_tr) ###### setup prior hyper_type = "deterministic" # "stochastic" or "deterministic" var_type = "deterministic" # "stochastic" or "deterministic" weight_shapes = tools.get_weight_shapes3(num_inputs, layer_sizes, num_outputs, m_trainable_arr, b_trainable_arr) dependence_lengths = tools.get_degen_dependence_lengths(weight_shapes, independent=True) if hyper_type == "deterministic" and var_type == "deterministic": prior_types = [4] prior_hyperparams = [[0., 1.]] param_prior_types = [0] prior = inverse_priors.inverse_prior(prior_types, prior_hyperparams, dependence_lengths, param_prior_types, num_weights) n_stoc = 0 n_stoc_var = 0 elif hyper_type == "stochastic" and var_type == "deterministic": granularity = 'single' hyper_dependence_lengths = tools.get_hyper_dependence_lengths( weight_shapes, granularity) hyperprior_types = [9] prior_types = [4] hyperprior_params = [[1. / 2., 1. / (2. * 100)]] prior_hyperparams = [0.] param_hyperprior_types = [0] param_prior_types = [0] n_stoc = len(hyper_dependence_lengths) prior = isp.inverse_stoc_hyper_prior( hyperprior_types, prior_types, hyperprior_params, prior_hyperparams, hyper_dependence_lengths, dependence_lengths, param_hyperprior_types, param_prior_types, n_stoc, num_weights) n_stoc_var = 0 elif hyper_type == "stochastic" and var_type == "stochastic": granularity = 'single' hyper_dependence_lengths = tools.get_hyper_dependence_lengths( weight_shapes, granularity) var_dependence_lengths = [1] n_stoc_var = len(var_dependence_lengths) hyperprior_types = [9] var_prior_types = [10] prior_types = [4] hyperprior_params = [[1. / 2., 1. / (2. * 100)]] var_prior_params = [[1. / 2., 1. / (2. * 100)]] prior_hyperparams = [0.] param_hyperprior_types = [0] var_param_prior_types = [0] param_prior_types = [0] n_stoc = len(hyper_dependence_lengths) prior = isvp.inverse_stoc_var_hyper_prior( hyperprior_types, var_prior_types, prior_types, hyperprior_params, var_prior_params, prior_hyperparams, hyper_dependence_lengths, var_dependence_lengths, dependence_lengths, param_hyperprior_types, var_param_prior_types, param_prior_types, n_stoc, n_stoc_var, num_weights) ###### test prior output from nn setup if "nn_prior_test" in run_string: prior_tests.nn_prior_test(prior, n_stoc + n_stoc_var + num_weights) #set up np model np_nn = npms.mlp_ResNet_2 npm = np_model(np_nn, x_tr, y_tr, batch_size, layer_sizes, m_trainable_arr, b_trainable_arr, n_stoc_var) ll_type = 'gauss' # 'gauss', 'av_gauss', 'categorical_crossentropy', 'av_categorical_crossentropy' npm.setup_LL(ll_type) ###### test llhood output if "forward_test_linear" in run_string: forward_tests.forward_test_linear([npm], num_weights + n_stoc_var, weights_dir) ###### setup polychord nDerived = 0 settings = PyPolyChord.settings.PolyChordSettings( n_stoc + n_stoc_var + num_weights, nDerived) settings.base_dir = './np_chains/' settings.file_root = data + "_slp_sh_sv_sm" settings.nlive = 1000 ###### run polychord if "polychord1" in run_string: PyPolyChord.run_polychord(npm, n_stoc, n_stoc_var, num_weights, nDerived, settings, prior, polychord_tools.dumper)
def inverse_stoc_hyper_priors_test14(): """ real nn arch with 16 nn params, two hyperparams, input_size granularity, degen dependent params """ num_inputs = 1 layer_sizes = [3, 2] num_outputs = 1 print "num weights" n_dims = tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) print tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) weight_shapes = tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) print "weight shapes" print tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) granularity = 'input_size' hyper_dependence_lengths = tools.get_hyper_dependence_lengths( weight_shapes, granularity) n_stoc = len(hyper_dependence_lengths) print "n_stoc" print n_stoc print "granularity" print tools.get_hyper_dependence_lengths(weight_shapes, granularity) print "number of weights per layer" print tools.calc_num_weights_layers(weight_shapes) dependence_lengths = tools.get_degen_dependence_lengths(weight_shapes) print "degen dependence lengths" print tools.get_degen_dependence_lengths(weight_shapes) hyperprior_types = [9, 7] prior_types = [4, 5] hyperprior_params = [[1., 2.], [2., 0.]] prior_hyperparams = [0., 1.] param_hyperprior_types = [0, 1, 0, 0, 1, 1, 1, 0, 0] param_prior_types = [0, 1, 0, 1, 0, 0, 1, 1, 0] prior = isp.inverse_stoc_hyper_prior(hyperprior_types, prior_types, hyperprior_params, prior_hyperparams, hyper_dependence_lengths, dependence_lengths, param_hyperprior_types, param_prior_types, n_stoc, n_dims) p = np.array([ 0.1, 0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.2, 0.1, 0.5, 0.7, 0.8, 0.9, 0.4, 0.2, 0.1, 0.5, 0.9, 0.2, 0.7, 0.4, 0.9, 0.3, 0.5, 0.6, 0.8 ]) prior(p) u = [0., 0., 0., 1., 1., 1., 0., 0., 1., 1., 0., 0., 0., 0., 1., 1., 0.] v = [ 4.35688457, 4.35688457, 4.35688457, 2., 2., 2., 1.47740087, 1.47740087, 1.28886271, 1.28886271, 2., 2., 2., 2., 2., 2.9938003, 4.35688457 ] print isp.gaussian_prior()(p[9], u[0], v[0]) print isp.gaussian_prior()(p[10], u[1], v[1]) print isp.gaussian_prior()(p[11], u[2], v[2]) print isp.laplace_prior()(p[12], u[3], v[3]) print isp.laplace_prior()(p[13], u[4], v[4]) print isp.laplace_prior()(p[14], u[5], v[5]) print isp.gaussian_prior()(p[15], u[6], v[6]) print isp.gaussian_prior()(p[16], u[7], v[7]) print isp.laplace_prior()(p[17], u[8], v[8]) print isp.laplace_prior()(p[18], u[9], v[9]) print isp.gaussian_prior()(p[19], u[10], v[10]) print isp.gaussian_prior()(p[20], u[11], v[11]) print isp.gaussian_prior()(p[21], u[12], v[12]) print isp.gaussian_prior()(p[22], u[13], v[13]) print isp.laplace_prior()(p[23], u[14], v[14]) print isp.laplace_prior()(p[24], u[15], v[15]) print isp.gaussian_prior()(p[25], u[16], v[16])
def inverse_stoc_hyper_priors_test7(): """ real nn arch with 16 nn params, one hyperparam, single granularity, degen dependent params (2 priors) """ num_inputs = 1 layer_sizes = [3, 2] num_outputs = 1 print "num weights" n_dims = tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) print tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) weight_shapes = tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) print "weight shapes" print tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) granularity = 'single' hyper_dependence_lengths = tools.get_hyper_dependence_lengths( weight_shapes, granularity) n_stoc = len(hyper_dependence_lengths) print "number of weights per layer" print tools.calc_num_weights_layers(weight_shapes) dependence_lengths = tools.get_degen_dependence_lengths(weight_shapes) print "degen dependence lengths" print tools.get_degen_dependence_lengths(weight_shapes) hyperprior_types = [9] prior_types = [4, 5] hyperprior_params = [[1., 2.]] prior_hyperparams = [0., 1.] param_hyperprior_types = [0] param_prior_types = [0, 1, 0, 1, 0, 0, 1, 1, 0] prior = isp.inverse_stoc_hyper_prior(hyperprior_types, prior_types, hyperprior_params, prior_hyperparams, hyper_dependence_lengths, dependence_lengths, param_hyperprior_types, param_prior_types, n_stoc, n_dims) p = np.array([ 0.1, 0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.2, 0.1, 0.5, 0.7, 0.8, 0.9, 0.4, 0.2, 0.1, 0.5, 0.9 ]) prior(p) u = [0., 0., 0., 1., 1., 1., 0., 0., 1., 1., 0., 0., 0., 0., 1., 1., 0.] v = [ 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457, 4.35688457 ] print isp.gaussian_prior()(p[1], u[0], v[0]) print isp.gaussian_prior()(p[2], u[1], v[1]) print isp.gaussian_prior()(p[3], u[2], v[2]) print isp.laplace_prior()(p[4], u[3], v[3]) print isp.laplace_prior()(p[5], u[4], v[4]) print isp.laplace_prior()(p[6], u[5], v[5]) print isp.gaussian_prior()(p[7], u[6], v[6]) print isp.gaussian_prior()(p[8], u[7], v[7]) print isp.laplace_prior()(p[9], u[8], v[8]) print isp.laplace_prior()(p[10], u[9], v[9]) print isp.gaussian_prior()(p[11], u[10], v[10]) print isp.gaussian_prior()(p[12], u[11], v[11]) print isp.gaussian_prior()(p[13], u[12], v[12]) print isp.gaussian_prior()(p[14], u[13], v[13]) print isp.laplace_prior()(p[15], u[14], v[14]) print isp.laplace_prior()(p[16], u[15], v[15]) print isp.gaussian_prior()(p[17], u[16], v[16])
def inverse_stoc_hyper_priors_test9(): """ real nn arch with 16 nn params, two hyperparams, layer granularity, independent params """ num_inputs = 1 layer_sizes = [3, 2] num_outputs = 1 print "num weights" n_dims = tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) print tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) weight_shapes = tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) print "weight shapes" print tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) granularity = 'layer' hyper_dependence_lengths = tools.get_hyper_dependence_lengths( weight_shapes, granularity) n_stoc = len(hyper_dependence_lengths) print "number of weights per layer" print tools.calc_num_weights_layers(weight_shapes) dependence_lengths = tools.get_degen_dependence_lengths(weight_shapes, independent=True) print "degen dependence lengths" print tools.get_degen_dependence_lengths(weight_shapes, independent=True) hyperprior_types = [9, 7] prior_types = [4] hyperprior_params = [[1., 2.], [2., 0.]] prior_hyperparams = [0.] param_hyperprior_types = [1, 0, 1] param_prior_types = [0] prior = isp.inverse_stoc_hyper_prior(hyperprior_types, prior_types, hyperprior_params, prior_hyperparams, hyper_dependence_lengths, dependence_lengths, param_hyperprior_types, param_prior_types, n_stoc, n_dims) p = np.array([ 0.1, 0.5, 0.6, 0.7, 0.8, 0.9, 0.4, 0.2, 0.1, 0.5, 0.7, 0.8, 0.9, 0.4, 0.2, 0.1, 0.5, 0.9, 0.2, 0.7 ]) prior(p) u = [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.] v = [ 2., 2., 2., 2., 2., 2., 1.6986436, 1.6986436, 1.6986436, 1.6986436, 1.6986436, 1.6986436, 1.6986436, 1.6986436, 2., 2., 2. ] print isp.gaussian_prior()(p[3], u[0], v[0]) print isp.gaussian_prior()(p[4], u[1], v[1]) print isp.gaussian_prior()(p[5], u[2], v[2]) print isp.gaussian_prior()(p[6], u[3], v[3]) print isp.gaussian_prior()(p[7], u[4], v[4]) print isp.gaussian_prior()(p[8], u[5], v[5]) print isp.gaussian_prior()(p[9], u[6], v[6]) print isp.gaussian_prior()(p[10], u[7], v[7]) print isp.gaussian_prior()(p[11], u[8], v[8]) print isp.gaussian_prior()(p[12], u[9], v[9]) print isp.gaussian_prior()(p[13], u[10], v[10]) print isp.gaussian_prior()(p[14], u[11], v[11]) print isp.gaussian_prior()(p[15], u[12], v[12]) print isp.gaussian_prior()(p[16], u[13], v[13]) print isp.gaussian_prior()(p[17], u[14], v[14]) print isp.gaussian_prior()(p[18], u[15], v[15]) print isp.gaussian_prior()(p[19], u[16], v[16])
note file is saved to base_dir/file_root.paramnames """ param_names = get_pred_param_names(num_outputs) param_f = base_dir + file_root + '.paramnames' make_param_names_file(param_names, param_f) if __name__ == '__main__': num_inputs = 1 layer_sizes = [3, 2] num_outputs = 1 print "num weights" n_dims = tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) print tools.calc_num_weights(num_inputs, layer_sizes, num_outputs) weight_shapes = tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) print "weight shapes" print weight_shapes granularity = 'input_size' hyper_dependence_lengths = tools.get_hyper_dependence_lengths( weight_shapes, granularity) n_stoc = len(hyper_dependence_lengths) print "n_stoc" print n_stoc print "granularity" print hyper_dependence_lengths print "degen dependence lengths" print tools.get_degen_dependence_lengths(weight_shapes) print "param names" print get_param_names_sh(num_inputs, layer_sizes, num_outputs, weight_shapes, granularity)