<jupyter_start><jupyter_text>MLE Generation<jupyter_code># location to store results
os.makedirs(config.task.name,exist_ok=True)
print(config.task.mle_weights_path)
mle_weights = torch.load(config.task.mle_weights_path)
model.generator.load_state_dict(mle_weights)
temperatures = (np.arange(10) + 1) / 10.0

for temperature in temperatures:
    f_write = open(f"{config.task.name}/mle_{temperature}_{random_seed}.txt", "w")

    for batch in tqdm.tqdm(valid_dataloader):
        batch = collate_fn(batch)
        batch = move_to_device(batch, device)

        ground_truth = batch["target_text"]

        results = decoder.generate(batch["source_token_ids"], temperature=temperature)
        generated = []

        for i in range(len(results["tokens"])):
            res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
            generated.append(res)

        for gt, gen in zip(ground_truth, generated):
            f_write.write(json.dumps([gt, gen]))
            f_write.write("\n")
        
    f_write.close()<jupyter_output>100%|██████████| 189/189 [01:01<00:00,  3.05it/s]
100%|██████████| 189/189 [01:11<00:00,  2.64it/s]
100%|██████████| 189/189 [01:18<00:00,  2.41it/s]
100%|██████████| 189/189 [01:45<00:00,  1.79it/s]
100%|██████████| 189/189 [02:07<00:00,  1.48it/s]
100%|██████████| 189/189 [02:29<00:00,  1.27it/s]
 52%|█████▏    | 99/189 [01:32<01:23,  1.07it/s]<jupyter_text>Beam Search<jupyter_code># f_write = open(f"{config.task.name}/mle_beam_4.txt", "w")

# for batch in tqdm.tqdm(valid_dataloader):
#     batch = collate_fn(batch)
#     batch = move_to_device(batch, device)

#     ground_truth = batch["target_text"]

#     results = decoder.generate(batch["source_token_ids"], do_sample=False, num_beams=4)
#     generated = []

#     for i in range(len(results["tokens"])):
#         res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
#         generated.append(res)

#     for gt, gen in zip(ground_truth, generated):
#         f_write.write(json.dumps([gt, gen]))
#         f_write.write("\n")        

# f_write.close()<jupyter_output><empty_output><jupyter_text>TextGAIL Generation<jupyter_code>print(config.task.textgail_weights_path)
textgail_weights = torch.load(config.task.textgail_weights_path)
model.load_state_dict(textgail_weights)
for temperature in temperatures:
    f_write = open(f"{config.task.name}/textgail_{temperature}_{random_seed}.txt", "w")

    for batch in tqdm.tqdm(valid_dataloader):
        batch = collate_fn(batch)
        batch = move_to_device(batch, device)

        ground_truth = batch["target_text"]

        results = decoder.generate(batch["source_token_ids"], temperature=temperature)
        generated = []

        for i in range(len(results["tokens"])):
            res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
            generated.append(res)

        for gt, gen in zip(ground_truth, generated):
            f_write.write(json.dumps([gt, gen]))
            f_write.write("\n")
        
    f_write.close()<jupyter_output><empty_output><jupyter_text>Beam Search<jupyter_code>f_write = open(f"{config.task.name}/textgail_no_pretrain2_beam_4.txt", "w")

for batch in tqdm.tqdm(valid_dataloader):
    batch = collate_fn(batch)
    batch = move_to_device(batch, device)

    ground_truth = batch["target_text"]

    results = decoder.generate(batch["source_token_ids"], do_sample=False, num_beams=4)
    generated = []

    for i in range(len(results["tokens"])):
        res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
        generated.append(res)
        
    for gt, gen in zip(ground_truth, generated):
        f_write.write(json.dumps([gt, gen]))
        f_write.write("\n")        

f_write.close()
hydra._internal.GlobalHydra().get_state().clear()
import hydra
from hydra.experimental import initialize, compose
import numpy as np
import tqdm
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import RobertaTokenizer
from omegaconf import DictConfig

from torchfly.text.decode import TransformerDecoder
from torchfly.common import set_random_seed, move_to_device

from configure_dataloader import DataLoaderHandler
from model import Generator, TextGAILModel

import logging
import os
import pathlib
random_seed = 1
set_random_seed(random_seed)
hydra.experimental.initialize("config")
# config = hydra.experimental.compose("config.yaml")
hydra.core.global_hydra.GlobalHydra().instance().clear()

with initialize(config_path='config'):
  config = compose(config_name="config")
# cfg = hydra.experimental.compose(config_file='config.yaml', overrides=[])
print(config.pretty())
config
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
dataloader_handler = DataLoaderHandler(config)
valid_dataloader = dataloader_handler.test_dataloader(config)
collate_fn = test_dataloader.dataset.collate_fn
device = torch.device("cuda")
model = TextGAILModel(config)
model = model.cuda()
decoder = TransformerDecoder(config.decode)
decoder.register_generator(model.generator.decoder)
decoder.register_tokenizer(tokenizer)
decoder.prepare_model_inputs_for_generation = model.generator.prepare_model_inputs_for_generation<jupyter_output><empty_output><jupyter_text>MLE Generation<jupyter_code># location to store results
os.makedirs(config.task.name,exist_ok=True)
print(config.task.mle_weights_path)
mle_weights = torch.load(config.task.mle_weights_path)
model.generator.load_state_dict(mle_weights)
temperatures = (np.arange(10) + 1) / 10.0
for temperature in temperatures:
    f_write = open(f"{config.task.name}/mle_{temperature}_{random_seed}.txt", "w")

    for batch in tqdm.tqdm(valid_dataloader):
        batch = collate_fn(batch)
        batch = move_to_device(batch, device)

        ground_truth = batch["target_text"]

        results = decoder.generate(batch["source_token_ids"], temperature=temperature)
        generated = []

        for i in range(len(results["tokens"])):
            res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
            generated.append(res)

        for gt, gen in zip(ground_truth, generated):
            f_write.write(json.dumps([gt, gen]))
            f_write.write("\n")
        
    f_write.close()<jupyter_output><empty_output><jupyter_text>Beam Search<jupyter_code># f_write = open(f"{config.task.name}/mle_beam_4.txt", "w")

# for batch in tqdm.tqdm(valid_dataloader):
#     batch = collate_fn(batch)
#     batch = move_to_device(batch, device)

#     ground_truth = batch["target_text"]

#     results = decoder.generate(batch["source_token_ids"], do_sample=False, num_beams=4)
#     generated = []

#     for i in range(len(results["tokens"])):
#         res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
#         generated.append(res)

#     for gt, gen in zip(ground_truth, generated):
#         f_write.write(json.dumps([gt, gen]))
#         f_write.write("\n")        

# f_write.close()<jupyter_output><empty_output><jupyter_text>TextGAIL Generation<jupyter_code>print(config.task.textgail_weights_path)
textgail_weights = torch.load(config.task.textgail_weights_path)
model.load_state_dict(textgail_weights)
for temperature in temperatures:
    f_write = open(f"{config.task.name}/textgail_{temperature}_{random_seed}.txt", "w")

    for batch in tqdm.tqdm(valid_dataloader):
        batch = collate_fn(batch)
        batch = move_to_device(batch, device)

        ground_truth = batch["target_text"]

        results = decoder.generate(batch["source_token_ids"], temperature=temperature)
        generated = []

        for i in range(len(results["tokens"])):
            res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
            generated.append(res)

        for gt, gen in zip(ground_truth, generated):
            f_write.write(json.dumps([gt, gen]))
            f_write.write("\n")
        
    f_write.close()<jupyter_output><empty_output><jupyter_text>Beam Search<jupyter_code>f_write = open(f"{config.task.name}/textgail_no_pretrain2_beam_4.txt", "w")

for batch in tqdm.tqdm(valid_dataloader):
    batch = collate_fn(batch)
    batch = move_to_device(batch, device)

    ground_truth = batch["target_text"]

    results = decoder.generate(batch["source_token_ids"], do_sample=False, num_beams=4)
    generated = []

    for i in range(len(results["tokens"])):
        res = tokenizer.decode(results["tokens"][i][0][1:-1].tolist())
        generated.append(res)
        
    for gt, gen in zip(ground_truth, generated):
        f_write.write(json.dumps([gt, gen]))
        f_write.write("\n")        

f_write.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1459177764910249, 'fraction_numerical': 0.021424435437174292, 'mean_word_length': 4.249848024316109, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '102806', 'license': 'MIT', 'n_tokens_mistral': 3189, 'n_tokens_neox': 2980, 'n_words': 460, 'path': 'Conditional/Evaluation/Generation.ipynb', 'repo_name': 'Macho000/TextGAIL', 'size': '8635'}