is this correct why to compute blue after trainer.train for question geneartiiom
- User
is this correct why to compute blue after trainer.train for question geneartiiom
- Assistant
- User
bleu for test dataset
- Assistant
- User
this is my Dataset({ features: ['query', 'positive', 'index_level_0', 'input_ids', 'attention_mask', 'offset_mapping', 'labels', 'decoder_attention_mask'], num_rows: 20000 })
- Assistant
- User
cde like this from nltk.translate.bleu_score import SmoothingFunction, corpus_bleu
Function to calculate BLEU score for the entire dataset
def calculate_bleu(references, hypotheses): smooth = SmoothingFunction().method1 reference_tokens = [[ref.split()] for ref in references] hypothesis_tokens = [hyp.split() for hyp in hypotheses] bleu_score = corpus_bleu(reference_tokens, hypothesis_tokens, smoothing_function=smooth) return bleu_score
Function to generate a question using a given model and context
Function to generate questions and calculate BLEU score for the entire test dataset
def generate_and_calculate_bleu(model, tokenizer, test_dataset, max_records=None, max_length=128, device="cuda" if torch.cuda.is_available() else "cpu"): generated_questions = [] reference_questions = []
# Generate questions for each example in the test dataset for i, context in enumerate(test_dataset): if max_records is not None and i >= max_records: break # Generate question using the model generated_question = generate_question(model, tokenizer, context, max_length=max_length, device=device) # For illustration purposes, assuming reference questions are the same as context reference_question = context generated_questions.append(generated_question) reference_questions.append(reference_question) # Calculate BLEU score bleu = calculate_bleu(reference_questions, generated_questions) print(f"BLEU Score: {bleu * 100:.2f}") - Assistant
- User
can compute another metric suitable for question genertain task
- Assistant