A minimal utility for working with AWS Sagemaker hyperparameters. More broadly for dealing with environment variables. Two key functions:
hyperparam
reads the enviroment variablereport_hyperparameters
writes them to a JSON file, and logs them.
For purposes of this example we have the following environment variables set:
ENV["SM_HP_FOO"] = "1";
ENV["SM_HP_BAR"] = "2";
ENV["SM_HP_BAZ"] = "three";
ENV["SM_HP_QUX"] = "-3.14";
Sagemaker prefixes the environment variables it automatically defines for hyperparameters with SM_HP_
.
We can access an enviroment variable by name using hyperparam
:
julia> hyperparam(:foo)
1
We can tell it the type by passing that as the first argument:
julia> hyperparam(Float64, :bar)
2.0
If we don't it defaults to trying in order: Bool
, Int
, Float64
and finally falling back to assuming it is a String
:
julia> hyperparam(:baz)
"three"
report_hyperparameters(directory)
is used to output all the hyperparameters to the logs,
and write a file called hyperparameters.json
into the directory.
julia> using FilePathsBase
julia> report_hyperparameters(p".")
[info | Hyperparameters]: hyperparameters: baz=three
[info | Hyperparameters]: hyperparameters: bar=2.0
[info | Hyperparameters]: hyperparameters: qux=-3.14
[info | Hyperparameters]: hyperparameters: foo=1
[info | Hyperparameters]: Report: saving at ./hyperparameters.json
p"./hyperparameters.json"
The JSON file looks like:
{
"baz": "three",
"bar": 2.0,
"qux": -3.14,
"foo": 1
}
Notice two key things:
- Even though
qux
was never accessed during our code, it is still saved as the environment variable existed with the right prefix. It's type was found with the same mechanism used if the type is not provided tohyperparam
. Which determined it was aFloat64
(and not aString
). - When we accessed
bar
passing in the type, that type was remembered, so even though the enviroment variables just contained2
, the report correctly read2.0