SetFit documentation
Main Classes
Main Classes
SetFitModel
class setfit.SetFitModel
< source >( model_body: typing.Optional[sentence_transformers.SentenceTransformer.SentenceTransformer] = None model_head: typing.Union[setfit.modeling.SetFitHead, sklearn.linear_model._logistic.LogisticRegression, NoneType] = None multi_target_strategy: typing.Optional[str] = None normalize_embeddings: bool = False labels: typing.Optional[typing.List[str]] = None model_card_data: typing.Optional[setfit.model_card.SetFitModelCardData] = <factory> )
A SetFit model with integration to the Hugging Face Hub.
Example:
>>> from setfit import SetFitModel
>>> model = SetFitModel.from_pretrained("tomaarsen/setfit-bge-small-v1.5-sst2-8-shot")
>>> model.predict([
... "It's a charming and often affecting journey.",
... "It's slow -- very, very slow.",
... "A sometimes tedious film.",
... ])
['positive', 'negative', 'negative']
from_pretrained
< source >( pretrained_model_name_or_path: typing.Union[str, pathlib.Path] force_download: bool = False resume_download: bool = False proxies: typing.Optional[typing.Dict] = None token: typing.Union[str, bool, NoneType] = None cache_dir: typing.Union[str, pathlib.Path, NoneType] = None local_files_only: bool = False revision: typing.Optional[str] = None **model_kwargs )
Parameters
- pretrained_model_name_or_path (str, Path) —
- Either the model_id (string) of a model hosted on the Hub, e.g. bigscience/bloom.
- Or a path to a directory containing model weights saved using [~transformers.PreTrainedModel.save_pretrained], e.g., ../path/to/my_model_directory/.
- revision (str, optional) — Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the latest commit on main branch.
- force_download (bool, optional, defaults to False) — Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding the existing cache.
- resume_download (bool, optional, defaults to False) — Whether to delete incompletely received files. Will attempt to resume the download if such a file exists.
- proxies (Dict[str, str], optional) — A dictionary of proxy servers to use by protocol or endpoint, e.g., {‘http’: ‘foo.bar:3128’, ‘http://hostname’: ‘foo.bar:4012’}. The proxies are used on every request.
- token (str or bool, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when running huggingface-cli login.
- cache_dir (str, Path, optional) — Path to the folder where cached files are stored.
- local_files_only (bool, optional, defaults to False) — If True, avoid downloading the file and return the path to the local cached file if it exists.
- labels (List[str], optional) — If the labels are integers ranging from 0 to num_classes-1, then these labels indicate the corresponding labels.
- model_card_data (SetFitModelCardData, optional) — A SetFitModelCardData instance storing data such as model language, license, dataset name, etc. to be used in the automatically generated model cards.
- multi_target_strategy (str, optional) — The strategy to use with multi-label classification. One of “one-vs-rest”, “multi-output”, or “classifier-chain”.
- use_differentiable_head (bool, optional) — Whether to load SetFit using a differentiable (i.e., Torch) head instead of Logistic Regression.
- normalize_embeddings (bool, optional) — Whether to apply normalization on the embeddings produced by the Sentence Transformer body.
- device (Union[torch.device, str], optional) — The device on which to load the SetFit model, e.g. “cuda:0”, “mps” or torch.device(“cuda”).
Download a model from the Huggingface Hub and instantiate it.
save_pretrained
< source >( save_directory: typing.Union[str, pathlib.Path] config: typing.Optional[dict] = None repo_id: typing.Optional[str] = None push_to_hub: bool = False **kwargs )
Parameters
- save_directory (
str
orPath
) — Path to directory in which the model weights and configuration will be saved. - config (
dict
, optional) — Model configuration specified as a key/value dictionary. - push_to_hub (
bool
, optional, defaults toFalse
) — Whether or not to push your model to the Huggingface Hub after saving it. - repo_id (
str
, optional) — ID of your repository on the Hub. Used only ifpush_to_hub=True
. Will default to the folder name if not provided. kwargs — Additional key word arguments passed along to theSetFitModel.push_to_hub()
method.
Save weights in local directory.
push_to_hub
< source >( repo_id: str config: typing.Optional[dict] = None commit_message: str = 'Push model using huggingface_hub.' private: bool = False api_endpoint: typing.Optional[str] = None token: typing.Optional[str] = None branch: typing.Optional[str] = None create_pr: typing.Optional[bool] = None allow_patterns: typing.Union[str, typing.List[str], NoneType] = None ignore_patterns: typing.Union[str, typing.List[str], NoneType] = None delete_patterns: typing.Union[str, typing.List[str], NoneType] = None )
Parameters
- repo_id (
str
) — ID of the repository to push to (example:"username/my-model"
). - config (
dict
, optional) — Configuration object to be saved alongside the model weights. - commit_message (
str
, optional) — Message to commit while pushing. - private (
bool
, optional, defaults toFalse
) — Whether the repository created should be private. - api_endpoint (
str
, optional) — The API endpoint to use when pushing the model to the hub. - token (
str
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - branch (
str
, optional) — The git branch on which to push the model. This defaults to"main"
. - create_pr (
boolean
, optional) — Whether or not to create a Pull Request frombranch
with that commit. Defaults toFalse
. - allow_patterns (
List[str]
orstr
, optional) — If provided, only files matching at least one pattern are pushed. - ignore_patterns (
List[str]
orstr
, optional) — If provided, files matching any of the patterns are not pushed. - delete_patterns (
List[str]
orstr
, optional) — If provided, remote files matching any of the patterns will be deleted from the repo.
Upload model checkpoint to the Hub.
Use allow_patterns
and ignore_patterns
to precisely filter which files should be pushed to the hub. Use
delete_patterns
to delete existing remote files in the same commit. See upload_folder
reference for more
details.
__call__
< source >( inputs: typing.Union[str, typing.List[str]] batch_size: int = 32 as_numpy: bool = False use_labels: bool = True show_progress_bar: typing.Optional[bool] = None ) → Union[torch.Tensor, np.ndarray, List[str], int, str]
Parameters
- inputs (Union[str, List[str]]) — The input sentence or sentences to predict classes for.
- batch_size (int, defaults to 32) — The batch size to use in encoding the sentences to embeddings. Higher often means faster processing but higher memory usage.
- as_numpy (bool, defaults to False) — Whether to output as numpy array instead.
- use_labels (bool, defaults to True) — Whether to try and return elements of SetFitModel.labels.
- show_progress_bar (Optional[bool], defaults to None) — Whether to show a progress bar while encoding.
Returns
Union[torch.Tensor, np.ndarray, List[str], int, str]
A list of string labels with equal length to the inputs if use_labels is True and SetFitModel.labels has been defined. Otherwise a vector with equal length to the inputs, denoting to which class each input is predicted to belong. If the inputs is a single string, then the output is a single label as well.
Predict the various classes.
Return a mapping from string labels to integer IDs.
Return a mapping from integer IDs to string labels.
create_model_card
< source >( path: str model_name: typing.Optional[str] = 'SetFit Model' )
Creates and saves a model card for a SetFit model.
encode
< source >( inputs: typing.List[str] batch_size: int = 32 show_progress_bar: typing.Optional[bool] = None ) → Union[torch.Tensor, np.ndarray]
Parameters
- inputs (
List[str]
) — The input sentences to embed. - batch_size (
int
, defaults to32
) — The batch size to use in encoding the sentences to embeddings. Higher often means faster processing but higher memory usage. - show_progress_bar (
Optional[bool]
, defaults toNone
) — Whether to show a progress bar while encoding.
Returns
Union[torch.Tensor, np.ndarray]
A matrix with shape [INPUT_LENGTH, EMBEDDING_SIZE], as a torch Tensor if this model has a differentiable Torch head, or otherwise as a numpy array.
Convert input sentences to embeddings using the SentenceTransformer
body.
fit
< source >( x_train: typing.List[str] y_train: typing.Union[typing.List[int], typing.List[typing.List[int]]] num_epochs: int batch_size: typing.Optional[int] = None body_learning_rate: typing.Optional[float] = None head_learning_rate: typing.Optional[float] = None end_to_end: bool = False l2_weight: typing.Optional[float] = None max_length: typing.Optional[int] = None show_progress_bar: bool = True )
Parameters
- x_train (
List[str]
) — A list of training sentences. - y_train (
Union[List[int], List[List[int]]]
) — A list of labels corresponding to the training sentences. - num_epochs (
int
) — The number of epochs to train for. - batch_size (
int
, optional) — The batch size to use. - body_learning_rate (
float
, optional) — The learning rate for theSentenceTransformer
body in theAdamW
optimizer. Disregarded ifend_to_end=False
. - head_learning_rate (
float
, optional) — The learning rate for the differentiable torch head in theAdamW
optimizer. - end_to_end (
bool
, defaults toFalse
) — If True, train the entire model end-to-end. Otherwise, freeze theSentenceTransformer
body and only train the head. - l2_weight (
float
, optional) — The l2 weight for both the model body and head in theAdamW
optimizer. - max_length (
int
, optional) — The maximum token length a tokenizer can generate. If not provided, the maximum length for theSentenceTransformer
body is used. - show_progress_bar (
bool
, defaults toTrue
) — Whether to display a progress bar for the training epochs and iterations.
Train the classifier head, only used if a differentiable PyTorch head is used.
freeze
< source >( component: typing.Union[typing.Literal['body', 'head'], NoneType] = None )
Freeze the model body and/or the head, preventing further training on that component until unfrozen.
Generate and return a model card string based on the model card data.
predict
< source >( inputs: typing.Union[str, typing.List[str]] batch_size: int = 32 as_numpy: bool = False use_labels: bool = True show_progress_bar: typing.Optional[bool] = None ) → Union[torch.Tensor, np.ndarray, List[str], int, str]
Parameters
- inputs (Union[str, List[str]]) — The input sentence or sentences to predict classes for.
- batch_size (int, defaults to 32) — The batch size to use in encoding the sentences to embeddings. Higher often means faster processing but higher memory usage.
- as_numpy (bool, defaults to False) — Whether to output as numpy array instead.
- use_labels (bool, defaults to True) — Whether to try and return elements of SetFitModel.labels.
- show_progress_bar (Optional[bool], defaults to None) — Whether to show a progress bar while encoding.
Returns
Union[torch.Tensor, np.ndarray, List[str], int, str]
A list of string labels with equal length to the inputs if use_labels is True and SetFitModel.labels has been defined. Otherwise a vector with equal length to the inputs, denoting to which class each input is predicted to belong. If the inputs is a single string, then the output is a single label as well.
Predict the various classes.
predict_proba
< source >( inputs: typing.Union[str, typing.List[str]] batch_size: int = 32 as_numpy: bool = False show_progress_bar: typing.Optional[bool] = None ) → Union[torch.Tensor, np.ndarray]
Parameters
- inputs (Union[str, List[str]]) — The input sentences to predict class probabilities for.
- batch_size (int, defaults to 32) — The batch size to use in encoding the sentences to embeddings. Higher often means faster processing but higher memory usage.
- as_numpy (bool, defaults to False) — Whether to output as numpy array instead.
- show_progress_bar (Optional[bool], defaults to None) — Whether to show a progress bar while encoding.
Returns
Union[torch.Tensor, np.ndarray]
A matrix with shape [INPUT_LENGTH, NUM_CLASSES] denoting probabilities of predicting an input as a class. If the input is a string, then the output is a vector with shape [NUM_CLASSES,].
Predict the probabilities of the various classes.
Example:
>>> model = SetFitModel.from_pretrained(...)
>>> model.predict_proba(["What a boring display", "Exhilarating through and through", "I'm wowed!"])
tensor([[0.9367, 0.0633],
[0.0627, 0.9373],
[0.0890, 0.9110]], dtype=torch.float64)
>>> model.predict_proba("That was cool!")
tensor([0.8421, 0.1579], dtype=torch.float64)
to
< source >( device: typing.Union[str, torch.device] ) → SetFitModel
Move this SetFitModel to device, and then return self. This method does not copy.
unfreeze
< source >( component: typing.Union[typing.Literal['body', 'head'], NoneType] = None keep_body_frozen: typing.Optional[bool] = None )
Unfreeze the model body and/or the head, allowing further training on that component.
SetFitHead
class setfit.SetFitHead
< source >( in_features: typing.Optional[int] = None out_features: int = 2 temperature: float = 1.0 eps: float = 1e-05 bias: bool = True device: typing.Union[torch.device, str, NoneType] = None multitarget: bool = False )
Parameters
- in_features (
int
, optional) — The embedding dimension from the output of the SetFit body. IfNone
, defaults toLazyLinear
. - out_features (
int
, defaults to2
) — The number of targets. If setout_features
to 1 for binary classification, it will be changed to 2 as 2-class classification. - temperature (
float
, defaults to1.0
) — A logits’ scaling factor. Higher values make the model less confident and lower values make it more confident. - eps (
float
, defaults to1e-5
) — A value for numerical stability when scaling logits. - bias (
bool
, optional, defaults toTrue
) — Whether to add bias to the head. - device (
torch.device
, str, optional) — The device the model will be sent to. IfNone
, will check whether GPU is available. - multitarget (
bool
, defaults toFalse
) — Enable multi-target classification by makingout_features
binary predictions instead of a single multinomial prediction.
A SetFit head that supports multi-class classification for end-to-end training. Binary classification is treated as 2-class classification.
To be compatible with Sentence Transformers, we inherit Dense
from:
https://github.com/UKPLab/sentence-transformers/blob/master/sentence_transformers/models/Dense.py
forward
< source >( features: typing.Union[typing.Dict[str, torch.Tensor], torch.Tensor] temperature: typing.Optional[float] = None )
Parameters
- features (
Dict[str, torch.Tensor]
ortorch.Tensor) -- The embeddings from the encoder. If using
dict` format, make sure to store embeddings under the key: ‘sentence_embedding’ and the outputs will be under the key: ‘prediction’. - temperature (
float
, optional) — A logits’ scaling factor. Higher values make the model less confident and lower values make it more confident. Will override the temperature given during initialization.
SetFitHead can accept embeddings in:
- Output format (
dict
) from Sentence-Transformers. - Pure
torch.Tensor
.
SetFitModelCardData
class setfit.SetFitModelCardData
< source >( language: typing.Union[str, typing.List[str], NoneType] = None license: typing.Optional[str] = None tags: typing.Optional[typing.List[str]] = <factory> model_name: typing.Optional[str] = None model_id: typing.Optional[str] = None dataset_name: typing.Optional[str] = None dataset_id: typing.Optional[str] = None dataset_revision: typing.Optional[str] = None task_name: typing.Optional[str] = None st_id: typing.Optional[str] = None )
Parameters
- language (Optional[Union[str, List[str]]]) — The model language, either a string or a list, e.g. “en” or [“en”, “de”, “nl”]
- license (Optional[str]) — The license of the model, e.g. “apache-2.0”, “mit”, or “cc-by-nc-sa-4.0”
- model_name (Optional[str]) — The pretty name of the model, e.g. “SetFit with mBERT-base on SST2”. If not defined, uses encoder_name/encoder_id and dataset_name/dataset_id to generate a model name.
- model_id (Optional[str]) — The model ID when pushing the model to the Hub, e.g. “tomaarsen/span-marker-mbert-base-multinerd”.
- dataset_name (Optional[str]) — The pretty name of the dataset, e.g. “SST2”.
- dataset_id (Optional[str]) — The dataset ID of the dataset, e.g. “dair-ai/emotion”.
- dataset_revision (Optional[str]) — The dataset revision/commit that was for training/evaluation.
- st_id (Optional[str]) — The Sentence Transformers model ID.
A dataclass storing data used in the model card.
Install codecarbon
to automatically track carbon emission usage and
include it in your model cards.
Example:
>>> model = SetFitModel.from_pretrained(
... "sentence-transformers/paraphrase-mpnet-base-v2",
... labels=["negative", "positive"],
... # Model card variables
... model_card_data=SetFitModelCardData(
... model_id="tomaarsen/setfit-paraphrase-mpnet-base-v2-sst2",
... dataset_name="SST2",
... dataset_id="sst2",
... license="apache-2.0",
... language="en",
... ),
... )
AbsaModel
class setfit.AbsaModel
< source >( aspect_extractor: AspectExtractor aspect_model: AspectModel polarity_model: PolarityModel )
from_pretrained
< source >( model_id: str polarity_model_id: typing.Optional[str] = None spacy_model: typing.Optional[str] = None span_contexts: typing.Tuple[typing.Optional[int], typing.Optional[int]] = (None, None) force_download: bool = None resume_download: bool = None proxies: typing.Optional[typing.Dict] = None token: typing.Union[str, bool, NoneType] = None cache_dir: typing.Optional[str] = None local_files_only: bool = None use_differentiable_head: bool = None normalize_embeddings: bool = None **model_kwargs )
save_pretrained
< source >( save_directory: typing.Union[str, pathlib.Path] polarity_save_directory: typing.Union[str, pathlib.Path, NoneType] = None push_to_hub: bool = False **kwargs )
AspectModel
class setfit.AspectModel
< source >( model_body: typing.Optional[sentence_transformers.SentenceTransformer.SentenceTransformer] = None model_head: typing.Union[setfit.modeling.SetFitHead, sklearn.linear_model._logistic.LogisticRegression, NoneType] = None multi_target_strategy: typing.Optional[str] = None normalize_embeddings: bool = False labels: typing.Optional[typing.List[str]] = None model_card_data: typing.Optional[setfit.model_card.SetFitModelCardData] = <factory> spacy_model: str = 'en_core_web_lg' span_context: int = 0 )
__call__
< source >( docs: typing.List[ForwardRef('Doc')] aspects_list: typing.List[typing.List[slice]] )
Get the Torch device that this model is on.
from_pretrained
< source >( pretrained_model_name_or_path: typing.Union[str, pathlib.Path] force_download: bool = False resume_download: bool = False proxies: typing.Optional[typing.Dict] = None token: typing.Union[str, bool, NoneType] = None cache_dir: typing.Union[str, pathlib.Path, NoneType] = None local_files_only: bool = False revision: typing.Optional[str] = None **model_kwargs )
Parameters
- pretrained_model_name_or_path (
str
,Path
) —- Either the
model_id
(string) of a model hosted on the Hub, e.g.bigscience/bloom
. - Or a path to a
directory
containing model weights saved using save_pretrained, e.g.,../path/to/my_model_directory/
.
- Either the
- revision (
str
, optional) — Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the latest commit onmain
branch. - force_download (
bool
, optional, defaults toFalse
) — Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding the existing cache. - resume_download (
bool
, optional, defaults toFalse
) — Whether to delete incompletely received files. Will attempt to resume the download if such a file exists. - proxies (
Dict[str, str]
, optional) — A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
. The proxies are used on every request. - token (
str
orbool
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - cache_dir (
str
,Path
, optional) — Path to the folder where cached files are stored. - local_files_only (
bool
, optional, defaults toFalse
) — IfTrue
, avoid downloading the file and return the path to the local cached file if it exists. - labels (
List[str]
, optional) — If the labels are integers ranging from0
tonum_classes-1
, then these labels indicate the corresponding labels. - model_card_data (
SetFitModelCardData
, optional) — ASetFitModelCardData
instance storing data such as model language, license, dataset name, etc. to be used in the automatically generated model cards. - model_card_data (
SetFitModelCardData
, optional) — ASetFitModelCardData
instance storing data such as model language, license, dataset name, etc. to be used in the automatically generated model cards. - use_differentiable_head (
bool
, optional) — Whether to load SetFit using a differentiable (i.e., Torch) head instead of Logistic Regression. - normalize_embeddings (
bool
, optional) — Whether to apply normalization on the embeddings produced by the Sentence Transformer body. - span_context (
int
, defaults to0
) — The number of words before and after the span candidate that should be prepended to the full sentence. By default, 0 for Aspect models and 3 for Polarity models. - device (
Union[torch.device, str]
, optional) — The device on which to load the SetFit model, e.g."cuda:0"
,"mps"
ortorch.device("cuda")
.
Download a model from the Huggingface Hub and instantiate it.
predict
< source >( inputs: typing.Union[str, typing.List[str]] batch_size: int = 32 as_numpy: bool = False use_labels: bool = True show_progress_bar: typing.Optional[bool] = None ) → Union[torch.Tensor, np.ndarray, List[str], int, str]
Parameters
- inputs (Union[str, List[str]]) — The input sentence or sentences to predict classes for.
- batch_size (int, defaults to 32) — The batch size to use in encoding the sentences to embeddings. Higher often means faster processing but higher memory usage.
- as_numpy (bool, defaults to False) — Whether to output as numpy array instead.
- use_labels (bool, defaults to True) — Whether to try and return elements of SetFitModel.labels.
- show_progress_bar (Optional[bool], defaults to None) — Whether to show a progress bar while encoding.
Returns
Union[torch.Tensor, np.ndarray, List[str], int, str]
A list of string labels with equal length to the inputs if use_labels is True and SetFitModel.labels has been defined. Otherwise a vector with equal length to the inputs, denoting to which class each input is predicted to belong. If the inputs is a single string, then the output is a single label as well.
Predict the various classes.
push_to_hub
< source >( repo_id: str config: typing.Optional[dict] = None commit_message: str = 'Push model using huggingface_hub.' private: bool = False api_endpoint: typing.Optional[str] = None token: typing.Optional[str] = None branch: typing.Optional[str] = None create_pr: typing.Optional[bool] = None allow_patterns: typing.Union[str, typing.List[str], NoneType] = None ignore_patterns: typing.Union[str, typing.List[str], NoneType] = None delete_patterns: typing.Union[str, typing.List[str], NoneType] = None )
Parameters
- repo_id (
str
) — ID of the repository to push to (example:"username/my-model"
). - config (
dict
, optional) — Configuration object to be saved alongside the model weights. - commit_message (
str
, optional) — Message to commit while pushing. - private (
bool
, optional, defaults toFalse
) — Whether the repository created should be private. - api_endpoint (
str
, optional) — The API endpoint to use when pushing the model to the hub. - token (
str
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - branch (
str
, optional) — The git branch on which to push the model. This defaults to"main"
. - create_pr (
boolean
, optional) — Whether or not to create a Pull Request frombranch
with that commit. Defaults toFalse
. - allow_patterns (
List[str]
orstr
, optional) — If provided, only files matching at least one pattern are pushed. - ignore_patterns (
List[str]
orstr
, optional) — If provided, files matching any of the patterns are not pushed. - delete_patterns (
List[str]
orstr
, optional) — If provided, remote files matching any of the patterns will be deleted from the repo.
Upload model checkpoint to the Hub.
Use allow_patterns
and ignore_patterns
to precisely filter which files should be pushed to the hub. Use
delete_patterns
to delete existing remote files in the same commit. See upload_folder
reference for more
details.
save_pretrained
< source >( save_directory: typing.Union[str, pathlib.Path] config: typing.Optional[dict] = None repo_id: typing.Optional[str] = None push_to_hub: bool = False **kwargs )
Parameters
- save_directory (
str
orPath
) — Path to directory in which the model weights and configuration will be saved. - config (
dict
, optional) — Model configuration specified as a key/value dictionary. - push_to_hub (
bool
, optional, defaults toFalse
) — Whether or not to push your model to the Huggingface Hub after saving it. - repo_id (
str
, optional) — ID of your repository on the Hub. Used only ifpush_to_hub=True
. Will default to the folder name if not provided. kwargs — Additional key word arguments passed along to theSetFitModel.push_to_hub()
method.
Save weights in local directory.
to
< source >( device: typing.Union[str, torch.device] ) → SetFitModel
Move this SetFitModel to device, and then return self. This method does not copy.
PolarityModel
class setfit.PolarityModel
< source >( model_body: typing.Optional[sentence_transformers.SentenceTransformer.SentenceTransformer] = None model_head: typing.Union[setfit.modeling.SetFitHead, sklearn.linear_model._logistic.LogisticRegression, NoneType] = None multi_target_strategy: typing.Optional[str] = None normalize_embeddings: bool = False labels: typing.Optional[typing.List[str]] = None model_card_data: typing.Optional[setfit.model_card.SetFitModelCardData] = <factory> spacy_model: str = 'en_core_web_lg' span_context: int = 3 )
__call__
< source >( docs: typing.List[ForwardRef('Doc')] aspects_list: typing.List[typing.List[slice]] )
Get the Torch device that this model is on.
from_pretrained
< source >( pretrained_model_name_or_path: typing.Union[str, pathlib.Path] force_download: bool = False resume_download: bool = False proxies: typing.Optional[typing.Dict] = None token: typing.Union[str, bool, NoneType] = None cache_dir: typing.Union[str, pathlib.Path, NoneType] = None local_files_only: bool = False revision: typing.Optional[str] = None **model_kwargs )
Parameters
- pretrained_model_name_or_path (
str
,Path
) —- Either the
model_id
(string) of a model hosted on the Hub, e.g.bigscience/bloom
. - Or a path to a
directory
containing model weights saved using save_pretrained, e.g.,../path/to/my_model_directory/
.
- Either the
- revision (
str
, optional) — Revision of the model on the Hub. Can be a branch name, a git tag or any commit id. Defaults to the latest commit onmain
branch. - force_download (
bool
, optional, defaults toFalse
) — Whether to force (re-)downloading the model weights and configuration files from the Hub, overriding the existing cache. - resume_download (
bool
, optional, defaults toFalse
) — Whether to delete incompletely received files. Will attempt to resume the download if such a file exists. - proxies (
Dict[str, str]
, optional) — A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}
. The proxies are used on every request. - token (
str
orbool
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - cache_dir (
str
,Path
, optional) — Path to the folder where cached files are stored. - local_files_only (
bool
, optional, defaults toFalse
) — IfTrue
, avoid downloading the file and return the path to the local cached file if it exists. - labels (
List[str]
, optional) — If the labels are integers ranging from0
tonum_classes-1
, then these labels indicate the corresponding labels. - model_card_data (
SetFitModelCardData
, optional) — ASetFitModelCardData
instance storing data such as model language, license, dataset name, etc. to be used in the automatically generated model cards. - model_card_data (
SetFitModelCardData
, optional) — ASetFitModelCardData
instance storing data such as model language, license, dataset name, etc. to be used in the automatically generated model cards. - use_differentiable_head (
bool
, optional) — Whether to load SetFit using a differentiable (i.e., Torch) head instead of Logistic Regression. - normalize_embeddings (
bool
, optional) — Whether to apply normalization on the embeddings produced by the Sentence Transformer body. - span_context (
int
, defaults to0
) — The number of words before and after the span candidate that should be prepended to the full sentence. By default, 0 for Aspect models and 3 for Polarity models. - device (
Union[torch.device, str]
, optional) — The device on which to load the SetFit model, e.g."cuda:0"
,"mps"
ortorch.device("cuda")
.
Download a model from the Huggingface Hub and instantiate it.
predict
< source >( inputs: typing.Union[str, typing.List[str]] batch_size: int = 32 as_numpy: bool = False use_labels: bool = True show_progress_bar: typing.Optional[bool] = None ) → Union[torch.Tensor, np.ndarray, List[str], int, str]
Parameters
- inputs (Union[str, List[str]]) — The input sentence or sentences to predict classes for.
- batch_size (int, defaults to 32) — The batch size to use in encoding the sentences to embeddings. Higher often means faster processing but higher memory usage.
- as_numpy (bool, defaults to False) — Whether to output as numpy array instead.
- use_labels (bool, defaults to True) — Whether to try and return elements of SetFitModel.labels.
- show_progress_bar (Optional[bool], defaults to None) — Whether to show a progress bar while encoding.
Returns
Union[torch.Tensor, np.ndarray, List[str], int, str]
A list of string labels with equal length to the inputs if use_labels is True and SetFitModel.labels has been defined. Otherwise a vector with equal length to the inputs, denoting to which class each input is predicted to belong. If the inputs is a single string, then the output is a single label as well.
Predict the various classes.
push_to_hub
< source >( repo_id: str config: typing.Optional[dict] = None commit_message: str = 'Push model using huggingface_hub.' private: bool = False api_endpoint: typing.Optional[str] = None token: typing.Optional[str] = None branch: typing.Optional[str] = None create_pr: typing.Optional[bool] = None allow_patterns: typing.Union[str, typing.List[str], NoneType] = None ignore_patterns: typing.Union[str, typing.List[str], NoneType] = None delete_patterns: typing.Union[str, typing.List[str], NoneType] = None )
Parameters
- repo_id (
str
) — ID of the repository to push to (example:"username/my-model"
). - config (
dict
, optional) — Configuration object to be saved alongside the model weights. - commit_message (
str
, optional) — Message to commit while pushing. - private (
bool
, optional, defaults toFalse
) — Whether the repository created should be private. - api_endpoint (
str
, optional) — The API endpoint to use when pushing the model to the hub. - token (
str
, optional) — The token to use as HTTP bearer authorization for remote files. By default, it will use the token cached when runninghuggingface-cli login
. - branch (
str
, optional) — The git branch on which to push the model. This defaults to"main"
. - create_pr (
boolean
, optional) — Whether or not to create a Pull Request frombranch
with that commit. Defaults toFalse
. - allow_patterns (
List[str]
orstr
, optional) — If provided, only files matching at least one pattern are pushed. - ignore_patterns (
List[str]
orstr
, optional) — If provided, files matching any of the patterns are not pushed. - delete_patterns (
List[str]
orstr
, optional) — If provided, remote files matching any of the patterns will be deleted from the repo.
Upload model checkpoint to the Hub.
Use allow_patterns
and ignore_patterns
to precisely filter which files should be pushed to the hub. Use
delete_patterns
to delete existing remote files in the same commit. See upload_folder
reference for more
details.
save_pretrained
< source >( save_directory: typing.Union[str, pathlib.Path] config: typing.Optional[dict] = None repo_id: typing.Optional[str] = None push_to_hub: bool = False **kwargs )
Parameters
- save_directory (
str
orPath
) — Path to directory in which the model weights and configuration will be saved. - config (
dict
, optional) — Model configuration specified as a key/value dictionary. - push_to_hub (
bool
, optional, defaults toFalse
) — Whether or not to push your model to the Huggingface Hub after saving it. - repo_id (
str
, optional) — ID of your repository on the Hub. Used only ifpush_to_hub=True
. Will default to the folder name if not provided. kwargs — Additional key word arguments passed along to theSetFitModel.push_to_hub()
method.
Save weights in local directory.
to
< source >( device: typing.Union[str, torch.device] ) → SetFitModel
Move this SetFitModel to device, and then return self. This method does not copy.