Beispiel #1
0
def get_param_names3(num_inputs, layer_sizes, num_outputs, m_trainable_arr,
                     b_trainable_arr):
    """
	see calc_num_weights3 for relevant of suffix
	probably a way to code this function with less conditionals, but not work
	spending any more time on
	"""
    weight_shapes = tools.get_weight_shapes3(num_inputs, layer_sizes,
                                             num_outputs, m_trainable_arr,
                                             b_trainable_arr)
    param_names = []
    i = 0
    j = 0
    weight_offset = 1
    bias_offset = 1
    if np.all(~np.array(m_trainable_arr)) and np.all(
            ~np.array(b_trainable_arr)):
        print "no trainable parameters"
        return param_names
    for k in range(len(m_trainable_arr)):
        if m_trainable_arr[k] and b_trainable_arr[k]:
            weight_row = weight_shapes[j][0]
            weight_col = weight_shapes[j][1]
            bias = weight_shapes[j + 1][0]
            j += 2
        elif m_trainable_arr[k] and not b_trainable_arr[k]:
            weight_row = weight_shapes[j][0]
            weight_col = weight_shapes[j][1]
            bias = 0
            j += 1
        elif not m_trainable_arr[k] and b_trainable_arr[k]:
            weight_row = 0
            weight_col = 0
            bias = weight_shapes[j][0]
            j += 1
        else:
            weight_row = 0
            weight_col = 0
            bias = 0
        for l in range(weight_row):
            for m in range(weight_col):
                param_names.append(
                    ('p%i' % (i + 1),
                     'w_{%i,%i,%i}' % (k + weight_offset, l + 1, m + 1)))
                i += 1
        for l in range(bias):
            param_names.append(
                ('p%i' % (i + 1), 'b_{%i,%i}' % (k + bias_offset, l + 1)))
            i += 1
    return param_names
Beispiel #2
0
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)
Beispiel #3
0
def get_param_names_svh3(num_inputs,
                         layer_sizes,
                         num_outputs,
                         m_trainable_arr,
                         b_trainable_arr,
                         granularity=None,
                         n_stoc_var=0):
    """
	NOT TESTED
	see calc_num_weights3 for relevant of suffix
	"""
    weight_shapes = tools.get_weight_shapes3(num_inputs, layer_sizes,
                                             num_outputs, m_trainable_arr,
                                             b_trainable_arr)
    param_names = []
    i = 0
    j = 0
    weight_offset = 1
    bias_offset = 1
    if np.all(~np.array(m_trainable_arr)) and np.all(
            ~np.array(b_trainable_arr)):
        print "no trainable parameters"
        return param_names
    if granularity == 'single':
        param_names.append(('p1', 'h_{1}'))
        i += 1
    elif granularity == 'layer':
        for k in range(len(m_trainable_arr)):
            if m_trainable_arr[k] or b_trainable_arr[k]:
                param_names.append(('p%i' % (i + 1), 'h_{%i}' % (k + 1)))
                i += 1
    elif granularity == 'input_size':
        t = 0
        w = 0
        for k in range(len(m_trainable_arr)):
            if m_trainable_arr[k] and b_trainable_arr[k]:
                t += weight_shapes[w][0]
                t += 1
                w += 2
            elif m_trainable_arr[k] and not b_trainable_arr[k]:
                t += weight_shapes[w][0]
                w += 1
            elif not m_trainable_arr[k] and b_trainable_arr[k]:
                t += 1
                w += 1
            else:
                pass
            for n in range(t):
                param_names.append(
                    ('p%i' % (i + 1), 'h_{%i, %i}' % (k + 1, n + 1)))
                i += 1
            t = 0
    else:  #no stochastic prior hyperparams
        pass
    if n_stoc_var:
        for k in range(n_stoc_var):
            param_names.append(('p%i' % (i + 1), 'v_{%i}' % (k + 1)))
            i += 1
    else:  #no stochastic lhood variance parameters
        pass
    for k in range(len(m_trainable_arr)):
        if m_trainable_arr[k] and b_trainable_arr[k]:
            weight_row = weight_shapes[j][0]
            weight_col = weight_shapes[j][1]
            bias = weight_shapes[j + 1][0]
            j += 2
        elif m_trainable_arr[k] and not b_trainable_arr[k]:
            weight_row = weight_shapes[j][0]
            weight_col = weight_shapes[j][1]
            bias = 0
            j += 1
        elif not m_trainable_arr[k] and b_trainable_arr[k]:
            weight_row = 0
            weight_col = 0
            bias = weight_shapes[j][0]
            j += 1
        else:
            weight_row = 0
            weight_col = 0
            bias = 0
        for l in range(weight_row):
            for m in range(weight_col):
                param_names.append(
                    ('p%i' % (i + 1),
                     'w_{%i,%i,%i}' % (k + weight_offset, l + 1, m + 1)))
                i += 1
        for l in range(bias):
            param_names.append(
                ('p%i' % (i + 1), 'b_{%i,%i}' % (k + bias_offset, l + 1)))
            i += 1
    return param_names