### Install vendi_score from source Source: https://github.com/vertaix/vendi-score/blob/main/README.md Clone the repository and install vendi_score in editable mode. ```bash git clone https://github.com/vertaix/Vendi-Score.git cd Vendi-Score pip install -e . ``` -------------------------------- ### Install vendi_score from pip Source: https://github.com/vertaix/vendi-score/blob/main/README.md Install the vendi_score package using pip. ```bash pip install vendi_score ``` -------------------------------- ### Install optional dependencies Source: https://github.com/vertaix/vendi-score/blob/main/README.md Install optional dependencies for specific data types like images, text, or molecules. ```bash pip install vendi_score[images] ``` ```bash pip install vendi_score[text,molecules] ``` ```bash pip install vendi_score[all] ``` -------------------------------- ### Group MultiNLI Examples by Category Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Groups MultiNLI examples by their 'y' label and prints the count of examples in each category. This is a setup step for diversity analysis. ```python categories = sorted(set(e.labels["y"] for e in mnli_examples)) category_groups = {c: data_utils.Group(c, []) for c in categories} for e in mnli_examples: category_groups[e.labels["y"]].examples.append(e) groups = list(category_groups.values()) for g in groups: print(g.name, len(g.examples)) ``` -------------------------------- ### Access and Print Example Data Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Retrieves the second example from the loaded MultiNLI dataset and prints its text content (e.x) and genre label (e.labels["y"]). ```python e = mnli_examples[1] print("Sentence:", e.x) print("Genre:", e.labels["y"]) ``` -------------------------------- ### Group examples by category Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Organizes the CIFAR100 examples into groups based on their category labels. This setup is used for calculating diversity metrics per category. ```python categories = sorted(set(e.labels["y"] for e in cifar_examples)) category_groups = {c: data_utils.Group(c, []) for c in categories} for e in cifar_examples: category_groups[e.labels["y"]].examples.append(e) groups = list(category_groups.values()) ``` -------------------------------- ### Load MultiNLI Dataset and Print Count Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Loads the MultiNLI dataset using text_utils.get_mnli(). Each example is stored in a data_utils.Example object. The number of loaded examples is then printed. ```python mnli_examples = text_utils.get_mnli() print(len(mnli_examples)) ``` -------------------------------- ### Load CIFAR100 dataset and display an example Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Loads the CIFAR100 dataset using image_utils.get_cifar100 and displays the label and feature names of the first example. The image data is stored in Pillow format. ```python cifar_examples = image_utils.get_cifar100("samples/cifar100/original", split="test") e = cifar_examples[0] print("Label:", e.labels["y"], "Features names:", e.features.keys()) e.x ``` -------------------------------- ### Prepare for Mode Dropping Analysis Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Initializes a tokenizer and prepares groups for mode dropping analysis by selecting a subset of categories and examples. ```python tokenizer = text_utils.get_tokenizer() random.seed(0) cats = sorted(categories) label_to_examples = {group.name: group.examples for group in category_groups.values()} mode_dropping_groups = data_utils.mode_dropping_groups(label_to_examples, cats, N=500) ``` -------------------------------- ### Clone MOSES repository Source: https://github.com/vertaix/vendi-score/blob/main/examples/molecules.ipynb Clones the Molecular Sets (MOSES) repository, which contains sample molecular datasets required for this example. ```shell # ! git clone https://github.com/molecularsets/moses.git ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vertaix/vendi-score/blob/main/examples/molecules.ipynb Imports required libraries for data manipulation, plotting, and RDKit functionalities. Ensure these libraries are installed. ```python import importlib import random from matplotlib import pyplot as plt plt.rcParams["savefig.bbox"] = "tight" import numpy as np import scipy.sparse from rdkit import Chem from rdkit.Chem.AllChem import GetMorganFingerprintAsBitVect ``` -------------------------------- ### Compute pixel-based Vendi Scores for MNIST digits Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate Vendi Scores for MNIST digits using pixel similarity. This example demonstrates using `image_utils.pixel_vendi_score`. ```python from torchvision import datasets from vendi_score import image_utils mnist = datasets.MNIST("data/mnist", train=False, download=True) digits = [[x for x, y in mnist if y == c] for c in range(10)] pixel_vs = [image_utils.pixel_vendi_score(imgs) for imgs in digits] for y, pvs in enumerate(pixel_vs): print(f"{y}\t{pvs:.02f}") ``` -------------------------------- ### Compute N-gram Vendi Score for text sentences Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate the Vendi Score for a list of sentences using n-gram similarity. This example uses `text_utils.ngram_vendi_score` with different n-gram orders. ```python from vendi_score import text_utils sents = ["Look, Jane.", "See Spot.", "See Spot run.", "Run, Spot, run.", "Jane sees Spot run."] ngram_vs = text_utils.ngram_vendi_score(sents, ns=[1, 2]) ``` -------------------------------- ### Getting Colormap Colors Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Retrieves a list of colors from the 'Set1' colormap. Useful for assigning distinct colors to plot elements. ```python cmap = plt.cm.get_cmap("Set1") colors = cmap.colors ``` -------------------------------- ### Compute Inception embedding-based Vendi Scores for MNIST digits Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate Vendi Scores for MNIST digits using embeddings from the Inception v3 model. This example shows how to use `image_utils.embedding_vendi_score` with a specified device. ```python from torchvision import datasets from vendi_score import image_utils mnist = datasets.MNIST("data/mnist", train=False, download=True) digits = [[x for x, y in mnist if y == c] for c in range(10)] # The default embeddings are from the pool-2048 layer of the torchvision # Inception v3 model. inception_vs = [image_utils.embedding_vendi_score(imgs, device="cuda") for imgs in digits] for y, ivs in enumerate(inception_vs): print(f"{y}\t{ivs:02f}") ``` -------------------------------- ### Compute SimCSE embedding-based Vendi Score for text sentences Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate the Vendi Score for a list of sentences using SimCSE embeddings. This example demonstrates using `text_utils.embedding_vendi_score` with a SimCSE model. ```python from vendi_score import text_utils sents = ["Look, Jane.", "See Spot.", "See Spot run.", "Run, Spot, run.", "Jane sees Spot run."] simcse_vs = text_utils.embedding_vendi_score(sents, model_path="princeton-nlp/unsup-simcse-bert-base-uncased") print(f"N-grams: {ngram_vs:.02f}, BERT: {bert_vs:.02f}, SimCSE: {simcse_vs:.02f}") ``` -------------------------------- ### Compute BERT embedding-based Vendi Score for text sentences Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate the Vendi Score for a list of sentences using BERT embeddings. This example utilizes `text_utils.embedding_vendi_score` with a specified BERT model path. ```python from vendi_score import text_utils sents = ["Look, Jane.", "See Spot.", "See Spot run.", "Run, Spot, run.", "Jane sees Spot run."] bert_vs = text_utils.embedding_vendi_score(sents, model_path="bert-base-uncased") ``` -------------------------------- ### Calculate Embedding Diversity Scores Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Calculates diversity scores using embeddings from two SimCSE models (unsupervised and supervised). It iterates through specified models, generates embeddings for each group's examples, and computes the Vendi Score (vNd). ```python device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") models = [("Unsupervised SimCSE", "princeton-nlp/unsup-simcse-roberta-base"), ("Supervised SimCSE", "princeton-nlp/sup-simcse-roberta-base")] for name, path in models: tok = AutoTokenizer.from_pretrained(path, use_fast=True) model = AutoModel.from_pretrained(path).eval().to(device) for group in groups: group.features[name] = X = text_utils.get_embeddings( [e.x for e in group.examples], model=model, tokenizer=tok ) group.metrics[f"{name}/vNd"] = vendi.score_X(X) ``` -------------------------------- ### Initialize Tokenizer Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Initializes a tokenizer from the text_utils library. This tokenizer is used for n-gram extraction. ```python tokenizer = text_utils.get_tokenizer() ``` -------------------------------- ### Import Vendi Score modules Source: https://github.com/vertaix/vendi-score/blob/main/examples/molecules.ipynb Imports the core modules from the vendi_score library for use in the subsequent analysis. ```python from vendi_score import vendi, data_utils, molecule_utils ``` -------------------------------- ### Load and group sample molecules Source: https://github.com/vertaix/vendi-score/blob/main/examples/molecules.ipynb Loads sample molecules from specified directories for various generative models and groups them using the data_utils.Group class. Each group is limited to 1500 samples. ```python mol_groups = {} for model in ("aae", "char_rnn", "combinatorial", "hmm", "jtn", "latent_gan", "ngram", "vae"): mol_groups[model] = data_utils.Group(model, molecule_utils.load_molecules( f"../../moses/data/samples/{model}/{model}_3.csv", max_samples=1500 )) ``` -------------------------------- ### Load real molecule data Source: https://github.com/vertaix/vendi-score/blob/main/examples/molecules.ipynb Loads a sample of real molecular data from the MOSES test set and groups it for comparison. ```python mol_groups["real"] = data_utils.Group("real", molecule_utils.load_molecules( f"../../moses/data/test.csv", max_samples=1500 )) ``` -------------------------------- ### Calculate and print Vendi Score and IntDiv Source: https://github.com/vertaix/vendi-score/blob/main/examples/molecules.ipynb Iterates through all loaded molecule groups, calculates the Tanimoto similarity kernel ('morgan'), computes the Vendi Score (VS) and Intrinsic Diversity (IntDiv) for each group, and prints the results. ```python groups = mol_groups.values() for group in groups: group.Ks["morgan"] = K = molecule_utils.get_tanimoto_K([e.x for e in group.examples], fp="morgan") group.metrics["VS"] = vendi.score_K(K) group.metrics["IntDiv"] = vendi.intdiv_K(K) print(group.name, round(group.metrics["VS"], 2), round(group.metrics["IntDiv"], 4)) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Imports all required libraries for data manipulation, plotting, and Vendi Score functionalities. ```python import random from matplotlib import pyplot as plt import numpy as np from scipy.stats import spearmanr from sklearn.preprocessing import normalize import torch from torch import nn from torchvision.models import inception_v3 import torchvision ``` ```python import vendi_score from vendi_score import vendi, image_utils, data_utils ``` -------------------------------- ### Prepare data for mode dropping analysis Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Selects a random subset of categories and prepares them for mode dropping analysis using the data_utils.mode_dropping_groups function. Sets random seed for reproducibility. ```python random.seed(0) cats = random.sample(categories, 10) label_to_examples = {group.name: group.examples for group in groups} mode_dropping_groups = data_utils.mode_dropping_groups(label_to_examples, cats, N=100) ``` -------------------------------- ### Import Vendi Score Utilities Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Imports specific modules from the vendi_score library, including core functionality, data utilities, and text processing tools. ```python from vendi_score import vendi, data_utils, text_utils ``` -------------------------------- ### Set Working Directory Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Changes the current working directory to a specified path. This is often necessary to access project-specific files or data. ```python import os os.chdir("/n/fs/nlp-df22/project/diversity/clean") ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Imports a comprehensive set of libraries required for data manipulation, machine learning, and natural language processing tasks. ```python import importlib import random import datasets from matplotlib import pyplot as plt import numpy as np import scipy.sparse from scipy.stats import spearmanr from scipy.stats import pearsonr from sklearn.feature_extraction.text import CountVectorizer from sklearn.preprocessing import normalize import torch from torch import nn from torchvision.models import inception_v3 import torchvision from transformers import AutoModel, AutoTokenizer ``` -------------------------------- ### Visualize molecular similarity kernels Source: https://github.com/vertaix/vendi-score/blob/main/examples/molecules.ipynb Generates and displays heatmaps of the Tanimoto similarity kernels for selected molecular models (HMM, AAE, Real). It calculates Vendi Score and Intrinsic Diversity for the top 250 molecules based on a 's' feature and saves the plot as a PDF. ```python models = ["hmm", "aae", "real"] fig, axes = plt.subplots(1, 3, figsize=(6.5, 3), constrained_layout=True) models = [("hmm", "HMM"), ("aae", "AAE"), ("real", "Real")] for (model_key, model_name), ax in zip(models, axes): group = mol_groups[model_key] K = group.Ks["morgan"] order = np.argsort(np.array([e.features["s"] for e in group.examples]))[:250] K = K[order][:, order] im = ax.imshow(K, vmin=0, vmax=1, cmap="inferno") #vnd, intdiv = group.metrics["vNd"], group.metrics["IntDiv"] vnd = vendi.score_K(K) intdiv = vendi.intdiv_K(K) ax.set_title(model_name + f"\nVS: {vnd:.01f}" + f"\nIntDiv: {intdiv:.03f}", size=10) ax.set_xticks([]) ax.set_yticks([]) cbar = fig.colorbar(im, ax=axes, location="right", shrink=0.6) cbar.ax.set_ylabel("Similarity", rotation=-90, va="bottom", size=10) plt.savefig("molecule_kernels.pdf", dpi=300) plt.show() ``` -------------------------------- ### Display Diversity Scores in Markdown Table Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Generates and displays a Markdown table summarizing the calculated diversity scores (Vendi Score and embedding-based scores) for each genre. ```python from vendi_score import notebook_utils rows = [] keys = sorted(groups[0].metrics.keys()) for group in groups: row = [f"group.name"] + [f"{group.metrics[k]:.02f}" for k in keys] rows.append(row) notebook_utils.markdown_table(["Genre"] + keys, rows) ``` -------------------------------- ### Compute Vendi Score from samples and similarity function Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate the Vendi Score using a list of samples and a custom similarity function `k`. The similarity function should be symmetric and return 1 for identical inputs. ```python import numpy as np from vendi_score import vendi samples = [0, 0, 10, 10, 20, 20] k = lambda a, b: np.exp(-np.abs(a - b)) vendi.score(samples, k) ``` -------------------------------- ### Compute Vendi Score of order q Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate the Vendi Score for a specific order `q`. Higher orders emphasize common elements more. ```python vendi.score(samples, k, q=1.) ``` -------------------------------- ### Plotting Specific Metrics with Matplotlib Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Generates a two-row plot comparing 'VS' and 'IntDiv' metrics for 'N-grams'. Saves the plot to a PDF file. ```python fig, axes = plt.subplots(2, 1, figsize=(2.75, 2.5), sharex=True) for i, (ax, metric) in enumerate(zip(axes, ("VS", "IntDiv"))): #ax.set_title(metric) for feature in ("N-grams",): ax.plot([g.name for g in mode_dropping_groups], [g.metrics[f"{feature}/{metric}"] for g in mode_dropping_groups], marker="o", color=colors[i], label=metric, markersize=5) #label=feature.title()) xticks = [cats[0]] + [f"+ {c}" for c in cats[1:]] ax.set_xticks([g.name for g in mode_dropping_groups]) #ax.set_xticklabels(xticks, rotation=45, horizontalalignment="right", fontsize=7) ax.set_xticklabels([g.name for g in mode_dropping_groups]) ax.legend() axes[1].set_xlabel("Number of classes") axes[0].set_title("MultiNLI", size=9) plt.tight_layout() #fig.subplots_adjust(top=0.9) plt.savefig("mnli_modes.pdf", dpi=300) plt.show() ``` -------------------------------- ### Generate Inception embeddings for images Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Extracts Inception v3 embeddings from a list of images using a pre-trained model. It utilizes a specified batch size and device (CUDA if available). ```python device = torch.device("cuda") embeddings = image_utils.get_inception_embeddings( [e.x for e in cifar_examples], batch_size=64, device=torch.device("cuda") ) for e, emb in zip(cifar_examples, embeddings): e.features["inception"] = emb ``` -------------------------------- ### Display Diversity Score Ranks in Markdown Table Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Calculates and displays the rank of each genre for each diversity metric in a Markdown table. This helps in comparing genres across different scoring methods. ```python rows = [] keys = sorted(groups[0].metrics.keys()) ranks = {} for key in keys: order = list(np.argsort(-np.array([g.metrics[key] for g in groups]))) ranks[key] = [order.index(i) for i in range(len(order))] for i, group in enumerate(groups): row = [group.name] + [f"{ranks[k][i]}" for k in keys] rows.append(row) notebook_utils.markdown_table(["Genre"] + keys, rows) ``` -------------------------------- ### Calculate N-gram and IntDiv Scores for Mode Dropping Groups Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Calculates n-gram features (binary), Vendi Scores (VS), and Integer Division (IntDiv) scores for groups selected for mode dropping analysis. This focuses on binary n-grams. ```python for group in mode_dropping_groups: sents = [e.x for e in group.examples] for n in (1, 2, 3, 4): X = text_utils.get_ngrams(sents, n=n, tokenizer=tokenizer, binary=True) group.features[f"{n}-grams"] = X = normalize(X, axis=1) group.Ks[f"{n}-grams"] = K = (X @ X.T).toarray() group.metrics[f"{n}-grams/VS"] = vendi.score_K(K) group.metrics[f"{n}-grams/IntDiv"] = vendi.intdiv_K(K) group.Ks[f"N-grams"] = K = np.stack([group.Ks[f"{n}-grams"] for n in (1, 2, 3, 4)], 0).mean(0) group.metrics[f"N-grams/VS"] = vendi.score_K(K) group.metrics[f"N-grams/IntDiv"] = vendi.intdiv_K(K) ``` -------------------------------- ### Plot least and most diverse categories Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Visualizes the least and most diverse categories based on Inception and Pixel similarity scores. It displays sample images from these categories. ```python fig = plt.figure(constrained_layout=True, figsize=(10, 5)) subfigs = fig.subfigures(nrows=2, ncols=1) for row, (feature, subfig) in enumerate(zip(("Inception", "Pixels"), subfigs)): subfig.suptitle(feature.title()) order = np.argsort([g.metrics[f"{feature} vNd"] for g in groups]) left, right = subfig.subfigures(nrows=1, ncols=2) left.suptitle("Least diverse") axs = left.subplots(nrows=1, ncols=3) for col, ax in enumerate(axs): group = groups[order[col]] ax.set_title(group.name) image_utils.plot_images([e.x for e in group.examples][:100], cols=10, ax=ax) right.suptitle("Most diverse") axs = right.subplots(nrows=1, ncols=3) for col, ax in enumerate(axs): group = groups[order[-3 + col]] ax.set_title(group.name) image_utils.plot_images([e.x for e in group.examples][:100], cols=10, ax=ax) plt.show() ``` -------------------------------- ### Calculate Vendi Scores and IntDiv for mode dropping groups Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Computes Vendi Score (vNd) and Internal Diversity (IntDiv) for 'inception' and 'pixels' features on selected groups. Normalizes feature vectors before calculating kernel matrices. ```python for group in mode_dropping_groups: for feature in ("inception", "pixels"): X = np.stack([e.features[feature] for e in group.examples], 0) X = normalize(X) group.Ks[feature] = K = X @ X.T group.metrics[f"{feature.title()} vNd"] = vendi.score_K(K) group.metrics[f"{feature.title()} IntDiv"] = vendi.intdiv_K(K) ``` -------------------------------- ### Compute Vendi Score from precomputed similarity matrix Source: https://github.com/vertaix/vendi-score/blob/main/README.md Calculate the Vendi Score directly from a precomputed similarity matrix `K`. ```python import numpy as np from vendi_score import vendi K = np.array([[1.0, 0.9, 0.0], [0.9, 1.0, 0.0], [0.0, 0.0, 1.0]]) vendi.score_K(K) ``` -------------------------------- ### Plotting Multiple Metrics with Matplotlib Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Generates a plot showing different metrics across various groups. Useful for comparing model performance under different conditions. ```python fig, axes = plt.subplots(1, 3, figsize=(16, 4)) for ax, metric in zip(axes, ("N-grams/VS", "N-grams/IntDiv", "N-gram diversity")):#, "Self BLEU"): ax.set_title(metric) ax.plot([g.name for g in mode_dropping_groups], [g.metrics[metric] for g in mode_dropping_groups], marker="o") xticks = [cats[0]] + [f"+ {c}" for c in cats[1:]] ax.set_xticks([g.name for g in mode_dropping_groups]) ax.set_xticklabels(xticks, rotation=45) plt.tight_layout() plt.show() ``` -------------------------------- ### Calculate Vendi Scores for Inception and Pixel features Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Computes the Vendi Score (vNd) for both 'inception' embeddings and 'pixels' features for each category. Requires PyTorch and CUDA for optimal performance. ```python device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") for group in category_groups.values(): for feature in ("inception", "pixels"): X = np.stack([e.features[feature] for e in group.examples], 0) group.metrics[f"{feature.title()} vNd"] = vendi.score_X(X) ``` -------------------------------- ### Configuring Matplotlib Font Sizes Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Sets default font sizes for various Matplotlib elements. Useful for ensuring consistent text scaling across plots. ```python SMALL_SIZE = 5 MEDIUM_SIZE = 7 BIGGER_SIZE = 9 plt.rc('font', size=SMALL_SIZE) # controls default text sizes plt.rc('axes', titlesize=MEDIUM_SIZE) # fontsize of the axes title plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title #plt.rc('font', **{'family': 'serif', 'serif': ['Computer Modern']}) plt.rc('text', usetex=False) ``` -------------------------------- ### Plot Vendi Score and IntDiv across categories Source: https://github.com/vertaix/vendi-score/blob/main/examples/images.ipynb Generates plots comparing Vendi Score (vNd) and Internal Diversity (IntDiv) for Inception and Pixel features across different categories. X-axis labels are customized for clarity. ```python fig, axes = plt.subplots(1, 2, figsize=(8, 4)) for ax, metric in zip(axes, ("vNd", "IntDiv")): ax.set_title(metric) for feature in ("Inception", "Pixels"): ax.plot([g.name for g in mode_dropping_groups], [g.metrics[f"{feature} {metric}"] for g in mode_dropping_groups], marker="o", label=feature.title()) xticks = [cats[0]] + [f"+ {c}" for c in cats[1:]] ax.set_xticks([g.name for g in mode_dropping_groups]) ax.set_xticklabels(xticks, rotation=45) ax.legend() plt.show() ``` -------------------------------- ### Calculate N-gram Diversity for Mode Dropping Groups Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Calculates the overall n-gram diversity for the groups selected for mode dropping analysis using the text_utils library. ```python for group in mode_dropping_groups: sents = [e.x for e in group.examples] group.metrics["N-gram diversity"] = text_utils.ngram_diversity(sents, tokenizer=tokenizer) ``` -------------------------------- ### Calculate N-gram Diversity Scores Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Calculates n-gram features (1 to 4-grams) for each category group, computes the Vendi Score (VS) for each n-gram size and an average 'N-grams' score. ```python for group in groups: sents = [e.x for e in group.examples] for n in (1, 2, 3, 4): X = text_utils.get_ngrams(sents, n=n, tokenizer=tokenizer) group.features[f"{n}-grams"] = X = normalize(X, axis=1) group.Ks[f"{n}-grams"] = K = (X @ X.T).toarray() group.metrics[f"{n}-grams/VS"] = vendi.score_K(K) group.Ks[f"N-grams"] = K = np.stack([group.Ks[f"{n}-grams"] for n in (1, 2, 3, 4)], 0).mean(0) group.metrics[f"N-grams/VS"] = vendi.score_K(K) ``` -------------------------------- ### Calculating Spearman Rank Correlation Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Computes and prints the Spearman rank correlation coefficient and p-value between group names and specified metrics. Useful for assessing monotonic relationships. ```python feature = "N-grams" for metric in ("vNd", "IntDiv"): x = [g.name for g in mode_dropping_groups] y = [g.metrics[f"{feature}/{metric}"] for g in mode_dropping_groups] print(metric, spearmanr(x, y)) ``` -------------------------------- ### Compute Vendi Score using covariance matrix for normalized embeddings Source: https://github.com/vertaix/vendi-score/blob/main/README.md Efficiently compute the Vendi Score using the covariance matrix when dealing with normalized embeddings `X` where the dimension `d` is less than the number of samples `n`. Set `normalize=True` if the rows of `X` are not normalized. ```python vendi.score_dual(X) ``` -------------------------------- ### Calculating Pearson Correlation Source: https://github.com/vertaix/vendi-score/blob/main/examples/text.ipynb Computes and prints the Pearson correlation coefficient and p-value between group names and specified metrics. Useful for assessing linear relationships. ```python feature = "N-grams" for metric in ("vNd", "IntDiv"): x = [g.name for g in mode_dropping_groups] y = [g.metrics[f"{feature}/{metric}"] for g in mode_dropping_groups] print(metric, pearsonr(x, y)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.