### Install DialogEntailment Package Source: https://github.com/nouhadziri/dialogentailment/blob/master/README.md Installs the DialogEntailment package and links the SpaCy English model. Ensure Python 3.6+ and other dependencies are met. ```bash git clone git@github.com:nouhadziri/DialogEntailment.git pip install -e . python -m spacy link en_core_web_lg en ``` -------------------------------- ### Run Full Visualization Pipeline Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Execute the complete visualization pipeline using specified response and human judgment files, BERT and ESIM models, and generator types. Output plots are saved to the specified directory. ```bash python -m dialogentail \ --response_file reddit \ --human_judgment reddit \ --bert_dir ./models/bert_entailment \ --esim_model ./models/esim_elmo/model.tar.gz \ --embedding elmo \ --generator_types THRED HRED Seq2Seq TA-Seq2Seq \ --plot_dir ./plots ``` ```bash python -m dialogentail \ --response_file opensubtitles \ --human_judgment opensubtitles \ --bert_dir ./models/bert_entailment \ --plot_dir ./plots ``` ```bash python -m dialogentail \ --response_file ./my_responses.txt \ --human_judgment ./my_human_scores.pkl \ --generator_types Model1 Model2 Model3 \ --bert_dir ./models/bert_entailment ``` -------------------------------- ### Train ESIM with AllenNLP Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Executes training for the ESIM model using AllenNLP scripts and custom JSONNet configuration files. ```bash # Train ESIM model with provided configuration training/allennlp.sh -s ./models/esim_elmo --overwrite # Custom configuration file training/allennlp.sh -s ./models/esim_custom --config ./my_config.jsonnet ``` ```jsonnet // Example training config (training/configs/esim_elmo.jsonnet) { "dataset_reader": { "type": "snli", "tokenizer": {"type": "word"}, "token_indexers": { "elmo": {"type": "elmo_characters"} } }, "train_data_path": "./data/InferConvAI/train.jsonl", "validation_data_path": "./data/InferConvAI/dev.jsonl", "model": { "type": "esim", "text_field_embedder": { "token_embedders": { "elmo": {"type": "elmo_token_embedder"} } }, "encoder": {"type": "lstm", "input_size": 1024, "hidden_size": 300} }, "trainer": { "num_epochs": 20, "patience": 5, "cuda_device": 0 } } ``` -------------------------------- ### Generate NLI Samples Programmatically Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Programmatically generate different types of NLI samples for training entailment models. This includes generating neutral (dull) samples, neutral samples from existing utterances, and contradiction (broken) samples. ```python from dialogentail.preprocessing.convai_to_nli import ( generate_dull_samples, generate_poor_samples_from_utterances, generate_broken_samples ) # Generate neutral samples (dull responses) dull_samples = generate_dull_samples(size=2) print(dull_samples) # ["i don't know .", "i'm not sure ."] # Generate neutral samples from other utterances in dialogue utterances = ["Hello!", "How are you?", "I'm great!", "What's up?", "Nothing much."] poor_samples = generate_poor_samples_from_utterances(utterances, lb=1, ub=3, size=2) print(poor_samples) # Randomly selected from utterances[0] and utterances[4:] # Generate contradiction samples (broken/incoherent responses) broken_samples = generate_broken_samples(utterances, lb=1, ub=3, size=2) print(broken_samples) # Random token combinations forming incoherent text ``` -------------------------------- ### Use Embedders API Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Demonstrates how to instantiate and use various embedding models for sentence and collection-level vectorization. ```python from dialogentail.embedder.factory import from_embedding from dialogentail.embedder.elmo import ElmoEmbedder from dialogentail.embedder.bert import BertEmbedder # Factory method to get embedder by name embedder = from_embedding('elmo') # Options: 'elmo', 'bert', 'bert-base-uncased', 'bert-large-uncased', 'glove', 'word2vec' # Direct instantiation elmo_embedder = ElmoEmbedder() bert_embedder = BertEmbedder(bert_model='bert-base-uncased', no_cuda=False) # Embed a single sentence sentence = "I love playing tennis on weekends" embedding = elmo_embedder.embed_sentence(sentence, tokenized=True, term_vectors=False) print(f"Sentence embedding shape: {embedding.shape}") # (embedding_dim,) # Get term-level vectors term_embeddings = elmo_embedder.embed_sentence(sentence, term_vectors=True) print(f"Term embeddings shape: {term_embeddings.shape}") # (num_tokens, embedding_dim) # Batch embedding collection sentences = [ "Hello, how are you?", "I'm doing great, thanks!", "What are your plans for today?" ] for embedding in elmo_embedder.embed_collection(sentences, tokenized=True, pooling="mean"): print(f"Embedding shape: {embedding.shape}") # BERT with custom layer selection bert_embeddings = bert_embedder.embed_collection( sentences, layers=[-1, -2, -3, -4], # Last 4 layers max_seq_length=128, batch_size=32, pooling="concat" # Options: 'concat', 'mean', 'max' ) ``` -------------------------------- ### Evaluate Dialogue Coherence with ESIM Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Initializes the ESIMCoherence class with a pre-trained model archive to compute entailment metrics for dialogue turns. ```python from dialogentail.coherence import ESIMCoherence # Initialize with trained ESIM model archive esim_coherence = ESIMCoherence(model_archive="./models/esim_elmo/model.tar.gz") # Evaluate dialogue turn conversation_history = [ "Do you have any pets?", "Yes, I have two cats named Luna and Milo." ] actual_response = "Cats are wonderful companions." generated_response = "I prefer dogs over cats." results = esim_coherence.compute_metric(conversation_history, actual_response, generated_response) # Each result includes labels for full context and individual utterances print(results[0]) # {'context_label': 'entailment', 'Utt_-2_label': 'neutral', 'Utt_-1_label': 'entailment', ...} print(results[1]) # {'context_label': 'neutral', 'Utt_-2_label': 'neutral', 'Utt_-1_label': 'contradiction', ...} ``` -------------------------------- ### Visualize DialogEntailment Results Source: https://github.com/nouhadziri/dialogentailment/blob/master/README.md Generates plots for visualizing results from trained ESIM and BERT models. Requires paths to the BERT model directory and the ESIM model file. ```bash python -m dialogentail --bert_dir --esim_model [--plots_dir ] ``` -------------------------------- ### Programmatic Visualization and Metric Computation Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Perform visualization and compute correlation metrics programmatically. This involves setting up SemanticSimilarity, computing metrics, and accessing the resulting DataFrames. ```python from dialogentail.visualize import plot, compute_metric from dialogentail.semantic_similarity import SemanticSimilarity # Programmatic visualization plot( response_file="https://s3.ca-central-1.amazonaws.com/ehsk-research/thred/eval/responses_reddit.txt", human_judgment_file="https://s3.ca-central-1.amazonaws.com/ehsk-research/thred/eval/human_mer_reddit.pkl", generator_types=["THRED", "HRED", "Seq2Seq", "TA-Seq2Seq"], bert_model_dir="./models/bert_entailment", esim_model="./models/esim_elmo/model.tar.gz", embedding_method='elmo', plots_dir="./output_plots" ) # Compute metrics and get DataFrames for custom analysis ss = SemanticSimilarity(embedding_method='elmo') df, groundtruth_df, gen_df = compute_metric(ss, ["THRED", "HRED"], "responses.txt") # Access computed metrics print(gen_df[['gen_type', 'SS_context', 'delta_SS_context']].head()) ``` -------------------------------- ### Train ESIM Model with AllenNLP Source: https://github.com/nouhadziri/dialogentailment/blob/master/README.md Trains the Enhanced Sequential Inference Model (ESIM) entangled with ELMO embeddings using AllenNLP. Specify model directory and optionally configuration file and overwrite flag. ```bash training/allennlp.sh -s [--overwrite] [--config ] ``` -------------------------------- ### Convert ConvAI Data to NLI Format Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Convert ConvAI dialogue data into NLI format for training entailment models. This command-line tool supports basic conversion and advanced configuration with augmentation from MultiNLI and DialogueNLI datasets. ```bash python -m dialogentail.preprocessing.convai_to_nli ./data/convai_train.txt \ --context_size 2 ``` ```bash python -m dialogentail.preprocessing.convai_to_nli ./data/convai_train.txt \ --context_size 2 \ --prob_dull_samples 0.33 \ --n_dull_samples 2 \ --prob_broken_samples 0.30 \ --max_broken_samples 3 \ --max_poor_samples 3 \ --mnli_file ./data/multinli_1.0_train.txt \ --mnli_contradictions 180000 \ --mnli_neutrals 0 \ --mnli_entailments 0 \ --dnli_file ./data/dialogue_nli_train.jsonl \ --dnli_positives 20000 \ --dnli_neutrals 4000 \ --dnli_negatives 110000 ``` -------------------------------- ### BERT Model Training Arguments Source: https://github.com/nouhadziri/dialogentailment/blob/master/README.md Lists default and configurable arguments for training a BERT model for entailment using the dialogentail.huggingface module. ```text --train_dataset default: InferConvAI train data --eval_dataset default: InferConvAI validation data --model bert-base-uncased, bert-large-uncased (default: bert-base-uncased) --train_batch_size default: 32 --eval_batch_size default: 8 --num_train_epochs default: 3 --max_seq_length default: 128 ``` -------------------------------- ### Read Response Files with ResponseFileReader Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Utilize the ResponseFileReader class to parse response files, iterating through conversation histories, ground truth responses, and generated responses from multiple models. Ensure the correct number of generator methods is specified. ```python from dialogentail.reader.response_reader import ResponseFileReader # Response file format: # Line 1: TAB-separated utterances (conversation history) # Line 2: Ground truth response # Line 3: Generated response from Model_1 # Line 4: Generated response from Model_2 # ... repeat for each test sample # Example response file content: # "Hello, how are you?\tI'm doing well, thanks for asking." # That's great to hear! # I'm fine. # Good, thanks. n_generator_methods = 2 # Number of models being evaluated reader = ResponseFileReader("test_responses.txt", n_generator_methods) for conversation_history, actual_response, generated_responses in reader: print(f"Context: {conversation_history}") print(f"Ground truth: {actual_response}") print(f"Generated responses: {generated_responses}") print("---") # Output: # Context: ['Hello, how are you?', "I'm doing well, thanks for asking."] # Ground truth: That's great to hear! # Generated responses: ["I'm fine.", "Good, thanks."] ``` -------------------------------- ### Fine-tune BERT for Entailment Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Trains a BERT model on dialogue entailment data using either command-line arguments or the programmatic run function. ```bash # Train BERT model on InferConvAI dataset python -m dialogentail.huggingface \ --do_train \ --do_eval \ --bert_model bert-base-uncased \ --train_dataset ./data/InferConvAI/train.tsv \ --eval_dataset ./data/InferConvAI/dev.tsv \ --output_dir ./models/bert_entailment \ --train_batch_size 32 \ --eval_batch_size 8 \ --num_train_epochs 3 \ --max_seq_length 128 \ --learning_rate 5e-5 ``` ```python # Programmatic training via the run function from dialogentail.huggingface.finetune_bert import run import argparse args = argparse.Namespace( bert_model='bert-base-uncased', train_dataset='./data/InferConvAI/train.tsv', eval_dataset='./data/InferConvAI/dev.tsv', output_dir='./models/bert_entailment', do_train=True, do_eval=True, train_batch_size=32, eval_batch_size=8, num_train_epochs=3, max_seq_length=128, learning_rate=5e-5, warmup_proportion=0.1, gradient_accumulation_steps=1, no_cuda=False, local_rank=-1, seed=42, do_lower_case=True, cache_dir='' ) run(args) # Output: eval_loss, eval_accuracy, and saved model weights in output_dir ``` -------------------------------- ### Compute metrics and correlations Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Calculates semantic similarity metrics and computes Pearson and Spearman correlations against human scores. ```python # Compute correlation after running evaluation from dialogentail.util import files from dialogentail.semantic_similarity import SemanticSimilarity from dialogentail.visualize import compute_metric ss = SemanticSimilarity(embedding_method='elmo') _, _, gen_df = compute_metric(ss, ["Seq2Seq", "HRED", "THRED"], "responses.txt") human_scores = np.array([score for _, _, score in human_judgment]) gen_df['human_score'] = human_scores # Calculate correlations pearson_r, pearson_p = pearsonr(gen_df['human_score'], gen_df['SS_context']) spearman_r, spearman_p = spearmanr(gen_df['human_score'], gen_df['SS_context']) print(f"Pearson correlation: r={pearson_r:.4f}, p={pearson_p:.4f}") print(f"Spearman correlation: r={spearman_r:.4f}, p={spearman_p:.4f}") ``` -------------------------------- ### Train BERT Model for Entailment Source: https://github.com/nouhadziri/dialogentailment/blob/master/README.md Fine-tunes a pre-trained BERT model for the entailment task using the Hugging Face Transformers library. Supports training and evaluation with various configurable parameters. ```bash python -m dialogentail.huggingface --do_eval --do_train --output_dir ``` -------------------------------- ### Compute semantic similarity Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Initialize the SemanticSimilarity class to evaluate dialogue responses against history using embedding-based cosine similarity. ```python from dialogentail.semantic_similarity import SemanticSimilarity # Initialize with ELMo embeddings (default) ss = SemanticSimilarity( embedding_method='elmo', # Options: 'elmo', 'bert-base-uncased', 'bert-large-uncased', 'glove', 'word2vec' separator='[SEP]', boost_factor=True # Penalizes dull responses like "I don't know" ) # Evaluate a single dialogue turn conversation_history = [ "What do you do for a living?", "I'm a software engineer at a tech startup." ] actual_response = "That sounds exciting! What kind of software do you build?" generated_response = "I don't know what you mean." results = ss.compute_metric(conversation_history, actual_response, generated_response) # Results contain similarity scores for ground truth and generated response print(results[0]) # Ground truth: {'i': 0, 'SS_context': 0.72, 'delta_SS_context': 0.0, 'gen_type': 'ground_truth', ...} print(results[1]) # Generated: {'i': 0, 'SS_context': 0.31, 'delta_SS_context': -0.41, 'gen_type': 'unknown', ...} # Batch evaluation from response file generator_methods = ["Seq2Seq", "HRED", "THRED", "TA-Seq2Seq"] batch_results = ss.compute_metric_for_file("responses.txt", generator_methods, cache_dir="./cache") ``` -------------------------------- ### Save human judgment data Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Serializes a list of human judgment tuples into a pickle file for later use. ```python # List of tuples: (method_name, sample_index, mean_human_rating) human_judgment = [ ('Seq2Seq', 0, 2.1), ('HRED', 0, 3.4), ('THRED', 0, 4.2), ('Seq2Seq', 1, 1.8), ('HRED', 1, 2.9), ('THRED', 1, 3.7), # ... more ratings ] # Save human judgment with open('human_judgment.pkl', 'wb') as f: pickle.dump(human_judgment, f) ``` -------------------------------- ### SemanticSimilarity Class Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Computes semantic similarity between dialogue context and generated responses using embedding-based cosine similarity. ```APIDOC ## SemanticSimilarity ### Description Computes semantic similarity between dialogue context and generated responses using embedding-based cosine similarity. Supports multiple embedding methods and includes a boost factor to penalize dull/generic responses. ### Parameters - **embedding_method** (string) - Required - Options: 'elmo', 'bert-base-uncased', 'bert-large-uncased', 'glove', 'word2vec' - **separator** (string) - Optional - Default: '[SEP]' - **boost_factor** (boolean) - Optional - Penalizes dull responses ### Methods - **compute_metric(conversation_history, actual_response, generated_response)**: Evaluates a single dialogue turn. - **compute_metric_for_file(file_path, generator_methods, cache_dir)**: Batch evaluation from a response file. ``` -------------------------------- ### Evaluate coherence with BERT Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Use the BertCoherence class to classify responses as entailed, neutral, or contradictory relative to the conversation history. ```python from dialogentail.coherence import BertCoherence # Initialize with fine-tuned BERT model bert_coherence = BertCoherence( model_dir="./models/bert_entailment", separator=" ", bert_model='bert-base-uncased', max_seq_length=128, batch_size=16 ) # Evaluate single dialogue conversation_history = [ "I'm a vegetarian.", "That's great for health and the environment." ] actual_response = "Yes, I stopped eating meat five years ago." generated_response = "I had a delicious steak for dinner yesterday." results = bert_coherence.compute_metric(conversation_history, actual_response, generated_response) # Results include entailment labels: 'entailment', 'neutral', or 'contradiction' print(results[0]) # {'i': 0, 'context_label': 'entailment', 'gen_type': 'ground_truth', ...} print(results[1]) # {'i': 0, 'context_label': 'contradiction', 'gen_type': 'unknown', ...} # Batch evaluation from file generator_methods = ["Seq2Seq", "HRED", "THRED"] batch_results = bert_coherence.compute_metric_for_file("test_responses.txt", generator_methods) ``` -------------------------------- ### SemanticDistance Class Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Computes semantic distance (inverse of similarity) between dialogue context and responses, where lower values indicate better coherence. ```APIDOC ## SemanticDistance ### Description Computes semantic distance (inverse of similarity) between dialogue context and responses. This is the metric reported in the paper where lower values indicate better coherence. ### Parameters - **embedding_method** (string) - Required - Options: 'elmo', 'bert-base-uncased', 'bert-large-uncased', 'glove', 'word2vec' - **separator** (string) - Optional - Default: '[SEP]' - **boost_factor** (boolean) - Optional - Higher penalty for dull responses ### Methods - **compute_metric(conversation_history, actual_response, generated_response)**: Evaluates dialogue coherence. ``` -------------------------------- ### BertCoherence Class Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Evaluates dialogue coherence using a fine-tuned BERT model for textual entailment classification. ```APIDOC ## BertCoherence ### Description Evaluates dialogue coherence using a fine-tuned BERT model for textual entailment classification. Predicts whether a generated response is entailed by, neutral to, or contradicts the conversation history. ### Parameters - **model_dir** (string) - Required - Path to the fine-tuned BERT model - **separator** (string) - Optional - Default: ' ' - **bert_model** (string) - Optional - Default: 'bert-base-uncased' - **max_seq_length** (integer) - Optional - Default: 128 - **batch_size** (integer) - Optional - Default: 16 ### Methods - **compute_metric(conversation_history, actual_response, generated_response)**: Evaluates single dialogue. - **compute_metric_for_file(file_path, generator_methods)**: Batch evaluation from file. ``` -------------------------------- ### Compute semantic distance Source: https://context7.com/nouhadziri/dialogentailment/llms.txt Use the SemanticDistance class to measure the inverse of similarity, where lower values indicate higher coherence. ```python from dialogentail.semantic_distance import SemanticDistance # Initialize semantic distance evaluator sd = SemanticDistance( embedding_method='elmo', separator='[SEP]', boost_factor=True # Higher penalty for dull responses ) # Evaluate dialogue coherence conversation_history = [ "I love hiking in the mountains.", "Me too! Have you tried any trails recently?" ] actual_response = "Yes, I went to Rocky Mountain National Park last weekend." generated_response = "I'm not sure what you're talking about." results = sd.compute_metric(conversation_history, actual_response, generated_response) # Lower distance = better coherence print(f"Ground truth distance: {results[0]['SS_context']:.4f}") # Lower value print(f"Generated distance: {results[1]['SS_context']:.4f}") # Higher value (worse) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.