Closed keloemma closed 2 years ago
not sure where you took that code from, but indeed the Trainer doesn't have such method.
What you want is:
model.save_pretrained(modeldir)
Or trainer.save_model(modeldir)
which will call the method Stas mentioned.
Hey folks, i couldn't get save_model
to work.
I am guessing push_to_hub
isn't the only option we got, right? My trainer is
from setfit import SetFitModel, SetFitTrainer
from sentence_transformers.losses import CosineSimilarityLoss
# Load a SetFit model from Hub
model_id = "sentence-transformers/all-mpnet-base-v2"
model = SetFitModel.from_pretrained(model_id)
# Create trainer
trainer = SetFitTrainer(
model=model,
train_dataset=train_dataset,
eval_dataset=test_dataset,
loss_class=CosineSimilarityLoss,
metric="accuracy",
batch_size=64,
num_iterations=20,
num_epochs=1,
)
# Train and evaluate
trainer.train()
metrics = trainer.evaluate()
you're calling save_pretrained
on the wrong object, please see my comment: https://github.com/huggingface/transformers/issues/14828#issuecomment-997433709 (edit: I see you edited your post to remove this failure).
wrt save_model
we don't know what SetFitTrainer
is. If you use the normal Trainer
object it has save_model
Sorry, I updated my comment i am using save_model
(not save_pretrained
) my trainer
is as shown below. My assumption was SetFitTrainer
is fundamentally of type Trainer
? I could be wrong.
<setfit.trainer.SetFitTrainer at 0x7fe8b748e710>
In my last comment I showed you that transformers.Trainer
has save_model
.
And I repeat I have no idea what setfit.trainer.SetFitTrainer
is - it's not a transformers
class.
Sorry, should've referred here about SetFit. I'll log an issue there. Thanks
It is not a subclass of transformers.Trainer
as far as I can see:
I suppose it looks like transformers.Trainer
but it's a totally independent implementation. So you will have to ask for this feature at that other project.
or use model.save_pretrained(modeldir)
which always works, since it's a feature of the transformers
models.
Yeah, I realized that it wasn't a subclass.
So, I guess exports to openvino and onnx are supported for now. The only other ways I could think is using joblib
or pickle
. This worked -
import joblib
joblib.dump(trainer, './model/cstom-setfit-model.joblib')
trainer = joblib.load('./model/cstom-setfit-model.joblib')
trainer.model.predict(["text", "text"])
P.S.: model.save_pretrained
works too.
save_pretrained
isn't just for saving/restoring objects on resume - its primarily use is to save just the parts that are needed to use from_pretrained
and/or sharing the results with others. But otherwise your method with joblib
for resumes is just fine.
Hello, I am not able to save pytorch.bin and cofig files from trl import SFTTrainer trainer = SFTTrainer( model=model, train_dataset=dataset, peft_config=peft_config, # passing peft config dataset_text_field="text", # mentioned the required column args=training_arguments, # training agruments tokenizer=tokenizer, # tokenizer packing=False, max_seq_length=512 ) trainer.train() model_to_save = trainer.model.module if hasattr(trainer.model, 'module') else trainer.model # Take care of distributed/parallel training model_to_save.save_pretrained("outputs") tokenizer.save_pretrained('outputs') import gc gc.collect() trainer.save_model('modeldir') could you please help me how can I get that?
Hi @Shruthipriya-BS,
If you believe there is a bug in the code, could you open a new issue? Please make sure to follow the issue template, providing a minimal code example we can use to reproduce the issue and information about the running environment. In the snippet in your comment, we don't have access to model
or dataset
so can't reproduce on our side. Code examples should also be in markdown code formatting i.e. between a pair of three backticks: ``` code goes here ```
Environment info
transformers
version: 4.8Who can help @LysandreJik, @stas00, @sgugger
Information
Model I am using (Bert, XLNet ...): Flaubert model
The problem arises when using:
I tried to save best model after training and I get that error.
The tasks I am working on is:
To reproduce
Steps to reproduce the behavior:
For doing experiment I used jean zay
Expected behavior