Skip to content

Add an example using Optuna and Transformers #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jul 7, 2025

Conversation

ParagEkbote
Copy link
Contributor

What does this PR do?

In this end-to-end tutorial, we are going to utilize the optuna library to perform hyperparameter optimization on a BERT model using the IMDB dataset.

Firstly, we will load and preprocess the dataset and define the model we want to perform HPO on. Then, we shall set the metrics and wrap it inside the trainer class along with a search space that will search the best set of hyperparameters for the learning rate, weight decay and batch size. Lastly, we will visualize the results as well.

Please let me know if any modifications are required and I will make the necessary changes.

Who can review?

@stevhliu.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@stevhliu
Copy link
Member

stevhliu commented Jun 4, 2025

Thanks for your work! However, I don't think its all that different from the current hyperparameter search docs in Transformers except its a more complete example.

@merveenoyan @sergiopaniego what do you think?

@ParagEkbote
Copy link
Contributor Author

ParagEkbote commented Jun 4, 2025

Just for the record, I'd actually wanted to include support for the transformer's library in their optuna-integration package. But since there is backend support provided by the transformers library, I contributed an starting example to their repo.

This PR builds on that example and provides a more hands-on approach for users to understand how to apply HPO to transformer models 🙂

@merveenoyan
Copy link
Collaborator

@ParagEkbote cookbook mostly contains end-to-end applied AI recipes where library integrations shine 💫 rather than minimal examples. it would be great to make it a more applied ML type of recipe

@ParagEkbote
Copy link
Contributor Author

I have now added the following improvements to the recipe to make it more applied:

  1. The successful trials are now saved in sqlite using RDBStorage.
  2. Observability is now available using Weight & Biases to track and analyze the HPO trials.
  3. Also, we can perform the final training from the optimized parameters and push the model to HF Hub.

Could you please review the changes?

cc: @stevhliu, @merveenoyan

Copy link
Member

@sergiopaniego sergiopaniego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the contribution, @ParagEkbote! 🙌

To make it more aligned with the rest of the recipes, it would be great to add a bit more context about the problem this recipe is aiming to solve. Including visuals or showcasing the results would also go a long way in making the explanation clearer and more engaging. I’d recommend checking out the other recipes for inspiration 😄 Also, make sure it runs as it is in Colab

What do you think?

@ParagEkbote
Copy link
Contributor Author

ParagEkbote commented Jun 17, 2025

Thanks so much for the contribution, @ParagEkbote! 🙌

To make it more aligned with the rest of the recipes, it would be great to add a bit more context about the problem this recipe is aiming to solve. Including visuals or showcasing the results would also go a long way in making the explanation clearer and more engaging. I’d recommend checking out the other recipes for inspiration 😄 Also, make sure it runs as it is in Colab

What do you think?

What type of visuals or results can help for the recipe. Also, are there any specific sections do you think additional context is required?

cc: @sergiopaniego

@sergiopaniego
Copy link
Member

sergiopaniego commented Jun 23, 2025

What type of visuals or results can help for the recipe. Also, are there any specific sections do you think additional context is required?

cc: @sergiopaniego

For example, we currently don't have any output throughout the notebook so we are not displaying any results 😄

@ParagEkbote
Copy link
Contributor Author

ParagEkbote commented Jun 25, 2025

The visualizations which can be created in the example:
image

image

image

@ParagEkbote
Copy link
Contributor Author

I have updated the tutorial to add context about the problem and executed the notebook as well. You can find the model on the HF Hub: https://huggingface.co/AINovice2005/bert-imdb-optuna-hpo

Could you please review the changes?

Copy link
Member

@sergiopaniego sergiopaniego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvement!!!

Could we include those visualizations in the notebook?
For example, there is a section specifically called Visualize the results but no results are displayed 😅

Additionally, we could still add additional context and make it more applied (explaining why is this important for the chosen use case in detail, providing outputs/visualization...) For example, showing the dataset, why the column selection, testing the trained model in the end to showcase its capabilities, compare the trained model to another one not trained using this approach... The idea of the recipe is to be and end-to-end applied notebook for a specific use case, rather than a documentation page 😄

@ParagEkbote
Copy link
Contributor Author

@sergiopaniego I've fixed the visualization errors and the made the cookbook language less instructional. Could you please review?

@@ -0,0 +1,1709 @@
{
Copy link
Member

@sergiopaniego sergiopaniego Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we adjust the titles, subtitles... in markdown format to follow:

\# Main title

\## Subtitle

....

?

Currently, the main tittle is \###. This will be really helpful for generating the index afterwards :)


Reply via ReviewNB

Copy link
Member

@sergiopaniego sergiopaniego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvement! Thanks!
Let's adjust the depth of the titles, subtitles... (#, ##, ###...) and we're ready :)

@ParagEkbote
Copy link
Contributor Author

@sergiopaniego Thank you for being a patient reviewer and providing me with detailed feedback for this recipe :)

I've updated the titles and subtitles with the appropriate markdown format as well.

Could you please review?

@ParagEkbote ParagEkbote requested a review from sergiopaniego July 4, 2025 10:00
Copy link
Member

@sergiopaniego sergiopaniego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

We finally need to add it to the index page as part of the latest notebooks. To do so, you can remove the oldest one (the one at the bottom) and add yours to the first position. You can look for example in previous PRs.

@ParagEkbote
Copy link
Contributor Author

It's done. Could you please review?

cc: @sergiopaniego

@ParagEkbote ParagEkbote requested a review from sergiopaniego July 4, 2025 13:52
Copy link
Member

@sergiopaniego sergiopaniego left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Let's wait for @stevhliu or @merveenoyan comments 😄

Copy link
Member

@stevhliu stevhliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks both for iterating on this!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@stevhliu stevhliu merged commit dc77d31 into huggingface:main Jul 7, 2025
1 check passed
@ParagEkbote ParagEkbote deleted the Add-Optuna-Example branch July 7, 2025 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants