### Install SetFit, Evaluate, and Matplotlib Source: https://github.com/huggingface/setfit/blob/main/notebooks/setfit-optimum-intel.ipynb Installs the SetFit library, the evaluate package for metrics, and matplotlib for visualization. Use -qqq for quiet installation. ```python !python -m pip install -U setfit evaluate matplotlib -qqq ``` -------------------------------- ### Install Optuna Backend Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/how_to/hyperparameter_optimization.mdx Install the Optuna library to enable hyperparameter optimization capabilities for SetFit. ```bash pip install optuna ``` -------------------------------- ### Install Watchdog for Previewing Source: https://github.com/huggingface/setfit/blob/main/docs/README.md Installs the watchdog module, which is required for previewing the documentation locally. ```bash python -m pip install watchdog ``` -------------------------------- ### Install SetFit and Optuna Source: https://github.com/huggingface/setfit/blob/main/notebooks/text-classification_hyperparameter-search.ipynb Install the necessary libraries for SetFit and hyperparameter optimization. This command installs SetFit with Optuna support and Matplotlib for visualization. ```python # %pip install setfit[optuna] matplotlib ``` -------------------------------- ### Install SetFit and Matplotlib Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/tutorials/zero_shot.mdx Install the necessary libraries for running the tutorial. This is typically done in a Colab or cloud environment. ```python # %pip install setfit matplotlib ``` -------------------------------- ### Install from PyPI Test Server Source: https://github.com/huggingface/setfit/blob/main/RELEASE.md Install the package from the PyPI test server in a fresh environment to verify the upload. A successful installation should result in a confirmation message. ```bash make pypi_test_install ``` -------------------------------- ### Install SetFit Library Source: https://github.com/huggingface/setfit/blob/main/notebooks/text-classification.ipynb Install the SetFit library using pip. This is typically the first step when setting up the environment. ```python # %pip install setfit ``` -------------------------------- ### Install SetFit with ABSA support Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/how_to/absa.mdx Install the SetFit library with ABSA capabilities. Alternatively, install spaCy separately if needed. ```bash !pip install "setfit[absa]" ``` ```bash # or # !pip install spacy ``` -------------------------------- ### Install SetFit Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb Installs the SetFit library and its dependencies. This is the first step to start using SetFit for fine-tuning Sentence Transformers. ```bash pip install setfit ``` -------------------------------- ### Install Dependencies Source: https://github.com/huggingface/setfit/blob/main/scripts/transformers/README.md Install the SetFit library and other required dependencies from the requirements file. ```bash python -m pip install setfit python -m pip install -r requirements.txt ``` -------------------------------- ### Example HPO Trial Log (Part 2) Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/how_to/hyperparameter_optimization.mdx This log snippet shows the completion of the first trial, including its final value and parameters, and the start of the second trial with its corresponding hyperparameters. ```text [I 2023-11-14 20:37:33,895] Trial 0 finished with value: 0.44 and parameters: {'body_learning_rate': 1.937397586885703e-06, 'num_epochs': 3, 'batch_size': 32, 'seed': 16, 'max_iter': 223, 'solver': 'newton-cg'}. Best is trial 0 with value: 0.44. Trial: {'body_learning_rate': 0.000946449838705604, 'num_epochs': 2, 'batch_size': 16, 'seed': 8, 'max_iter': 60, 'solver': 'liblinear'} model_head.pkl not found on HuggingFace Hub, initialising classification head with random weights. You should TRAIN this model on a downstream task to use it for predictions and inference. ***** Running training ***** Num examples = 120 Num epochs = 2 Total optimization steps = 240 Total train batch size = 16 ``` -------------------------------- ### Install SetFit Source: https://github.com/huggingface/setfit/blob/main/notebooks/setfit-absa-fiqa.ipynb Installs the Setfit library and its dependencies. This is the first step to start using Setfit for your NLP tasks. ```bash pip install setfit ``` -------------------------------- ### Install SetFit Source: https://github.com/huggingface/setfit/blob/main/README.md Install the setfit library using pip. For the latest development version, install from source. ```bash pip install setfit ``` ```bash pip install git+https://github.com/huggingface/setfit.git ``` -------------------------------- ### Install SetFit and Accelerate Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/tutorials/onnx.mdx Installs the necessary packages for SetFit and acceleration. Use this to set up your development environment. ```bash !pip install setfit accelerate -qqq ``` -------------------------------- ### Setup ADAPET Environment Source: https://github.com/huggingface/setfit/blob/main/scripts/adapet/ADAPET/README.md Installs necessary dependencies and PyTorch with CUDA support. Ensure you are using Python 3.6.8. ```bash python3.6 -m venv venv source mvenv/bin/activate pip install -r requirements.txt pip install torch==1.9.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html ``` -------------------------------- ### Install necessary libraries Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb Installs the required libraries for the project, including `setfit`, `transformers`, and `datasets`. ```python pip install setfit transformers datasets accelerate ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/huggingface/setfit/blob/main/docs/README.md Installs the necessary packages to build the documentation from the root of the code repository. ```bash python -m pip install -e ".[docs]" ``` -------------------------------- ### Install necessary libraries Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb Installs the required libraries for the project, including SetFit, Transformers, and PyTorch. Ensure you have a compatible environment. ```bash pip install -U setfit transformers torch ``` -------------------------------- ### Install SetFit Library Source: https://github.com/huggingface/setfit/blob/main/notebooks/multilabel_HoC.ipynb Installs the SetFit library. This is a prerequisite for using SetFit models. ```python !pip install setfit ``` -------------------------------- ### Exhaustive SetFit Training Example Source: https://github.com/huggingface/setfit/blob/main/scripts/setfit/README.md An example demonstrating all argument options and their default values for SetFit training. Supports running on multiple datasets and sample sizes. ```bash python run_fewshot.py \ --model paraphrase-mpnet-base-v2 \ --datasets sst2 ag_news bbc-news \ --sample_sizes 8 64 \ --num_epochs 1 \ --num_iterations 20 \ --batch_size 16 \ --max_seq_length 256 \ --classifier logistic_regression \ --loss CosineSimilarityLoss \ --exp_name "" \ --add_normalization_layer \ ``` -------------------------------- ### Example: Transcribing Speech with Wav2Vec2 Source: https://github.com/huggingface/setfit/blob/main/docs/README.md This Python example demonstrates how to use the Wav2Vec2 model and processor from the Transformers library to transcribe speech from an audio dataset. Ensure you have the 'transformers' and 'datasets' libraries installed. ```python >>> from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC >>> from datasets import load_dataset >>> import torch >>> dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation") >>> dataset = dataset.sort("id") >>> sampling_rate = dataset.features["audio"].sampling_rate >>> processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h") >>> model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h") >>> # audio file is decoded on the fly >>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt") >>> with torch.no_grad(): ... logits = model(**inputs).logits >>> predicted_ids = torch.argmax(logits, dim=-1) >>> # transcribe speech >>> transcription = processor.batch_decode(predicted_ids) >>> transcription[0] 'MISTER QUILTER IS THE APOSTLE OF THE MIDDLE CLASSES AND WE ARE GLAD TO WELCOME HIS GOSPEL' ``` -------------------------------- ### Install SetFit and Transformers Source: https://github.com/huggingface/setfit/blob/main/Zero_cost_Zero_time_Zero_shot_Financial_Sentiment_Analysis.ipynb Install the necessary libraries for SetFit and Transformers. Ensure you have a compatible Python environment. ```bash pip install setfit transformers datasets ``` -------------------------------- ### Install necessary libraries Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb Installs the required libraries for running the SetFit model and performing sentiment analysis. ```bash pip install -U sentence-transformers datasets transformers pip install -U setfit ``` -------------------------------- ### Install SetFit with OpenVINO support Source: https://github.com/huggingface/setfit/blob/main/notebooks/openvino_inference.ipynb Install the SetFit library with OpenVINO support. This is necessary if you are running the notebook on a cloud platform like Colab. ```python #%pip install setfit[openvino] ipywidgets widgetsnbextension ``` -------------------------------- ### Start T-Few Environment Session Source: https://github.com/huggingface/setfit/blob/main/scripts/tfew/README.md Source the start script to set up environment variables for T-Few, including PYTHONPATH, OUTPUT_PATH, CONFIG_PATH, and CUDA_VISIBLE_DEVICES. ```bash cd scripts/tfew . t-few/bin/start.sh ``` -------------------------------- ### Install SetFit and Transformers Source: https://github.com/huggingface/setfit/blob/main/Zero_cost_Zero_time_Zero_shot_Financial_Sentiment_Analysis.ipynb Installs the necessary libraries for SetFit and Transformers. Ensure you have a compatible Python environment. ```bash pip install setfit transformers ``` -------------------------------- ### Install SetFit Package Source: https://github.com/huggingface/setfit/blob/main/scripts/setfit/README.md Install the SetFit library using pip within your activated virtual environment. ```bash python -m pip install setfit ``` -------------------------------- ### Install PyTorch with CUDA Support Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/installation.mdx Recommended for users with a CUDA-capable GPU to accelerate training and inference. Ensure you follow the official PyTorch installation guide for your specific CUDA version. ```bash pip install torch --index-url https://download.pytorch.org/whl/cu118 ``` -------------------------------- ### Clone PERFECT Repository and Install Dependencies Source: https://github.com/huggingface/setfit/blob/main/scripts/perfect/README.md Clone the SetFit fork of the PERFECT codebase and install its dependencies. This includes setting up the Python environment and installing PyTorch with CUDA support. ```bash git clone git+https://github.com/SetFit/perfect.git cd perfect python setup.py develop conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch python -m pip install -r requirements.txt ``` -------------------------------- ### Install SetFit and Transformers Source: https://github.com/huggingface/setfit/blob/main/Zero_cost_Zero_time_Zero_shot_Financial_Sentiment_Analysis.ipynb Installs the necessary libraries for using SetFit and Transformers. Ensure you have a compatible Python environment. ```bash pip install setfit transformers datasets evaluate accelerate ``` -------------------------------- ### Install Dependencies for Financial Sentiment Analysis Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb Installs the necessary libraries for loading datasets and calculating evaluation metrics. Ensure these are installed before running the analysis. ```python !pip install datasets==2.18.0 # for loading the example dataset !pip install scikit-learn # for evaluation metrics ``` -------------------------------- ### Install necessary libraries Source: https://github.com/huggingface/setfit/blob/main/Zero_cost_Zero_time_Zero_shot_Financial_Sentiment_Analysis.ipynb Installs required Python packages for data loading, evaluation metrics, and the SetFit model. Run these commands in your environment before proceeding. ```python !pip install datasets # for loading the example dataset !pip install scikit-learn # for evaluation metrics %pip install setfit matplotlib ``` -------------------------------- ### Install Hugging Face Hub Source: https://github.com/huggingface/setfit/blob/main/Zero_cost_Zero_time_Zero_shot_Financial_Sentiment_Analysis.ipynb Installs the huggingface_hub library, which is necessary for interacting with the Hugging Face Hub, including logging in and uploading models. ```python !pip install huggingface_hub ``` -------------------------------- ### Install Base Requirements Source: https://github.com/huggingface/setfit/blob/main/README.md Install the project's base requirements, including development packages, in editable mode. ```bash pip install -e '.[dev]' ``` -------------------------------- ### View Synthetic Examples Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/tutorials/zero_shot.mdx Display a few shuffled examples from the generated synthetic dataset to inspect their format and labels. ```python train_dataset.shuffle()[:3] ``` -------------------------------- ### Install SetFit with Pip Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/installation.mdx Use this command to install the latest stable version of SetFit via pip. ```bash pip install setfit ``` -------------------------------- ### Install Latest SetFit from Source (Bleeding-Edge) Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/installation.mdx Install the most recent version directly from the GitHub repository without cloning. This provides the latest features but may be less stable. ```bash pip install git+https://github.com/huggingface/setfit.git ``` -------------------------------- ### Create Conda Environment and Install JupyterLab Source: https://github.com/huggingface/setfit/blob/main/notebooks/setfit-optimum-intel.ipynb Sets up a new conda environment named 'setfit' with Python 3.10 and installs JupyterLab. ```bash conda create -n setfit python=3.10 -y && conda activate setfit python -m pip install -U jupyterlab ``` -------------------------------- ### Install Setfit with ONNX support Source: https://github.com/huggingface/setfit/blob/main/notebooks/onnx_model_export.ipynb Install the Setfit library with ONNX dependencies. This is required for exporting models to ONNX format. ```python # %pip install setfit[onnx] ``` -------------------------------- ### Install Optimum with ONNX Runtime GPU Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/tutorials/onnx.mdx Installs the Optimum library with ONNX Runtime GPU support, essential for accelerating ONNX models on NVIDIA GPUs. ```bash !pip install optimum[onnxruntime-gpu] -qqq ``` -------------------------------- ### Install SetFit from Source (Editable Mode) Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/installation.mdx Clone the repository and install SetFit in editable mode to make code changes. This is useful for development or contributing to the project. ```bash git clone https://github.com/huggingface/setfit.git cd setfit pip install -e . ``` -------------------------------- ### Example Download Progress Output Source: https://github.com/huggingface/setfit/blob/main/notebooks/text-classification_hyperparameter-search.ipynb This output shows the progress of downloading model components from the HuggingFace Hub. It is typical during model loading or initialization. ```text Result:\nDownloading (…)lve/main/config.json: 0%| | 0.00/594 [00:00 {'accuracy': 0.8511806699615596} # Saving the trained model model.save_pretrained("setfit-bge-small-v1.5-sst2-8-shot") # or model.push_to_hub("tomaarsen/setfit-bge-small-v1.5-sst2-8-shot") # Loading a trained model model = SetFitModel.from_pretrained("tomaarsen/setfit-bge-small-v1.5-sst2-8-shot") # Load from the Hugging Face Hub # or model = SetFitModel.from_pretrained("setfit-bge-small-v1.5-sst2-8-shot") # Load from a local directory # Performing inference preds = model.predict([ "It's a charming and often affecting journey.", "It's slow -- very, very slow.", "A sometimes tedious film.", ]) print(preds) # => ["positive", "negative", "negative"] ``` -------------------------------- ### Example HPO Trial Log (Part 1) Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/how_to/hyperparameter_optimization.mdx This log snippet shows the initialization of a new study and the parameters for the first trial, followed by the training process output including embedding loss and learning rate metrics. ```text [I 2023-11-14 20:36:55,736] A new study created in memory with name: no-name-d9c6ec29-c5d8-48a2-8f09-299b1f3740f1 Trial: {'body_learning_rate': 1.937397586885703e-06, 'num_epochs': 3, 'batch_size': 32, 'seed': 16, 'max_iter': 223, 'solver': 'newton-cg'} model_head.pkl not found on HuggingFace Hub, initialising classification head with random weights. You should TRAIN this model on a downstream task to use it for predictions and inference. ***** Running training ***** Num examples = 60 Num epochs = 3 Total optimization steps = 180 Total train batch size = 32 {'embedding_loss': 0.26, 'learning_rate': 1.0763319927142795e-07, 'epoch': 0.02} {'embedding_loss': 0.2069, 'learning_rate': 1.5547017672539594e-06, 'epoch': 0.83} {'embedding_loss': 0.2145, 'learning_rate': 9.567395490793595e-07, 'epoch': 1.67} {'embedding_loss': 0.2236, 'learning_rate': 3.587773309047598e-07, 'epoch': 2.5} {'train_runtime': 36.1299, 'train_samples_per_second': 159.425, 'train_steps_per_second': 4.982, 'epoch': 3.0} 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 180/180 [00:29<00:00, 6.02it/s] ***** Running evaluation ***** ``` -------------------------------- ### Install SetFit with Conda Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/installation.mdx Install SetFit using the conda package manager from the conda-forge channel. ```bash conda install -c conda-forge setfit ``` -------------------------------- ### Load Model for Fine-tuning (Example) Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb This snippet shows how to load a model that is intended for fine-tuning. While this notebook focuses on zero-shot, this demonstrates the initial step if fine-tuning were to be performed. ```python from setfit import SetFitModel # Load a model that can be fine-tuned model_for_finetuning = SetFitModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2") ``` -------------------------------- ### Prepare Dataset and Trainer Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/how_to/hyperparameter_optimization.mdx Load a dataset using `datasets.load_dataset`, sample training data, and prepare the test dataset. Then, instantiate the `Trainer` with the training and evaluation datasets, and the `model_init` function. ```python from datasets import load_dataset from setfit import Trainer, sample_dataset dataset = load_dataset("SetFit/emotion") train_dataset = sample_dataset(dataset["train"], label_column="label", num_samples=8) test_dataset = dataset["test"] ``` ```python trainer = Trainer( train_dataset=train_dataset, eval_dataset=test_dataset, model_init=model_init, ) ``` -------------------------------- ### Train a SetFit Model from Scratch Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb Demonstrates how to train a SetFit model from scratch using a custom dataset. This involves preparing the data and using the SetFitTrainer. ```python from setfit import SetFitTrainer from sentence_transformers import SentenceTransformer from datasets import Dataset # Dummy data for demonstration data = { "text": ["This is a positive sentence.", "This is a negative sentence."], "label": [1, 0] } dataset = Dataset.from_dict(data) # Load a sentence transformer model model_name = "sentence-transformers/all-MiniLM-L6-v2" model = SentenceTransformer(model_name) # Initialize the trainer trainer = SetFitTrainer(model=model, train_dataset=dataset) # Train the model trainer.train() # Save the trained model trainer.save_model("./my_finetuned_model") ``` -------------------------------- ### Example Predicted Labels Output Source: https://github.com/huggingface/setfit/blob/main/notebooks/text-classification_multilabel.ipynb An example of the expected output format for predicted labels in a multilabel classification scenario. ```python [ ['religion'], ['directed_vs_generalized'] ] ``` -------------------------------- ### SetFit Model Configuration and Training Source: https://github.com/huggingface/setfit/blob/main/Zero_cost_Zero_time_Zero_shot_Financial_Sentiment_Analysis.ipynb Demonstrates the configuration and training process for a SetFit model. This includes setting up the trainer with a model and dataset. ```python from setfit import SetFitTrainer from datasets import load_dataset # Load a pre-trained model model = SetFitModel.from_pretrained("ProsusAI/finbert") # Load a dataset (example: financial news headlines) dataset = load_dataset("financial_phrasebank", "sentences") # Initialize the trainer trainer = SetFitTrainer( model=model, train_dataset=dataset["train"], eval_dataset=dataset["test"], loss_class=None, batch_size=16, num_iterations=20, num_epochs=1, seed=42, learning_rate=2e-5, weight_decay=0.001, model_save_path="setfit-ft-financial-sentiment", local_dir="setfit-ft-financial-sentiment" ) # Train the model trainer.train() # Save the trained model model.save_pretrained("setfit-ft-financial-sentiment") ``` -------------------------------- ### Fine-tune SetFit Model (Example) Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb This code block demonstrates the fine-tuning process for a SetFit model using a prepared dataset. It includes setting up training arguments and initiating the training. ```python from setfit import SetFitTrainer from transformers import TrainingArguments # Assuming 'model_for_finetuning' and 'train_dataset' are already defined # Define training arguments training_args = TrainingArguments( output_dir="/tmp/setfit-finetuned", num_train_epochs=1, per_device_train_batch_size=16, per_device_eval_batch_size=16, logging_dir="/tmp/setfit-finetuned/logs", logging_steps=10, evaluation_strategy="steps", eval_steps=50, save_strategy="steps", save_steps=50, learning_rate=2e-5, weight_decay=0.01, fp16=True, # Use mixed precision if available report_to="none" # Disable reporting to external services ) # Initialize the trainer trainer = SetFitTrainer( model=model_for_finetuning, args=training_args, train_dataset=train_dataset, # Use the prepared training dataset eval_dataset=test_dataset, # Use the prepared testing dataset # You can also specify metric_for_best_model if needed ) # Start training trainer.train() # Save the fine-tuned model trainer.save_model("./fine-tuned-financial-sentiment-model") ``` -------------------------------- ### Clone T-Few Fork and Install Dependencies Source: https://github.com/huggingface/setfit/blob/main/scripts/tfew/README.md Clone the T-Few repository and install necessary Python packages, including PyTorch with CUDA support. ```bash cd scripts/tfew git clone https://github.com/SetFit/t-few.git python -m pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu113 ``` -------------------------------- ### Import necessary libraries and setup logging Source: https://github.com/huggingface/setfit/blob/main/notebooks/setfit-optimum-intel.ipynb Imports essential libraries for model benchmarking, including system utilities, performance counters, PyTorch, Sentence Transformers, and SetFit components. Configures logging and disables parallel tokenization for consistency. ```python import sys import os from time import sleep from pathlib import Path from time import perf_counter import warnings import subprocess import sys from collections import defaultdict import evaluate import numpy as np import torch from tqdm.auto import trange from sentence_transformers import SentenceTransformer from setfit.exporters.utils import mean_pooling import logging logging.getLogger("sentence_transformers").setLevel(logging.ERROR) warnings.filterwarnings("ignore") os.environ["TOKENIZERS_PARALLELISM"] = "false" ``` -------------------------------- ### Download and Process Datasets Source: https://github.com/huggingface/setfit/blob/main/scripts/perfect/README.md Download the required datasets, extract them, move them to the correct directory, and process them for few-shot learning. ```bash wget https://nlp.cs.princeton.edu/projects/lm-bff/datasets.tar tar -xvf datasets.tar mv original/ datasets python fewshot/process_datasets.py ``` -------------------------------- ### Run Static Post Training Quantization with Optimum Intel Source: https://github.com/huggingface/setfit/blob/main/notebooks/setfit-optimum-intel.ipynb Defines and runs static post-training quantization using `optimum.intel`. Requires a Setfit model, tokenizer, and a calibration dataset. The quantized model is saved to a specified directory. ```python from optimum.intel import INCQuantizer from neural_compressor.config import PostTrainingQuantConfig setfit_body = setfit_model.model_body[0].auto_model quantizer = INCQuantizer.from_pretrained(setfit_body) optimum_model_path = "/tmp/bge-small-en-v1.5_setfit-sst2-english_opt" quantization_config = PostTrainingQuantConfig(approach="static", backend="ipex", domain="nlp") quantizer.quantize( quantization_config=quantization_config, calibration_dataset=calibration_set, save_directory=optimum_model_path, batch_size=1, ) tokenizer.save_pretrained(optimum_model_path); ``` -------------------------------- ### End-to-End Knowledge Distillation Example Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/how_to/knowledge_distillation.mdx Demonstrates the complete knowledge distillation workflow, including model loading, dataset preparation, teacher model training, and student model training via distillation. ```python from datasets import load_dataset from setfit import sample_dataset # Load a dataset from the Hugging Face Hub dataset = load_dataset("ag_news") # Create a sample few-shot dataset to train with train_dataset = sample_dataset(dataset["train"], label_column="label", num_samples=16) # Dataset({ # features: ['text', 'label'], # num_rows: 64 # }) # Dataset for evaluation eval_dataset = dataset["test"] # Dataset({ # features: ['text', 'label'], # num_rows: 7600 # }) from setfit import SetFitModel, TrainingArguments, Trainer model = SetFitModel.from_pretrained("sentence-transformers/paraphrase-MiniLM-L3-v2") args = TrainingArguments( batch_size=64, num_epochs=5, ) trainer = Trainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=eval_dataset, ) trainer.train() metrics = trainer.evaluate() print(metrics) ``` ```python # Create a dataset of unlabeled examples to perform knowledge distillation unlabeled_train_dataset = dataset["train"].shuffle(seed=0).select(range(500)) unlabeled_train_dataset = unlabeled_train_dataset.remove_columns("label") # Dataset({ # features: ['text'], # num_rows: 500 # }) from setfit import SetFitModel teacher_model = SetFitModel.from_pretrained("sentence-transformers/paraphrase-mpnet-base-v2") from setfit import TrainingArguments, Trainer teacher_args = TrainingArguments( batch_size=16, num_epochs=2, ) teacher_trainer = Trainer( model=teacher_model, args=teacher_args, train_dataset=train_dataset, eval_dataset=eval_dataset, ) # Train teacher model teacher_trainer.train() teacher_metrics = teacher_trainer.evaluate() print(teacher_metrics) ``` ```python from setfit import DistillationTrainer distillation_args = TrainingArguments( batch_size=16, max_steps=500, ) distillation_trainer = DistillationTrainer( teacher_model=teacher_model, student_model=model, args=distillation_args, train_dataset=unlabeled_train_dataset, eval_dataset=eval_dataset, ) # Train student with knowledge distillation distillation_trainer.train() distillation_metrics = distillation_trainer.evaluate() print(distillation_metrics) ``` -------------------------------- ### Financial Sentiment Analysis Example Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb Apply zero-shot classification specifically for financial sentiment analysis. This example uses financial-related categories to classify a given text. ```python from setfit import AutoModelForZeroShotClassification model = AutoModelForZeroShotClassification.from_pretrained("sentence-transformers/all-MiniLM-L6-v2") text = "The company reported record profits this quarter, exceeding all expectations." financial_categories = ["positive sentiment", "negative sentiment", "neutral sentiment"] results = model(text, candidate_labels=financial_categories) print(results) ``` -------------------------------- ### Build Setfit Documentation Source: https://github.com/huggingface/setfit/blob/main/docs/README.md Generates the documentation files using the doc-builder. The --build_dir flag can be adapted to set any preferred temporary folder. ```bash doc-builder build setfit docs/source/en/ --build_dir ./tmp/test-build ``` -------------------------------- ### Initialize Trainer Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/quickstart.mdx Instantiate the `Trainer` with the configured model, training arguments, and the prepared training dataset. ```python from setfit import Trainer trainer = Trainer( model=model, args=args, train_dataset=train_dataset, ) ``` -------------------------------- ### Train SetFit Model on Few Labeled Examples Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero-shot-classification.ipynb Trains a SetFit model using a small set of labeled examples and evaluates it on a test set. This serves as a baseline before data augmentation. ```python model = SetFitModel.from_pretrained(model_id) trainer = SetFitTrainer( model=model, train_dataset=train_dataset, eval_dataset=reference_dataset["test"] ) trainer.train() fewshot_metrics = trainer.evaluate() fewshot_metrics ``` -------------------------------- ### Install TCMalloc and Configure LD_PRELOAD Source: https://github.com/huggingface/setfit/blob/main/notebooks/setfit-optimum-intel.ipynb Installs Google's TCMalloc for improved memory allocation performance and configures the LD_PRELOAD environment variable to use it. Remember to restart your shell or source ~/.bashrc. ```bash conda install gperftools -c conda-forge -y echo export LD_PRELOAD=${CONDA_PREFIX}/lib/libtcmalloc.so:$LD_PRELOAD >> ~/.bashrc # **** Restart the shell or `source ~/.bashrc` **** ``` -------------------------------- ### Initialize SetFitTrainer Source: https://github.com/huggingface/setfit/blob/main/notebooks/text-classification.ipynb Sets up the SetFitTrainer for training. Key arguments include the model, training/evaluation datasets, loss function, number of iterations for contrastive learning, and column mapping for dataset formatting. ```python from sentence_transformers.losses import CosineSimilarityLoss from setfit import SetFitTrainer trainer = SetFitTrainer( model=model, train_dataset=train_dataset, eval_dataset=eval_dataset, loss_class=CosineSimilarityLoss, num_iterations=20, column_mapping={"sentence": "text", "label": "label"}, ) ``` -------------------------------- ### SetFit Few-Shot Learning Example (Python) Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb This example illustrates SetFit's capability in few-shot learning scenarios. It shows how to train a model with a very limited number of labeled examples, demonstrating its efficiency. The code requires the `sentence-transformers` library and a suitable dataset. ```Python from datasets import load_dataset from sentence_transformers import SentenceTransformer, InputExample, losses from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator import random # Load a dataset (e.g., AG News) raw_datasets = load_dataset("ag_news") # Select a small subset for few-shot learning num_examples_per_class = 5 classes = [0, 1, 2, 3] # AG News has 4 classes selected_train_examples = [] for class_id in classes: class_data = [d for d in raw_datasets["train"] if d["label"] == class_id] selected_train_examples.extend(random.sample(class_data, num_examples_per_class)) # Prepare data for SetFit def prepare_fewshot_data(dataset): train_examples = [] for item in dataset: train_examples.append(InputExample(texts=[item["text"], item["text"]], label=float(item["label"]))) return train_examples train_data = prepare_fewshot_data(selected_train_examples) # Load a pre-trained Sentence Transformer model model = SentenceTransformer("all-MiniLM-L6-v2") # Define training parameters train_loss = losses.CosineSimilarityLoss(model=model) # Train the model with few-shot data model.fit(train_sentences=train_data, epochs=5, warmup_steps=int(len(train_data) * 0.1), output_path="output/fewshot_setfit") # Load the trained model model = SentenceTransformer("output/fewshot_setfit") # Example of using the model for inference print(model.encode("This is a news article about technology.")) ``` -------------------------------- ### Train and Evaluate SetFit with Custom Head Source: https://github.com/huggingface/setfit/blob/main/docs/source/en/how_to/classification_heads.mdx Shows the complete workflow for training a SetFit model with a custom head, including dataset preparation, training arguments, trainer initialization, training, and evaluation. This example uses a sampled dataset for training. ```python from setfit import Trainer, TrainingArguments, sample_dataset from datasets import load_dataset # Preparing the dataset dataset = load_dataset("SetFit/sst2") train_dataset = sample_dataset(dataset["train"], label_column="label", num_samples=32) test_dataset = dataset["test"] # Preparing the training arguments args = TrainingArguments( batch_size=32, num_epochs=3, ) # Preparing the trainer trainer = Trainer( model=model, args=args, train_dataset=train_dataset, ) trainer.train() # Evaluating metrics = trainer.evaluate(test_dataset) print(metrics) # => {'accuracy': 0.8638110928061504} ``` -------------------------------- ### SetFit Model Configuration Source: https://github.com/huggingface/setfit/blob/main/Zero_cost_Zero_time_Zero_shot_Financial_Sentiment_Analysis.ipynb Demonstrates how to configure SetFit model parameters, such as the number of epochs and batch size, during training. Adjust these parameters to optimize performance. ```python from setfit import SetFitModel, SetFitTrainer from datasets import load_dataset # Load a pre-trained model model = SetFitModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2") # Load a dataset (example: IMDB dataset) data = load_dataset("imdb", split="train") data = data.map(lambda x: {"text": x["text"], "label": x["label"]}) # Initialize trainer with custom parameters trainer = SetFitTrainer( model=model, train_dataset=data, eval_dataset=data, num_epochs=5, # Number of training epochs batch_size=16, # Batch size for training loss_class=None, metric="accuracy" ) # Train the model trainer.train() # Save the model trainer.save_model("my-configured-setfit-model") ``` -------------------------------- ### Install SetFit with ABSA Option Source: https://github.com/huggingface/setfit/blob/main/notebooks/setfit-absa-fiqa.ipynb Installs the SetFit library with the ABSA (Aspect-Based Sentiment Analysis) option enabled. This command also ensures that necessary dependencies for ABSA functionalities are included. It's a prerequisite for using SetFit for ABSA tasks. ```bash !pip install -U "setfit[absa]" ``` -------------------------------- ### SetFit Evaluation Output Source: https://github.com/huggingface/setfit/blob/main/notebooks/zero_cost_zero_time_zero_shot_financial_sentiment_analysis.ipynb This output indicates the start of the evaluation process for the SetFit model. ```text ***** Running evaluation ***** ```