Skip to content

StaticModelForClassification with distilled model #199

@Mahhos

Description

@Mahhos

Hi! I have distilled a model2vec model from a relatively small sentence transformer and now I'm trying to train a classifier using my new m2v static model. Here's the error I'm encountering. Do you have any advise?


import pandas as pd
from model2vec.train import StaticModelForClassification

classifier = StaticModelForClassification.from_pretrained("stella_m2v_model")

classifier.fit(df['search_query'], df['label_indices'].tolist())
TypeError: 'str' object cannot be interpreted as an integer
File <command-2318105830669928>, line 4
      1 import pandas as pd
      2 from model2vec.train import StaticModelForClassification
----> 4 classifier = StaticModelForClassification.from_pretrained("stella_m2v_model")
      6 classifier.fit(df['search_query'], df['label_indices'].tolist())
File /lib/python3.12/site-packages/model2vec/train/base.py:52, in FinetunableStaticModel.from_pretrained(cls, out_dim, model_name, **kwargs)
     50 """Load the model from a pretrained model2vec model."""
     51 model = StaticModel.from_pretrained(model_name)
---> 52 return cls.from_static_model(model, out_dim, **kwargs)
File /lib/python3.12/site-packages/model2vec/train/base.py:59, in FinetunableStaticModel.from_static_model(cls, model, out_dim, **kwargs)
     57 model.embedding = np.nan_to_num(model.embedding)
     58 embeddings_converted = torch.from_numpy(model.embedding)
---> 59 return cls(
     60     vectors=embeddings_converted,
     61     pad_id=model.tokenizer.token_to_id("[PAD]"),
     62     out_dim=out_dim,
     63     tokenizer=model.tokenizer,
     64     **kwargs,
     65 )
File /lib/python3.12/site-packages/model2vec/train/classifier.py:46, in StaticModelForClassification.__init__(self, vectors, tokenizer, n_layers, hidden_dim, out_dim, pad_id)
     44 self.hidden_dim = hidden_dim
     45 # Alias: Follows scikit-learn. Set to dummy classes
---> 46 self.classes_: list[str] = [str(x) for x in range(out_dim)]
     47 # multilabel flag will be set based on the type of `y` passed to fit.
     48 self.multilabel: bool = False

P.S. I can fit a classifier on my data using
classifier = StaticModelForClassification.from_pretrained(model_name="minishlab/potion-base-32M")
However, this also took more than 3 hours.

#how i used to load my distilled model:

from model2vec import StaticModel
model = StaticModel.from_pretrained("stella_m2v_model")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions