Example #1
0
        for ch in parent_to_child[p]:
            temp_df = pickle.load(
                open(
                    pkl_dump_dir + "exclusive/" + algo + "/" + str(iteration) +
                    "it/" + ch + ".pkl", "rb"))
            temp_df["label"] = [ch] * len(temp_df)
            if df_weaksup is None:
                df_weaksup = temp_df
            else:
                df_weaksup = pd.concat([df_weaksup, temp_df])

    df = pd.concat([df, df_weaksup])
    coarse_input_ids, coarse_attention_masks = basic_gpt2_tokenize(
        tokenizer, df.text.values, df.label.values, pad_token_dict)
    # Combine the training inputs into a TensorDataset.
    dataset = TensorDataset(coarse_input_ids, coarse_attention_masks)

    # Create a 90-10 train-validation split.
    coarse_train_dataloader, coarse_validation_dataloader = create_data_loaders(
        dataset, batch_size=4)

    model = train(model, tokenizer, coarse_train_dataloader,
                  coarse_validation_dataloader, doc_start_ind, all_labels,
                  device, pad_token_dict)
    test_generate(model, tokenizer, all_labels, pad_token_dict, device)

    tokenizer.save_pretrained(tok_path)
    torch.save(model, model_path + model_name)
    pickle.dump(pad_token_dict, open(pkl_dump_dir + "pad_token_dict.pkl",
                                     "wb"))
                batch_size=1  # Trains with this batch size.
            )
            label_to_exclusive_dataloader[ch] = func(dataloader)

        fine_posterior, fine_model = train(coarse_model,
                                           fine_model,
                                           coarse_tokenizer,
                                           fine_tokenizer,
                                           train_dataloader,
                                           validation_dataloader,
                                           label_to_exclusive_dataloader,
                                           doc_start_ind,
                                           index_to_label,
                                           device,
                                           secondary_device)
        test_generate(fine_model, fine_tokenizer, children, pad_token_dict, device)
        # true, preds, _ = test(fine_model, fine_posterior, fine_input_ids, fine_attention_masks, doc_start_ind,
        #                       index_to_label, label_to_index, list(temp_df.label.values), device)
        # all_true += true
        # all_preds += preds

        fine_tokenizer.save_pretrained(fine_tok_path)
        torch.save(fine_model, fine_model_path + p + ".pt")
        torch.save(fine_posterior, fine_label_path + "/fine_posterior.pt")
        pickle.dump(index_to_label, open(fine_label_path + "/index_to_label.pkl", "wb"))
        pickle.dump(label_to_index, open(fine_label_path + "/label_to_index.pkl", "wb"))

        print("*" * 80)

    # print(classification_report(all_true, all_preds), flush=True)
    print("*" * 80, flush=True)
Example #3
0
        kl_dataloader = func(kl_dataloader)

        # Combine the training inputs into a TensorDataset.
        dataset = TensorDataset(input_ids, attention_masks, labels)

        # Create a 90-10 train-validation split.
        train_dataloader, validation_dataloader = create_data_loaders(dataset, batch_size=4)

        doc_start_ind_dict = {}
        for ch in children:
            doc_start_ind_dict[ch] = doc_start_ind

        model = train(fine_model, fine_tokenizer, train_dataloader, validation_dataloader, kl_dataloader,
                      index_to_label, pad_token_dict, doc_start_ind_dict, device, coarse_model, coarse_tokenizer,
                      doc_start_ind)
        test_generate(model, fine_tokenizer, set(children), pad_token_dict, device)

        fine_label_path = base_fine_path + p
        os.makedirs(fine_label_path, exist_ok=True)
        fine_tok_path = fine_label_path + "/tokenizer"
        fine_model_path = fine_label_path + "/model/"
        os.makedirs(fine_tok_path, exist_ok=True)
        os.makedirs(fine_model_path, exist_ok=True)

        fine_tokenizer.save_pretrained(fine_tok_path)
        torch.save(fine_model, fine_model_path + p + ".pt")
        pickle.dump(index_to_label, open(fine_label_path + "/index_to_label.pkl", "wb"))
        pickle.dump(label_to_index, open(fine_label_path + "/label_to_index.pkl", "wb"))
        pickle.dump(pad_token_dict, open(fine_label_path + "/pad_token_dict.pkl", "wb"))

        print("*" * 80)