Define your Hyperparameters once and use in argparse, hypertunning libraries, and as strongly-typed attributes in code!
Project description
Hyperparameters
Are you tired of repeating hyperparameters in code, argparse
definitions, and hyperparameter tunning libraries?
Hyperparameters
lets you define your hyperparameters once and use everywhere! Moreover, you get type linting and spelling checks for free!
Quickstart
Install Hyperparameters
:
pip install hyperparameters
You need to install the ray.tune
package separately if you want to use the ray.tune
hypertunning integration:
pip install -U "ray[tune]"
Define your parameters once using the Hyperparams
class:
from argparse import ArgumentParser
from typing import Optional
from hyperparameters import HP, Hyperparams
class MyHyperparams(Hyperparams):
epochs: int = HP(
"Number of epochs to train for",
default=5,
)
lr: float = HP(
"Learning rate",
default=1e-3,
)
tokenizer: str = HP(
"HF tokenizer to use",
default="BPE",
choices=["BPE", "WordPiece"],
)
train_data_path: str = HP(
"Path to the training dataset",
)
use_dropout: bool = HP(
"Whether the dropout layers should be activated",
default=True,
)
pretrained_weights: Optional[str] = HP(
"Path to the pretrained model weights, if any",
default=None,
)
parser = ArgumentParser()
MyHyperparams.add_arguments(parser)
params = MyHyperparams.from_arguments(parser.parse_args())
# access parameters as typed attributes
params.train_data_path
# convert to a dictionary
print(params.dict())
Let's see the registered arguments by running the code above with --help
:
usage: example.py [-h] [--epochs EPOCHS] [--lr LR] [--tokenizer {BPE,WordPiece}] --train-data-path TRAIN_DATA_PATH [--use-dropout | --no-use-dropout]
[--pretrained-weights PRETRAINED_WEIGHTS]
options:
-h, --help show this help message and exit
--epochs EPOCHS Number of epochs to train for
--lr LR Learning rate
--tokenizer {BPE,WordPiece}
HF tokenizer to use
--train-data-path TRAIN_DATA_PATH
Path to the training dataset
--use-dropout Whether the dropout layers should be activated
--no-use-dropout Disable: Whether the dropout layers should be activated
--pretrained-weights PRETRAINED_WEIGHTS
Path to the pretrained model weights, if any
As can be seen from the above, Hyperparameters
takes care of low level details for you:
- The
--train-data-path
parameter is required because we didn't provide a default value for it. All other parameters are optional. - Even though the default value for
--pretrained-weights
isNone
, this parameter is optional. However, we had to use theOptional[str]
type hint. - You can provide the
--use-dropout
or the--no-use-dropout
flag, but not both at the same time. Neither flag is required, as theuse_dropout
parameter has a default value. - The
--tokenizer
parameter can only beBPE
orWordPiece
. Providing any other value results in an error. - The data types of the parameters are parsed from strings and validated according to the type hints in the
MyHyperparams
class. - The default values are used whenever an argument is ommitted. Let's check that by running with
--train-data-path mydata/
: the script prints:{'epochs': 5, 'lr': 0.001, 'tokenizer': 'BPE', 'train_data_path': 'mydata/', 'use_dropout': True, 'pretrained_weights': None}
.
Default values and required parameters
- If a default value is omitted completely, the parameter will be required.
- Otherwise, if any default value is specified, even
None
, the parameter will be optional. - You must use the
Optional
type hint if the default value isNone
. - You can't have
bool
parameters withNone
as default. Flags that can beNone
just don't make any sense. - You can't have choice parameters with
None
as default. If you need this, just add a "null" value to the list of choices.
Hypertunning
Different hypertunning libraries provide different APIs for defining search spaces. Hyperparameters
can be easily extended to support any hypertunning library. You can do it yourself following the steps discussed below - it's easy! The ray.tune
library is supported out of the box.
When defining hypertunable parameters with Hyperparameters
, make sure to inherit from both the Hyperparams
class and the mixin class specific to your hypertunning library. Here is an example for ray.tune
:
from hyperparameters import HP, Hyperparams
from hyperparameters.ray_tune_hyperparams import RayTuneHyperparamsMixin
class MyHyperparams(Hyperparams, RayTuneHyperparamsMixin):
...
Next, you can specify the search spaces:
- use the hypertunning library API to describe the search space and provide this object to the
HP(..., search_space=)
parameter; - if you already specify the
HP(..., choices=[])
parameter you can treat the possible choices as a search space by setting theHP(..., tunable=True)
parameter. - for
bool
fields just set theHP(..., tunable=True)
parameter, which is equivallent toHP(..., tunable=True, choices=[False, True])
.
Note that when using the search_space
parameter, you don't need to use the tunable
parameter.
Finally, you are ready to use the mixin-specific methods to extract information about your search spaces. For ray.tune
these are:
.ray_tune_param_space()
method that returns a dictionary describing the search space..ray_tune_best_values()
method that returns a dictionary of the best values to start hypertunning from.
Here is a complete example of defining parameters hypertunable with ray.tune
:
from ray import tune
from hyperparameters import HP, Hyperparams
from hyperparameters.ray_tune_hyperparams import RayTuneHyperparamsMixin
class MyHyperparams(Hyperparams, RayTuneHyperparamsMixin):
lr: float = HP(
"Learning rate",
default=1e-3,
search_space=tune.loguniform(1e-5, 1e-2),
)
layers_num: int = HP(
"Number of model layers",
default=8,
tunable=True,
choices=[4, 8, 16, 24],
)
use_dropout: bool = HP(
"Whether the dropout layers should be activated",
default=True,
tunable=True,
)
params = MyHyperparams()
# Search space config that ray.tune understands
params.ray_tune_param_space()
# Best values to start the hypertunning from
params.ray_tune_best_values()
Supporting other hypertunning libraries
In Hyperparameters
, the logic specific to hypertunning libraries is implemented with mixin classes. This means that you can add many mixins to your parameters and support several hypertunning libraries at once.
The best place to start is to review the implementation of the RayTuneHyperparamsMixin
class in hyperparameters/ray_tune_hyperparams.py
.
The class implementing a new hypertunning library must:
- Inherit from
hyperparameters.hyperparams.HyperparamsProtocol
. - Provide class methods for converting the params data stored in
hyperparameters.hyperparams.HyperparamInfo
structure into a format that the hypertunning library understands. - Use the
cls._tunable_params()
to get the info about the parameters. - Wrap
info.choices
in a proper type for the hypertunning library. - Provide a method for returning the best parameter values to start the hypertunning from.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hyperparameters-0.4.0.tar.gz
.
File metadata
- Download URL: hyperparameters-0.4.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6baeecdc92c56237244f6f4b02abbffc95bb7c1225fbbe49b09c9b6d4d04d1f4 |
|
MD5 | 0174cd577763bfd8d759f65de122c6d7 |
|
BLAKE2b-256 | ea24d841df23ac17b7d3d082cdea0c4ebc93fd3d73a6bb85b9bd9b8cf92b3f47 |
File details
Details for the file hyperparameters-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: hyperparameters-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a0348c05ee28f5dd1596a13d5460c56a53dac50a7c05b2faea2e63c3ae58e01 |
|
MD5 | 68c9fc07eb0f78cc5207d0edc83385a8 |
|
BLAKE2b-256 | 29031f736bd638bfc47193a58ae91e608bc501843e5bdb663916978c21e6d1d1 |