Package: vectory
An embedding evaluation toolkitVectory provides a collection of tools totrack and compare embedding versions.Visualizing and registering each experiment is a crucial part of developing successful models. Vectory is a tool designed by and for machine learning engineers to handle embedding experiments with little overhead.Key features:Embedding linage. Keep track of the data and models used to generate embeddings.Compare performance. Compare metrics between different vector spaces.Ease of use. Easy usage through the CLI, Python, and GUI interfaces.Extensibility. Built with extensibility in mind.Persistence. Simple local state persistence using SQLite.Table of ContentsInstallationDemoUsageTroubleshootingLicenseInstallationAll you need for Vectory to run is to install the package and Elasticsearch. You can install the package using pip:pip install vectoryDemoAfter installing Vectory, you can play with the demo cases to get a feel of the toolkit.Tiny-ImageNet: A computer vision dataset set of embeddings made from pretrained models ResNet50 and ConvNext-tiny.IMDB: A NLP dataset set of embeddings made from pretrained models BERT and RoBERTa.To set up the demo, run the following command:vectory demoYou can specify the demo dataset by adding the name as the next argument. Seevectory demo --helpfor more information.Set up ElasticsearchWhat is Elasticsearch? It's a free high-performance search engine used for many types of data.Vectory uses Elasticsearch to load embeddings and then search for them.To start the engine, you must install Docker and start its daemon.
After that, just run:vectory elastic up --detachAnd you can turn it off with:vectory elastic downUsageThe key concepts needed to use Vectory aredatasets,experimentsandembedding spaces.Adatasetis just a collection of data. You could have evaluation or training datasets. Evaluation datasets are required for Vectory to run, whereas training datasets are optional and desired for tracking purposes.You will need a CSV file to define a Datasets. The CSV file must have a header row, followed by a row for each data point in the dataset. The only requirement we ask of the CSV is to have at least an identifier column. The following columns could be labels, features, or any other information.Anexperimentis a machine learning model trained with a particular dataset. You can create different experiments by varying the model and the dataset. You can optionally specify a training dataset for tracking purposes.A Dataset and an Experiment form anembedding space, which is just a 2-dimensional array with all the generated vectors (namely, features or embeddings) for a particular dataset given an experiment. You will need to provide the embeddings in a file that can be either.npzor.npy.The important thing about these embedding files is that they must follow the same indexing as the evaluation dataset CSV file. To summarize, for every line in the dataset, there's an embedding in the.npzfile.ExampleYou can have an experiment, such as a ResNet model trained with the dataset Data1. Let’s call the generated embedding space ES1. But either you split your data or you get new data once in a while (or both), so this experiment will not only be used in a static dataset. You might want to use this experiment on Data2 then, generating a particular embedding space called ES2.Vectory helps you to organize and analyze the obtained embeddings for each dataset and experiment.Command Line InterfaceCreateCreate datasets, experiments, and embedding spaces:vectory add --dataset [path_to_csv] --embeddings [path_to_npz]You can see all the options with the--helpflag.LoadEmbedding Spaces are links to ElasticSearchindices. To load the embeddings to ElasticSearch when creating the Embedding Space, add--loadafter setting the dataset, the Embedding Space, and the parameters. This option for theaddcommand only works for the default loading options. You can use the load command to load the embeddings with different options.To separately load an Embedding Space to ElasticSearch:vectory embeddings load [index_name] [embedding_space_name]You can specify the model name, the similarity function, the number of threads, the chunk size, and the hyperparameters for the kNN search. You can see all the options with the--helpflag.SearchList allyour datasets, experiments, embedding spaces, and indices:vectory lsList the indexes:vectory embeddings list-indicesDeleteDelete datasets:vectory dataset delete [dataset_name]Experiments:vectory experiment delete [experiment_name]Embedding Spaces:vectory embeddings delete [embedding_space_name]You can delete elements associated with these objects and their respective indices by adding the--recursiveflag.Indices:vectory embeddings delete-index [index_name]All indices:vectory embeddings delete-all-indicesComparing embedding spacesWith Vectory you can measure how similar two embedding spaces are. The similarity between two embedding spaces is the mean of the local neighborhood similarity of every point, which is the IoU of the ten nearest neighbors.To compare two embedding spaces, Vectory computes the ten nearest neighbors for every data point for both embedding spaces, getting the IoU for each group of ten nearest neighbors obtained. Then, it shows the distribution of the IoU values. Also, we compute the mean of the IoU values to provide a single value to compare the two embedding spaces.To learn more about comparing embedding spaces, check outthis embedding-comparatorarticle.To compare two embedding spaces, use:vectory compare [embedding_space_1_name] [embedding_space_2_name] --precomputeYou can specify the metric for the kNN search in each embedding space. You can also calculate the similarity histogram.Python APICreateCreate datasets, experiments, and an embedding space.fromvectory.datasetsimportDatasetfromvectory.experimentsimportExperimentfromvectory.spacesimportEmbeddingSpacedataset=Dataset.get_or_create(csv_path=CSV_PATH,name=DATASET_NAME)train_dataset=Dataset.get_or_create(csv_path=TRAIN_CSV_PATH,name=TRAIN_DATASET_NAME)experiment=Experiment.get_or_create(train_dataset=TRAIN_DATASET_NAME,model=MODEL_NAME,name=EXPERIMENT_NAME,)embedding_space=EmbeddingSpace.get_or_create(npz_path=NPZ_PATH,dims=EMBEDDINGS_DIMENSIONS,experiment=EXPERIMENT_NAME,dataset=DATASET_NAME,name=EMBEDDING_SPACE_NAME,)Thetrain_datasetparameter is optional, but we recommend to track the training process.Load an index on ElasticSearch for an embedding space:fromvectory.indicesimportload_indexload_index(index_name=INDEX_NAME,embedding_space_name=EMBEDDING_SPACE_NAME,)You can get the names ofdataset,experiment, andembedding_spaceobjects usingmodel.name.Additionally, you can specify the desired mapping to load the index. You can choose the mapping to usecosineoreuclideansimilarity for the kNN search. Searching will be slower but more accurate when using anexactmodel instead of thelsh. Thelshmodel and thecosinesimilarity are the default options. To see all the available mappings, check the possible options fromvectory.es.api.Mapping.SearchGet all your datasets, experiments, embedding spaces, and indices:fromvectory.db.modelsimport(DatasetModel,ElasticSearchIndexModel,EmbeddingSpaceModel,ExperimentModel,Query,)datasets=Query(DatasetModel).get()experiments=Query(ExperimentModel).get()spaces=Query(EmbeddingSpaceModel).get()indices=Query(ElasticSearchIndexModel).get()You can also get a specific dataset, experiment, space, or index by specifying an attribute:dataset=Query(DatasetModel).get(name=DATASET_NAME)[0]DeleteDelete old datasets and their indices:fromvectory.db.modelsimportDatasetModel,Querydataset=Query(DatasetModel).get(name=DATASET_NAME)[0]dataset.delete_instance(recursive=True)Setting therecursiveoption toTruedeletes the experiments, spaces, and indices associated with the dataset.The same can be done for experiments, embedding spaces and indices by using thedelete_instancemethod on the correct object.CompareWith Vectory you can measure how similar two embedding spaces are. The similarity between two embedding spaces is the mean of the local neighborhood similarity of every point, which is the IoU of the ten nearest neighbors.Compare two embedding spaces:fromvectory.spacesimportcompare_embedding_spacessimilarity,_,fig,_=compare_embedding_spaces(embedding_space_a=EMBEDDING_SPACE_NAME_1,embedding_space_b=EMBEDDING_SPACE_NAME_2,metric_a=METRIC_A,metric_b=METRIC_B,allow_precompute_knn=True,)Themetric_aandmetric_bparameters are eithereuclideanorcosine. Theallow_precompute_knnparameter is set toTrueto allow precomputing the bulk operations for the similarity computation.Thespaces_similarityvariable contains the similarity between the two embedding spaces. Theid_similarity_dictvariable has the similarity scores for every point in the embedding spaces.Setting thehistogramparameter toTruein thecompare_embedding_spacesfunction will show a histogram of the similarity scores. Thefigandaxvariables are the figure and axis of the histogram.Reduce dimensionalityReduce the dimensionality to 2D of an embedding space:fromvectory.visualization.utilsimportcalculate_points,get_index# Get the embedding space dataembeddings,rows,index=get_index(EMBEDDING_SPACE_NAME,model=MODEL,similarity=SIMILARITY_METHOD)# Reduce the dimensionalitydf=calculate_points(DIMENSIONAL_REDUCTION_MODEL,embeddings,rows)Thecalculate_pointsfunction reduces the dimensionality of the embeddings using theDIMENSIONAL_REDUCTION_MODELmodel. It can be eitherUMAP,PCA, orPCA +UMAP`. It returns a DataFrame with the reduced dimensionality points and the data contained in the dataset's CSV file.Get similar indicesGet the most similar indices for a given embedding:fromvectory.indicesimportmatch_query# Get the most similar indices for a sample embeddingsimilarity_results,_=match_query(indices_name=[INDEX_NAME],query_id=EMBEDDING_INDEX)Thematch_queryfunction returns the most similar indices for a given embedding and the index of the embedding. Theindices_nameparameter is a list of indices names, and thequery_idparameter is the id of the embedding to search. You can get the most similar indices and their scores from these results. Thesimilarity_resultsvariable contains a dictionary with the indices' names as keys and a list of tuples with the most similar indices and their scores as values.VisualizationOnce you have loaded your datasets, experiments, and embedding spaces, you can analyze the results by visualizing them on our Streamlit app or by following the Python API documentation and getting the indices.StreamlitVisualize your embedding spaces on a local Streamlit app:vectory runThe GUI dependencies are required to view the Streamlit app.LicenseThis project is licensed under the terms of the MIT license.