def get_param_names_svh(num_inputs, layer_sizes, num_outputs, weight_shapes, granularity=None, n_stoc_var=0): """ writes list of param_names for weight array which corresponds to (row-wise) weight matrices and bias vector w and b, as well as prior stochastic hyperparameters h (which are the standard deviation / scale parameters of the prior distributions of the neural network parameters) and likelihood variance stochastic parameters v (which are the variance parameters of the likelihood function). """ weight_shapes = tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) param_names = [] i = 0 if granularity == 'single': param_names.append(('p1', 'h_{1}')) i += 1 elif granularity == 'layer': for _ in range(len(weight_shapes) / 2): param_names.append(('p%i' % (i + 1), 'h_{%i}' % (i + 1))) i += 1 elif granularity == 'input_size': for w in range(len(weight_shapes) / 2): for n in range(weight_shape[2 * w][0] + 1): #+1 is for bias hyperparam term param_names.append( ('p%i' % (i + 1), 'h_{%i, %i}' % (w + 1, n + 1))) i += 1 else: #no stochastic prior hyperparams pass if n_stoc_var: for j in range(n_stoc_var): param_names.append(('p%i' % (i + 1), 'v_{%i}' % (j + 1))) i += 1 else: #no stochastic lhood variance parameters pass for j in range(len(weight_shapes) / 2): weight_row = weight_shapes[2 * j][0] weight_col = weight_shapes[2 * j][1] bias = weight_shapes[2 * j + 1][0] for k in range(weight_row): for l in range(weight_col): param_names.append( ('p%i' % (i + 1), 'w_{%i,%i,%i}' % (j + 1, k + 1, l + 1))) i += 1 for k in range(bias): param_names.append(('p%i' % (i + 1), 'b_{%i,%i}' % (j + 1, k + 1))) i += 1 return param_names
def get_param_names(num_inputs, layer_sizes, num_outputs): """ writes list of param_names for weight array which corresponds to (row-wise) weight matrices and bias vector w and b respectively. Length of weight_shapes should be even """ weight_shapes = tools.get_weight_shapes(num_inputs, layer_sizes, num_outputs) param_names = [] i = 0 for j in range(len(weight_shapes) / 2): weight_row = weight_shapes[2 * j][0] weight_col = weight_shapes[2 * j][1] bias = weight_shapes[2 * j + 1][0] for k in range(weight_row): for l in range(weight_col): param_names.append( ('p%i' % (i + 1), 'w_{%i,%i,%i}' % (j + 1, k + 1, l + 1))) i += 1 for k in range(bias): param_names.append(('p%i' % (i + 1), 'b_{%i,%i}' % (j + 1, k + 1))) i += 1 return param_names
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_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])
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])
make pred .paramnames file from scratch. 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)