How to use test_no_label method in lettuce_webdriver

Best Python code snippet using lettuce_webdriver_python

evaluate.py

Source:evaluate.py Github

copy

Full Screen

1import pickle23import numpy as np4import pandas as pd5from tqdm import tqdm67import torch8from torch.nn.utils.rnn import pad_sequence910# from dump_models import load_model11from transformers import (12 BertForSequenceClassification,13 BertTokenizer,14 XLNetForSequenceClassification,15 XLNetTokenizer16)171819###################################################################20'''21evaluate new data22< test_model >23input: model, tokenizer, mean_val_acc, mean_val_loss, file_name='test_no_label', device='cuda'24output: make submission25'''26###################################################################272829def collate_fn_style_test(samples):30 input_ids = samples31 max_len = max(len(input_id) for input_id in input_ids)3233 attention_mask = torch.tensor([[1] * len(input_id) + [0] * (max_len - len(input_id)) for input_id in input_ids])34 input_ids = pad_sequence([torch.tensor(input_id) for input_id in input_ids], batch_first=True)35 token_type_ids = torch.tensor([[0] * len(input_id) for input_id in input_ids])36 position_ids = torch.tensor([list(range(len(input_id))) for input_id in input_ids])3738 return input_ids, attention_mask, token_type_ids, position_ids3940def test_model(model, tokenizer, mean_val_acc, mean_val_loss, file_name='test_no_label', device='cuda'):41 test_df = pd.read_csv('./datasets/' + file_name + '.csv')42 test_df_ = test_df['Id']4344 test = [sent.lower() for sent in test_df_]454647 """48 전처리49 """5051 test_dataset = [np.array(tokenizer.encode(line)) for line in test]525354 test_batch_size = 6455 test_loader = torch.utils.data.DataLoader(test_dataset,56 batch_size=test_batch_size,57 shuffle=False, 58 collate_fn=collate_fn_style_test,59 num_workers=2)60 # for idx, i in enumerate(test_loader):61 # print(i)62 # if idx == 1:63 # break6465 model.eval()66 with torch.no_grad():67 predictions = []68 for input_ids, attention_mask, token_type_ids, position_ids in tqdm(test_loader, desc='Test', position=1, leave=None):69 input_ids = input_ids.to(device)70 attention_mask = attention_mask.to(device)71 token_type_ids = token_type_ids.to(device)72 position_ids = position_ids.to(device)7374 output = model(input_ids=input_ids,75 attention_mask=attention_mask,76 token_type_ids=token_type_ids,77 position_ids=position_ids)78 79 logits = output.logits80 batch_predictions = [0 if example[0] > example[1] else 1 for example in logits]81 predictions += batch_predictions82 print(predictions)8384 test_df['Category'] = predictions85 test_df.to_csv('./submissions/sub' + str(int(mean_val_acc*100)) + str(int(mean_val_loss*1000)) + '.csv', index=False)868788if __name__ == '__main__':89 MODEL_NAME = 'bert-base-uncased'90 # try:91 # with open('./dump_models_toknizer/' + MODEL_NAME + '.p', 'rb') as f:92 # print('model exist => just load model')93 # model = pickle.load(f)94 # tokenizer = pickle.load(f)95 # except:96 # print('exeption occur => download model')97 # model, tokenizer = load_model(MODEL_NAME)9899 100 tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)101 MODEL_NAME = './best_models/model9850'102 model = BertForSequenceClassification.from_pretrained(MODEL_NAME)103 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')104 model.to(device)105 106 # test_model(model, tokenizer, 0, 0, file_name='test_no_label', device='cuda') ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run lettuce_webdriver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful