Skip to content

feat: Allow ModelTrainer to accept hyperparameters file #5059

New issue

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

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

Already on GitHub? # to your account

Merged
merged 15 commits into from
Mar 5, 2025

Conversation

benieric
Copy link
Contributor

@benieric benieric commented Feb 25, 2025

Issue #, if available:

Description of changes:

  • This PR allows users to set hyperparameters via a yaml or json file
  • Also gives adds integs for additional hp contracts using custom tooling like OmegaConf

Example Customer Usage

Example hyperparameters.yaml file

batch_size: 32
learning_rate: 0.001

Create ModelTrainer with hyperparameters from file

model_trainer = ModelTrainer(
	...,
	hyperparameters="<path>/<to>/hyperparameters.yaml"
)
model_trainer.train()

Load Hyperparameters in training script

import os
import json
from dataclasses import dataclass
from omegaconf import OmegaConf


# Access as dictionary
hp_dict = json.loads(os.environ["SM_HPS"])
batch_size = hp_dict["batch_size"]


# Access as typed dataclass
@dataclass
class	HPConfig:
    batch_size: int
    learning_rate: float

hp_config: HPConfig = OmegaConf.merge(
	OmegaConf.structured(HPConfig), 
	OmegaConf.create(hp_dict)
)
batch_size = hp_config.batch_size

Testing done:

  • Added unit and integ tests

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

  • I have read the CONTRIBUTING doc
  • I certify that the changes I am introducing will be backward compatible, and I have discussed concerns about this, if any, with the Python SDK team
  • I used the commit message format described in CONTRIBUTING
  • I have passed the region in to all S3 and STS clients that I've initialized as part of this change.
  • I have updated any necessary documentation, including READMEs and API docs (if appropriate)

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have added unit and/or integration tests as appropriate to ensure backward compatibility of the changes
  • I have checked that my tests are not configured for a specific region or account (if appropriate)
  • I have used unique_name_from_base to create resource names in integ tests (if appropriate)
  • If adding any dependency in requirements.txt files, I have spell checked and ensured they exist in PyPi

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@benieric benieric force-pushed the master-model-trainer-hyperparameters branch from a76b672 to dc5ac8f Compare February 25, 2025 21:50
@benieric benieric changed the title Feat: Create Hyperparameter class and accept hyperparameters file in ModelTrainer feat: Create Hyperparameter class and accept hyperparameters file in ModelTrainer Feb 25, 2025
@benieric benieric force-pushed the master-model-trainer-hyperparameters branch from f3a9cba to f8f0854 Compare February 28, 2025 21:57
@benieric benieric marked this pull request as ready for review March 3, 2025 23:43
@benieric benieric requested a review from a team as a code owner March 3, 2025 23:43
@benieric benieric requested a review from mohanasudhan March 3, 2025 23:43
@benieric benieric force-pushed the master-model-trainer-hyperparameters branch from abe46cb to 3f0b85e Compare March 3, 2025 23:54
@benieric benieric changed the title feat: Create Hyperparameter class and accept hyperparameters file in ModelTrainer feat: Allow ModelTrainer to accept hyperparameters file Mar 3, 2025
@benieric benieric merged commit 83ce1a0 into aws:master Mar 5, 2025
14 checks passed
mollyheamazon pushed a commit to mollyheamazon/sagemaker-python-sdk that referenced this pull request Mar 14, 2025
* Allow ModelTrainer to accept hyperparameter file and create Hyperparameter class

* pylint

* Detect hyperparameters from contents rather than file extension

* pylint

* change: add integs

* change: add integs

* change: remove custom hyperparameter tooling

* Add tests for hp contracts

* change: add unit tests and remove unreachable condition

* fix integs

* doc check fix

* fix tests

* fix tox.ini

* add unit test
evakravi pushed a commit to evakravi/sagemaker-python-sdk that referenced this pull request Mar 20, 2025
* Allow ModelTrainer to accept hyperparameter file and create Hyperparameter class

* pylint

* Detect hyperparameters from contents rather than file extension

* pylint

* change: add integs

* change: add integs

* change: remove custom hyperparameter tooling

* Add tests for hp contracts

* change: add unit tests and remove unreachable condition

* fix integs

* doc check fix

* fix tests

* fix tox.ini

* add unit test
mollyheamazon pushed a commit to mollyheamazon/sagemaker-python-sdk that referenced this pull request Apr 8, 2025
* Allow ModelTrainer to accept hyperparameter file and create Hyperparameter class

* pylint

* Detect hyperparameters from contents rather than file extension

* pylint

* change: add integs

* change: add integs

* change: remove custom hyperparameter tooling

* Add tests for hp contracts

* change: add unit tests and remove unreachable condition

* fix integs

* doc check fix

* fix tests

* fix tox.ini

* add unit test
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants