This is just a short recap
We strongly recommend completing the Mastering Valohai learning path on Valohai Academy.
This guide will provide you with a overview and a “cheatsheet” for migrating projects. It won’t explain the concepts and all the options in detail.
Valohai parameters can be configuration values, hyperparameter values, or any other configurable value that is set before the job is launched.
- Exposing your parameters allows you to easily rerun, sort, and keep track of executions based on the parameter values used to run them.
- You can easily create (or copy) an execution and change the parameters in the web application, without changing your code.
- Defining parameters allows you to start creating Tasks where you run multiple parallel executions with different parameter combinations.
Parsing command line argument
Valohai parameters are passed as command-line arguments.
This means that your command could look something like:
python train_model.py --iterations=10 --learningrate=0.01
argparse
You can parse the command-line arguments exactly like you’d do it today. The most common approach is using the argparse
package. Below an example how you’d parse the above mentioned parameters in Python.
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--iterations', type=int, default=10)
parser.add_argument('--learningrate', type=float, default=0.01)
return parser.parse_args()
args = parse_args()
iterations = args.iterations
learningrate = args.learningrate
valohai.yaml
So far, everything has been standard Python and command-line arguments. You’ll need to expose your parameters to Valohai through the valohai.yaml
file.
- step:
name: train-model
image: tensorflow/tensorflow:2.6.0
command:
- python train_model.py
parameters:
- name: iterations
type: integer
default: 10
- name: learningrate
type: float
default: 0.01
The values in the valohai.yaml
file are just default values. You can set different values for the parameters iterations
and learningrate
whenever you launch the job (web application, command-line or API). For example, when launching from the command-line you can say:
vh execution run train --adhoc --iterations=50 --learningrate=0.05
Now, the values in your valohai.yaml
will be the same Valohai will launch an execution with the new parameter values, and version the job with those parameters.
We don’t use command-line arguments
You can parse the parameter values also from a read-only configuration file that’s available inside every Valohai execution.
Depending on your preference, you can also parse the values from either:
/valohai/config/parameters.json
or/valohai/config/parameters.yaml
Note, you can’t set the parameter values here. These files are automatically created by Valohai and available inside an execution. You can only read the values from there.
valohai-utils
You can also parse the command-line arguments using the valohai-utils
helper library, instead of argparse
:
import valohai
iterations = valohai.parameters('iterations').value
learningrate = valohai.parameters('learningrate').value