hparams
darts_segmentation.training.hparams
¶
Hyperparameters for training.
Augmentation
module-attribute
¶
Augmentation = typing.Literal[
"HorizontalFlip",
"VerticalFlip",
"RandomRotate90",
"Blur",
"RandomBrightnessContrast",
"MultiplicativeNoise",
"Posterize",
]
HP_NAMES
module-attribute
¶
HP_NAMES = [
field.name
for field in darts_segmentation.training.hparams.Hyperparameters.__dataclass_fields__.values()
]
Hyperparameters
dataclass
¶
Hyperparameters(
model_arch: str = "Unet",
model_encoder: str = "dpn107",
model_encoder_weights: str | None = None,
augment: list[
darts_segmentation.training.augmentations.Augmentation
]
| None = None,
learning_rate: float = 0.001,
gamma: float = 0.9,
focal_loss_alpha: float | None = None,
focal_loss_gamma: float = 2.0,
batch_size: int = 8,
bands: list[str] | None = None,
)
Hyperparameters for Cyclopts CLI.
Attributes:
-
model_arch
(str
) –Architecture of the model to use.
-
model_encoder
(str
) –Encoder type for the model.
-
model_encoder_weights
(str | None
) –Weights for the encoder, if any.
-
augment
(list[darts_segmentation.training.augmentations.Augmentation] | None
) –List of augmentations to apply.
-
learning_rate
(float
) –Learning rate for training.
-
gamma
(float
) –Decay factor for learning rate.
-
focal_loss_alpha
(float | None
) –Alpha parameter for focal loss, if using.
-
focal_loss_gamma
(float
) –Gamma parameter for focal loss.
-
batch_size
(int
) –Batch size for training.
-
bands
(list[str] | None
) –List of bands to use. Defaults to None.
augment
class-attribute
instance-attribute
¶
augment: (
list[
darts_segmentation.training.augmentations.Augmentation
]
| None
) = None
parse_hyperparameters
¶
parse_hyperparameters(
hpconfig_file: pathlib.Path,
) -> dict[
str,
list
| scipy.stats.rv_discrete
| scipy.stats.rv_continuous,
]
Parse hyperparameter configuration file to a valid dictionary for sklearn parameter search.
Can be YAML or TOML. Must contain a key called "hyperparameters" containing a list of hyperparameters distributions. These distributions can either be explicit defined by another dictionary containing a "distribution" key, or they can be implicit defined by a single value, a list or a dictionary containing a "low" and "high" key.
The following distributions are supported
- "uniform": Uniform distribution - must have a "low" and "high" value
- "loguniform": Log-uniform distribution - must have a "low" and "high" value
- "intuniform": Integer uniform distribution - must have a "low" and "high" value (both are inclusive)
- "choice": Choice distribution - must have a list of "choices" for explicit case, else just pass a list
- "value": Fixed value distribution - must have a "value" key for explicit case, else just pass a value
Examples:
Explicit Toml:
[hyperparameters]
learning_rate = {distribution = "loguniform", low = 1.0e-5, high = 1.0e-2}
batch_size = {distribution = "choice", choices = [32, 64, 128]}
gamma = {distribution = "uniform", low = 0.9, high = 2.5}
dropout = {distribution = "uniform", low = 0.0, high = 0.5}
layers = {distribution = "intuniform", low = 1, high = 10}
architecture = {distribution = "constant", value = "resnet"}
Explicit YAML:
hyperparameters:
learning_rate:
distribution: loguniform
low: 1.0e-5
high: 1.0e-2
batch_size:
distribution: choice
choices: [32, 64, 128]
gamma:
distribution: uniform
low: 0.9
high: 2.5
dropout:
distribution: uniform
low: 0.0
high: 0.5
layers:
distribution: intuniform
low: 1
high: 10
architecture:
distribution: constant
value: "resnet"
Implicit YAML:
hyperparameters:
learning_rate:
distribution: loguniform
low: 1.0e-5
high: 1.0e-2
batch_size: [32, 64, 128]
gamma:
low: 0.9
high: 2.5
dropout:
low: 0.0
high: 0.5
layers:
low: 1
high: 10
architecture: "resnet"
Will all result in the following dictionary:
{
"learning_rate": scipy.stats.loguniform(1.0e-5, 1.0e-2),
"batch_size": [32, 64, 128],
"gamma": scipy.stats.uniform(0.9, 1.6),
"dropout": scipy.stats.uniform(0.0, 0.5),
"layers": scipy.stats.randint(1, 11),
"architecture": ["resnet"]
}
Parameters:
Returns:
-
dict
(dict[str, list | scipy.stats.rv_discrete | scipy.stats.rv_continuous]
) –Dictionary of hyperparameters to tune and their distributions.
Raises:
-
ValueError
–If the hyperparameter configuration file is not a valid YAML or TOML file.
Source code in darts-segmentation/src/darts_segmentation/training/hparams.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
sample_hyperparameters
¶
sample_hyperparameters(
param_grid: dict[
str,
list
| scipy.stats.rv_discrete
| scipy.stats.rv_continuous,
],
n_trials: int | typing.Literal["grid"] = 100,
) -> list[
darts_segmentation.training.hparams.Hyperparameters
]
Sample hyperparameters from a parameter grid.
This function samples a list of hyperparameter combinations from a parameter grid. It supports both random sampling and grid search.
Parameters:
-
param_grid
(dict
) –Dictionary of hyperparameters to tune and their distributions. Values can be lists of values or scipy.stats distribution objects.
-
n_trials
(int | typing.Literal['grid']
, default:100
) –Number of hyperparameter combinations to sample. If set to "grid", will perform a grid search over all possible combinations. Defaults to 100.
Returns:
-
list
(list[darts_segmentation.training.hparams.Hyperparameters]
) –List of dictionaries, where each dictionary represents a hyperparameter combination.
Raises:
-
ValueError
–If n_trials is not an integer (saying a random search) or 'grid'.