Exemplo n.º 1
0
    sorted_labels = sorted(list(label_concepts.keys()))

    print()
    print("h = %.3f" % h_val)
    for label in sorted_labels:
        print("%s:" % label)
        for concept in label_concepts[label]:
            print("\t%s" % concept)


data = []
for h_val in np.linspace(MAX_H, MIN_H, num=NUM_HS):
    # Parameters.
    h = torch.tensor([h_val], dtype=rel_freqs.dtype, device='cuda')
    kernel_widths = model.compute_kernel_widths(h)
    priors = model.rel_freqs_to_priors(rel_freqs, uniform=False)

    # Prediction.
    ranks, posts, full = model.predict_production_ranks_and_posteriors(
        prod_sense_dist=dist_matrix,
        identity_prod_indices=[None] * len(full_lexicon),
        kernel_widths=kernel_widths,
        priors=priors,
        full_dist_matrix=None,
        child_prod_indices=None,
        allow_identity_prod=True,
        target_identity_prod=True,
        get_full_matrix=True)

    labels = []
    for i in range(len(concepts)):
def predict(dataset, overextension_indices=overextension_indices,
        allow_identity_prod=False, target_identity_prod=False,
        save_result=False):
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=False)

    ranks = np.zeros((0,))
    posts = np.zeros((0,))

    for batch in dataloader:
            child_prod_indices    = batch['child_prod']
            child_sense_indices   = batch['child_sense']
            identity_prod_indices = batch['identity_prod']

            dist_submatrix = dist_matrix[:, child_sense_indices, :]

            kernel_widths = model.compute_kernel_widths(h)
            priors        = model.rel_freqs_to_priors(
                    rel_freqs, uniform=UNIFORM)

            this_ranks, this_posts = model.predict_production_ranks_and_posteriors(
                    prod_sense_dist       = dist_submatrix,
                    identity_prod_indices = identity_prod_indices,
                    kernel_widths         = kernel_widths,
                    priors                = priors,
                    full_dist_matrix      = dist_matrix,
                    child_prod_indices    = child_prod_indices,
                    allow_identity_prod   = allow_identity_prod,
                    target_identity_prod  = target_identity_prod,
                    baseline              = BASELINE)

            ranks = np.append(ranks, this_ranks.detach().cpu().numpy())
            posts = np.append(posts, this_posts.detach().cpu().numpy())

    pred_result = {
            'ranks' : ranks,
            'posts' : posts,
            }

    if save_result:
        result_pred_filename = "{}_{}_{}{}.pkl".format(
                RESULT_PRED,
                'uniform' if UNIFORM else 'frequency',
                FEATURES_NAME,
                SUFFIX)
        with open(result_pred_filename, 'wb') as f:
            pickle.dump(pred_result, f)

    def prediction_df():
        rows = [
                {
                    'prod'  : prod_vocab.loc[oind['child_prod'], 'wordnet_synset'],
                    'sense' : full_lexicon.loc[oind['child_sense'], 'wordnet_synset'],
                    'rank'  : ranks[i],
                    'post'  : posts[i],
                    }
                for i, oind in enumerate(overextension_indices)
                ]
        df = pd.DataFrame(rows)
        return df[['prod', 'sense', 'post', 'rank']]

    df = prediction_df()
    print(df['rank'].median())
    return df
        item_wordnet_synsets.append(wordnet)
        item_vocab_indices.append(prod_vocab_lookup[wordnet, imagenet])
        item_lexicon_indices.append(full_lexicon_lookup[wordnet, imagenet])
        item_group.append(group)

items = pd.DataFrame({
    'wordnet_synset': item_wordnet_synsets,
    'vocab_index': item_vocab_indices,
    'lexicon_index': item_lexicon_indices,
    'group': item_group,
})

# Parameters.
kernel_widths = model.compute_kernel_widths(h)
priors = model.rel_freqs_to_priors(rel_freqs, uniform=UNIFORM)

# Results.
data = []

# Production.
dist_submatrix = dist_matrix[:, item_lexicon_indices, :]

prod_ranks, prod_posts, full_post = (
    model.predict_production_ranks_and_posteriors(
        prod_sense_dist=dist_submatrix,
        identity_prod_indices=item_vocab_indices,
        kernel_widths=kernel_widths,
        priors=priors,
        full_dist_matrix=dist_matrix,
        child_prod_indices=item_vocab_indices,
def train(dataset, save=True):
    opt = optim.Adam([h], lr = 0.001)

    min_loss = float('inf')
    tol = 1e-6
    patience = 30
    patience_counter = 0
    epoch = 0

    dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=True)

    while patience_counter < patience:
        if BASELINE:
            break

        if MULTIPLE_WIDTHS:
            print("Epoch %d, min loss %.6f" % (epoch+1, min_loss))
        else:
            print("Epoch %d, min loss %.6f, h %.4f" % (epoch+1, min_loss, h))
        epoch += 1

        total_loss = 0

        for batch in dataloader:
            opt.zero_grad()

            child_prod_indices    = batch['child_prod']
            child_sense_indices   = batch['child_sense']
            identity_prod_indices = batch['identity_prod']

            dist_submatrix = dist_matrix[:, child_sense_indices, :]

            kernel_widths = model.compute_kernel_widths(h)
            priors        = model.rel_freqs_to_priors(
                    rel_freqs, uniform=UNIFORM)

            nll = model.production_nll(
                    prod_sense_dist       = dist_submatrix,
                    child_prod_indices    = child_prod_indices,
                    identity_prod_indices = identity_prod_indices,
                    kernel_widths         = kernel_widths,
                    priors                = priors,
                    full_dist_matrix      = dist_matrix)

            nll.backward()
            opt.step()

            total_loss += float(nll.detach().cpu().numpy())

        if total_loss < min_loss:
            min_loss = total_loss
            patience_counter = 0
        else:
            patience_counter += 1

    # Save training result.
    if save:
        train_result = {'h' : h.detach().cpu().numpy()}

        result_train_filename = "{}_{}_{}{}.pkl".format(
                RESULT_TRAIN,
                'uniform' if UNIFORM else 'frequency',
                FEATURES_NAME,
                SUFFIX)

        with open(result_train_filename, 'wb') as f:
            pickle.dump(train_result, f)