Exemplo n.º 1
0
def generate_rdm_all(nnet,
                     name,
                     rdm_type=analysis.SPEARMAN,
                     save_files=True,
                     title="RDM training combined",
                     from_file=False,
                     delete_blank_states=True,
                     collapse_rdm=True):
    if not from_file:
        if rdm_type != analysis.SPEARMAN:
            raise Exception("not implemented")
        hidden_both, accuracy_totals_both, accuracy_fullseqs_both = test_network_all(
            nnet)
        hidden_ari, accuracy_totals_ari, accuracy_fullseqs_ari = test_network_ari(
            nnet, blanks=True)
        hidden_bev, accuracy_totals_bev, accuracy_fullseqs_bev = test_network_bev(
            nnet, blanks=True)
        print("Both: {0}, {1}".format(accuracy_totals_both,
                                      accuracy_fullseqs_both))
        print("Ari: {0}, {1}".format(accuracy_totals_ari,
                                     accuracy_fullseqs_ari))
        print("Bev: {0}, {1}".format(accuracy_totals_bev,
                                     accuracy_fullseqs_bev))

        hidden = utils.flatten_onelevel(hidden_bev) +\
                 utils.flatten_onelevel(hidden_ari) +\
                 utils.flatten_onelevel(hidden_both)
        rdmatrix = analysis.rdm_euclidian(hidden)

        utils.save_object(name + "rdmat", rdmatrix)
    else:
        rdmatrix = utils.load_object(name + "rdmat")

    return model2.process_matrix(rdmatrix, delete_blank_states)
Exemplo n.º 2
0
def generate_rdm_all_gradient(nnet,
                              name,
                              blanks,
                              rdm_type=analysis.SPEARMAN,
                              save_files=True,
                              title="RDM training combined",
                              from_file=False,
                              delete_blank_states=True):
    if not from_file:
        if rdm_type != analysis.SPEARMAN:
            raise Exception("not implemented")
        hidden_both, accuracy_totals_both, accuracy_fullseqs_both = test_network_all(
            nnet)
        hidden_ari, accuracy_totals_ari, accuracy_fullseqs_ari = test_network_ari(
            nnet, blanks)
        hidden_bev, accuracy_totals_bev, accuracy_fullseqs_bev = test_network_bev(
            nnet, blanks)
        print("Both: {0}, {1}".format(accuracy_totals_both,
                                      accuracy_fullseqs_both))
        print("Ari: {0}, {1}".format(accuracy_totals_ari,
                                     accuracy_fullseqs_ari))
        print("Bev: {0}, {1}".format(accuracy_totals_bev,
                                     accuracy_fullseqs_bev))

        hidden = utils.flatten_onelevel(hidden_bev) +\
                 utils.flatten_onelevel(hidden_ari) +\
                 utils.flatten_onelevel(hidden_both)

        hidden_left = []
        hidden_right = []
        for vector in hidden:
            hidden_left.append(vector[:len(vector) // 2])
            hidden_right.append(vector[len(vector) // 2:])

        # Now cut the hidden layer in two.
        rdmatrix_left = analysis.rdm_spearman(hidden_left)
        rdmatrix_right = analysis.rdm_spearman(hidden_right)
        # save the massive rdm for debug purposes (so that I don't have to generate it all over again everytime).
        utils.save_object(name + "rdmatright", rdmatrix_right)
        utils.save_object(name + "rdmatleft", rdmatrix_left)
    else:
        rdmatrix_left = utils.load_object(name + "rdmatleft")
        rdmatrix_right = utils.load_object(name + "rdmatright")

    rdmatrix_left, labels = model2.process_matrix(rdmatrix_left,
                                                  delete_blank_states)
    rdmatrix_right, _ = model2.process_matrix(rdmatrix_right,
                                              delete_blank_states)

    return rdmatrix_left, rdmatrix_right, labels
Exemplo n.º 3
0
def run_models_with_noise(models, noise):
    goal_list = [
        "g_1_make_coffee", "g_1_make_tea", "g_2_add_grounds", "g_2_add_cream",
        "g_2_add_sugar", "g_2_drink", "g_2_dip_teabag"
    ]
    all_action_targets_str = utils.flatten_onelevel(
        [tce.action_list[goal] for goal in goal_list])
    all_action_targets = [
        utils.str_to_onehot(action, tce.TeaCoffeeData.actions_list)
        for action in all_action_targets_str
    ]

    # Gather actual outputs
    all_outputs = []
    for model in models:
        all_outputs.append(run_model_with_noise(model, noise))
    # Now check which actions and which goals they got correctly
    num_bad_actions = np.zeros(len(all_action_targets))
    for output in all_outputs:
        for i, action in enumerate(output):
            if (all_action_targets[i] != action).any():
                num_bad_actions[i] += 1
            print(all_action_targets_str[i],
                  utils.onehot_to_str(action, tce.TeaCoffeeData.actions_list))

    return num_bad_actions
Exemplo n.º 4
0
def generate_rdm_bev(nnet,
                     name,
                     rdm_type=analysis.SPEARMAN,
                     save_files=True,
                     title="RDM training beverage only"):
    if rdm_type != analysis.SPEARMAN:
        raise Exception("not implemented")
    hidden, accuracy_totals, accuracy_fullseqs = test_network_bev(nnet)
    print(accuracy_totals)
    print(accuracy_fullseqs)
    hidden = utils.flatten_onelevel(hidden)
    rdmatrix = analysis.rdm_spearman(hidden)
    labels = utils.flatten_onelevel(task.label_seqs_bev)
    name = name.replace('.', '_') + '_' + rdm_type
    if save_files:
        np.savetxt(name + "_rdm_mat.txt", rdmatrix, delimiter="\t", fmt='%.2e')
    analysis.plot_rdm(rdmatrix,
                      labels,
                      title + " spearman rho matrix",
                      figsize=30,
                      fontsize=0.6)
    if save_files:
        plt.savefig(name + '_rdm.png', dpi=300, bbox_inches='tight')
    plt.clf()