Views
No views yet
1model_name = 'philipp-zettl/t5-small-long-qa'
2model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
3tokenizer = AutoTokenizer.from_pretrained('google/flan-t5-small')
4
5device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
6model = model.to(device)
7
8def eval(inputs, tokenizer, model):
9 model_inputs = tokenizer(inputs, max_length=512, padding=True, truncation=True)
10 input_ids = torch.tensor(model_inputs['input_ids']).to(device)
11 attention_mask = torch.tensor(model_inputs['attention_mask']).to(device)
12 with torch.no_grad():
13 sample_output = model.generate(
14 input_ids[:1],
15 max_length=85,
16 temperature=0.5,
17 do_sample=True
18 )
19 sample_output_text = tokenizer.decode(sample_output[0], skip_special_tokens=True)
20 input_text = tokenizer.decode(input_ids[0], skip_special_tokens=True)
21 print(f"Sample Input:\n \"{input_text}\"\n\n")
22 print(f"Model Output: \"{sample_output_text}\"")
23
24
25statement = "The model should change the words but not the meaning of this sentence."
26eval([f"paraphrase: {statement} . output: "], tokenizer, model)1def preprocess_batch(batch, tokenizer, max_input_length=512, max_output_length=128):
2 questions = batch['src']
3 answers = batch['tgt']
4
5 inputs = [f"paraphrase: {q} output: " for q in questions]
6 model_inputs = tokenizer(inputs, max_length=max_input_length, padding=True, truncation=True)
7
8 labels = tokenizer(answers, max_length=max_output_length, padding=True, truncation=True)
9 model_inputs['labels'] = labels['input_ids']
10
11 return model_inputs
12
13# Tokenize the dataset
14train_dataset = train_dataset.filter(lambda x: x['task'] == 'paraphrasing' and x['lang'] in ['de', 'en']).map(lambda batch: preprocess_batch(batch, tokenizer), batched=True)
15validation_dataset = validation_dataset.filter(lambda x: x['task'] == 'paraphrasing' and x['lang'] in ['de', 'en']).map(lambda batch: preprocess_batch(batch, tokenizer), batched=True)
16
17# Set format for PyTorch
18train_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
19validation_dataset.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])