def run_inference(model, test_loader):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model.eval()
    model.to(device)

    preds = []
    with torch.no_grad():
        for x_ids, x_mask in tqdm(test_loader):
            pred = model((x_ids.to(device), x_mask.to(device)))
            preds.append(pred.cpu())
    preds = torch.cat(preds)
    _, preds = torch.topk(preds, 1, dim=1)
    preds = [map_label(item, True) for item in preds.numpy().flatten()]

    return preds
    df_train, df_val = train_test_split(
        df, test_size=test_size, random_state=123, stratify=df[label_col])

    tokenizer = RobertaTokenizerFast.from_pretrained(
        tokenizer_path, max_len=max_length)
    train_loader, val_loader = get_roberta_dataloaders(df_train, df_val, list_procs,
                                                       text_col, label_col, tokenizer, max_length,
                                                       train_batch_size, val_batch_size, num_workers, pin_memory)

    # prepare class_weigths
    classes_weights = None
    if balanced_classes:
        val_counts = df_train[label_col].value_counts()
        tmp_idx, tmp_vals = np.array(val_counts.index), val_counts.values
        classes_weights = np.sum(tmp_vals)/tmp_vals
        tmp_idx = np.array([map_label(i, False) for i in tmp_idx])
        classes_weights = torch.FloatTensor(classes_weights[np.argsort(tmp_idx)])

    # load model
    dict_model = {}
    with open(model_path) as f:
        exec(f.read(), dict_model)
    model = dict_model["model"]
    optimizer = dict_model["optimizer"]
    scheduler = dict_model.get("scheduler")
    shutil.copy(model_path, os.path.join(output_path, "model_script.py"))
    train_config["model_script"] = os.path.join(output_path, "model_script.py")

    _pretty_print("Training start")
    model, train_metrics, val_metrics = run_training(model, optimizer, scheduler,
                                                     output_path, train_loader,
Exemple #3
0
            tokenizer,
            max_length,
            train_batch_size,
            val_batch_size,
            num_workers,
            pin_memory,
        )

        # prepare class_weigths
        classes_weights = None
        if balanced_classes:
            val_counts = df_train[label_col].value_counts()
            tmp_idx, tmp_vals = np.array(val_counts.index), val_counts.values
            classes_weights = np.sum(tmp_vals) / tmp_vals
            tmp_idx = np.array(
                [map_label(i, False, dict_label) for i in tmp_idx])
            classes_weights = torch.FloatTensor(
                classes_weights[np.argsort(tmp_idx)])

        # load model
        dict_model = {}
        with open(model_path) as f:
            exec(f.read(), dict_model)
        model = dict_model["model"]
        optimizer = dict_model["optimizer"]
        scheduler = dict_model.get("scheduler")

        _pretty_print("Training start")
        output_path_k = os.path.join(output_path, f"fold_{k}")
        model, train_metrics, val_metrics = run_training(
            model,
Exemple #4
0
            dict_label,
            tokenizer,
            max_length,
            train_batch_size,
            val_batch_size,
            num_workers,
            pin_memory,
        )

        # prepare class_weigths
        classes_weights = None
        if balanced_classes:
            val_counts = df_train[label_col].value_counts()
            tmp_idx, tmp_vals = np.array(val_counts.index), val_counts.values
            classes_weights = np.sum(tmp_vals) / tmp_vals
            tmp_idx = np.array([map_label(i, False, dict_label) for i in tmp_idx])
            classes_weights = torch.FloatTensor(classes_weights[np.argsort(tmp_idx)])

        # load model
        dict_model = {}
        with open(model_path) as f:
            exec(f.read(), dict_model)
        model = dict_model["model"]
        optimizer = dict_model["optimizer"]
        scheduler = dict_model.get("scheduler")
        if k == 0:
            shutil.copy(model_path, os.path.join(output_path, "model_script.py"))
            train_config["model_script"] = os.path.join(output_path, "model_script.py")

        _pretty_print("Training start")
        output_path_k = os.path.join(output_path, f"fold_{k}")
Exemple #5
0
            tokenizer,
            max_length,
            train_batch_size,
            val_batch_size,
            num_workers,
            pin_memory,
        )

        # prepare class_weigths
        classes_weights = None
        if balanced_classes:
            val_counts = df_train[label_col].value_counts()
            tmp_idx, tmp_vals = np.array(val_counts.index), val_counts.values
            classes_weights = np.sum(tmp_vals) / tmp_vals
            tmp_idx = np.array(
                [map_label(i, False, dict_label) for i in tmp_idx])
            classes_weights = torch.FloatTensor(
                classes_weights[np.argsort(tmp_idx)])

        # load model
        dict_model = {}
        with open(model_path) as f:
            exec(f.read(), dict_model)
        model = dict_model["model"]
        optimizer = dict_model["optimizer"]
        scheduler = dict_model.get("scheduler")
        if k == 0:
            shutil.copy(model_path, os.path.join(output_path,
                                                 "model_script.py"))
            train_config["model_script"] = os.path.join(
                output_path, "model_script.py")