<jupyter_start><jupyter_text><jupyter_code>import os
from pathlib import Path
in_folder_path = Path('../input/k/leolu1998/clrp-finetune-roberta-large')
scripts_dir = Path(in_folder_path / 'scripts')

os.chdir(scripts_dir)
exec(Path("imports.py").read_text())
exec(Path("config.py").read_text())
exec(Path("dataset.py").read_text())
exec(Path("model.py").read_text())
os.chdir('/kaggle/working')


test_df = pd.read_csv("/kaggle/input/commonlitreadabilityprize/test.csv")
tokenizer = torch.load('../input/mytokenizers/roberta-tokenizer.pt')
models_folder_path = Path(in_folder_path / 'models')
models_preds = []
n_models = 10

for model_num in range(n_models):
    print(f'Inference#{model_num+1}/{n_models}')
    test_ds = CLRPDataset(data=test_df, tokenizer=tokenizer, max_len=Config.max_len, is_test=True)
    test_sampler = SequentialSampler(test_ds)
    test_dataloader = DataLoader(test_ds, sampler = test_sampler, batch_size=Config.batch_size)
    model = torch.load(models_folder_path / f'best_model_{model_num}.pt').to(Config.device)

    all_preds = []
    
    model.eval()

    for step,batch in enumerate(test_dataloader):
        sent_id, mask = batch['input_ids'].to(Config.device), batch['attention_mask'].to(Config.device)
        with torch.no_grad():
            preds = model(sent_id, mask)
            all_preds += preds.flatten().cpu().tolist()
            
    models_preds.append(all_preds)
del model, tokenizer, test_dataloader, test_sampler
gc.collect()
torch.cuda.empty_cache()<jupyter_output>Inference#1/10
Inference#2/10
Inference#3/10
Inference#4/10
Inference#5/10
Inference#6/10
Inference#7/10
Inference#8/10
Inference#9/10
Inference#10/10<jupyter_text>ENS 2<jupyter_code>import os
from pathlib import Path
in_folder_path2 = Path('../input/clrprobertalarge463-lb')
scripts_dir2 = Path(in_folder_path2 / 'scripts')

os.chdir(scripts_dir2)
exec(Path("imports.py").read_text())
exec(Path("config.py").read_text())
exec(Path("dataset.py").read_text())
exec(Path("model.py").read_text())
os.chdir('/kaggle/working')

test_df = pd.read_csv("/kaggle/input/commonlitreadabilityprize/test.csv")
tokenizer = torch.load('../input/mytokenizers/roberta-tokenizer.pt')
models_folder_path2 = Path(in_folder_path2 / 'models')
models_preds2 = []
n_models = 5

for model_num in range(n_models):
    print(f'Inference#{model_num+1}/{n_models}')
    test_ds = CLRPDataset(data=test_df, tokenizer=tokenizer, max_len=Config.max_len, is_test=True)
    test_sampler = SequentialSampler(test_ds)
    test_dataloader = DataLoader(test_ds, sampler = test_sampler, batch_size=Config.batch_size)
    model2 = torch.load(models_folder_path2 / f'best_model_{model_num}.pt').to(Config.device)

    all_preds2 = []
    
    model2.eval()

    for step,batch in enumerate(test_dataloader):
        sent_id, mask = batch['input_ids'].to(Config.device), batch['attention_mask'].to(Config.device)
        with torch.no_grad():
            preds2 = model2(sent_id, mask)
            all_preds2 += preds2.flatten().cpu().tolist()
            
    models_preds2.append(all_preds2)

####
del model2, tokenizer, test_dataloader, test_sampler
gc.collect()
torch.cuda.empty_cache()<jupyter_output>Inference#1/5
Inference#2/5
Inference#3/5
Inference#4/5
Inference#5/5<jupyter_text>ENS 3<jupyter_code>import os
from pathlib import Path
in_folder_path3 = Path('../input/mymodelrobertabase')
scripts_dir3 = Path(in_folder_path3 / 'scripts')

os.chdir(scripts_dir3)
exec(Path("imports.py").read_text())
exec(Path("config.py").read_text())
exec(Path("dataset.py").read_text())
exec(Path("model.py").read_text())
os.chdir('/kaggle/working')

test_df = pd.read_csv("/kaggle/input/commonlitreadabilityprize/test.csv")
tokenizer = torch.load('../input/mytokenizers/roberta-tokenizer.pt')
models_folder_path3 = Path(in_folder_path3 / 'models')
models_preds3 = []
n_models = 5

for model_num in range(n_models):
    print(f'Inference#{model_num+1}/{n_models}')
    test_ds = CLRPDataset(data=test_df, tokenizer=tokenizer, max_len=Config.max_len, is_test=True)
    test_sampler = SequentialSampler(test_ds)
    test_dataloader = DataLoader(test_ds, sampler = test_sampler, batch_size=Config.batch_size)
    model3 = torch.load(models_folder_path3 / f'best_model_{model_num}.pt').to(Config.device)

    all_preds3 = []
    
    model3.eval()

    for step,batch in enumerate(test_dataloader):
        sent_id, mask = batch['input_ids'].to(Config.device), batch['attention_mask'].to(Config.device)
        with torch.no_grad():
            preds3 = model3(sent_id, mask)
            all_preds3 += preds3.flatten().cpu().tolist()
            
    models_preds3.append(all_preds3)


all_preds
all_preds2
all_preds3
models_preds_ens = 0.51*np.array(models_preds).mean(axis=0)+(np.array(models_preds2)*0.3+np.array(models_preds3)*0.2).mean(axis=0)
print(models_preds_ens.shape)
print(models_preds_ens)
all_preds_ens = models_preds_ens

result_df = pd.DataFrame(
    {
        'id': test_df.id,
        'target': all_preds_ens
    })


result_df.to_csv('submission.csv', index=False)
result_df.head(10)<jupyter_output>(7,)
[-0.36548318 -0.47019614 -0.39377353 -2.38485637 -1.88973525 -1.38508565
  0.2151849 ]
{'fraction_non_alphanumeric': 0.14628019323671498, 'fraction_numerical': 0.03285024154589372, 'mean_word_length': 5.082256169212691, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '20252', 'license': 'MIT', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1980, 'n_words': 247, 'path': '4.clrp-inference.ipynb', 'repo_name': 'Leo1998-Lu/CommonLit-Readability-Prize-Silver-Medal-Solution', 'size': '5175'}