Example #1
0
def get_lm_predictions_gpu(w,
                           b,
                           x,
                           y,
                           projection: np.ndarray = None,
                           device: str = 'cpu'):
    network = define_network(w, b, projection_mat=projection, device=device)
    accuracy = network.eval(x, y)
    return accuracy
Example #2
0
def get_top_k_lm_predictions_gpu(tokenizer,
                                 w,
                                 b,
                                 x,
                                 y,
                                 projection: np.ndarray = None,
                                 k=100,
                                 device: str = 'cpu'):
    network = define_network(w, b, projection_mat=projection, device=device)
    distribution = network.get_probs(x, y)[0]
    top_y = torch.tensor(distribution).to(device).topk(
        k=k, dim=1, largest=True, sorted=True).indices.cpu().numpy()
    top_words = []
    for top_k_per_word in top_y:
        top_k = tokenizer.convert_ids_to_tokens(top_k_per_word)
        top_words.append(top_k)
    return top_words
Example #3
0
    train_dir = arguments['--train_path']
    dev_dir = train_dir.replace('train', 'dev')
    out_dir = arguments['--out_dir']
    input_dim = int(arguments['--input_dim'])

    if arguments['--debias'] == 'none':
        debias = np.eye(input_dim)
    else:
        debias = np.load(arguments['--debias'])

    train_vecs, train_labels = load_data(train_dir)
    dev_vecs, dev_labels = load_data(dev_dir)

    x_train, y_train = flatten_tokens(train_vecs, train_labels, tokenizer)
    assert len(x_train) == len(y_train), f"{len(x_train)}, {len(y_train)}"

    x_test, y_test = flatten_tokens(dev_vecs, dev_labels, tokenizer)
    assert len(x_test) == len(y_test), f"{len(x_test)}, {len(y_test)}"

    net = define_network(word_embeddings, bias, debias, arguments['--device'])

    if arguments['--debias'] == 'none':
        print("Debiasing option is turned off.")
        dev_acc = net.eval(x_test, y_test)
        print("Dev accuracy without fine-tuning is: ", dev_acc)

    net.train(x_train, y_train, x_test, y_test,
              epochs=int(arguments['--n_epochs']),
              save_path=f"{out_dir}/debias_ft.pt",
              use_wandb=use_wandb)
Example #4
0
def get_lm_softmax_gpu(w, b, x, y, device: str):
    network = define_network(w, b, device=device)
    distribution = network.get_probs(x, y)
    return distribution