Use parameters

Note

This tutorial is a part of our Valohai fundamentals series.

This section will show you how to use parameters in Valohai. Defining parameters will allow you to easily rerun your executions and experiment with a different set of values.

In this section you will learn:

  • How to define Valohai parameters

  • How to change parameter values between executions both in the CLI and in the UI

  • Defining Valohai parameters will allow you to easily rerun your executions and experiment with a different set of values.

A short introduction parameters

  • Defining 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 UI, without changing your code.

  • Defining parameters allows you to start creating Tasks where you run multiple parallel executions with different parameter combinations.

  • Each step has it’s own parameter configuration in valohai.yaml.

  • A parameter can be type of a string, integer, float, and flag (=boolean).

  • Parameters are passed as command-line arguments to your code.

    • Edit how parameters are passed to your code using pass-as.

    • You can also parse parameter values from YAML and JSON files inside your execution. See file-based configuration.

Update train.py to parameterize epochs.

  • Define a parameter in your valohai.yaml

  • Import argparse

  • Create a method to parse the command line arguments

  • Use the parsed argument

Start by commenting out the example parameter in your valohai.yaml and updating the command to include parameters. The {parameters} will be replaced with all the parameters and their values during runtime.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
---

- step:
    name: train-model
    image: tensorflow/tensorflow:2.1.0-py3
    command: python train.py {parameters}
    #inputs:
    #  - name: example-input
    #    default: https://example.com/
    parameters:
      - name: epochs
        type: integer
        default: 5

Important

The linter (vh lint) will flag any errors you might have in your YAML file. The most common errors are around identation, so make sure you pay special attention to indentation when writing YAML.

Next update your train.py to parse the command line arguments and use the argument in model.fit.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import tensorflow as tf
import numpy
import argparse

VH_OUTPUTS_DIR = os.getenv('VH_OUTPUTS_DIR', '.outputs/')

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--epochs', type=int, default=10)
    return parser.parse_args()

args = parse_args()

mnist = tf.keras.datasets.mnist

mnist_file_path = 'mnist.npz'

with numpy.load(mnist_file_path, allow_pickle=True) as f:
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']

x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10)
])

predictions = model(x_train[:1]).numpy()
predictions

tf.nn.softmax(predictions).numpy()

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

loss_fn(y_train[:1], predictions).numpy()

model.compile(optimizer='adam',
            loss=loss_fn,
            metrics=['accuracy'])

model.fit(x_train, y_train, epochs=args.epochs)

save_path = os.path.join(VH_OUTPUTS_DIR, 'model.h5')
model.save(save_path)

Run in Valohai

Update your valohai.yaml config file with vh yaml step. This will generate a parameters section in your step.

Finally run a new Valohai execution.

vh exec run train-model --adhoc

Rerun an execution with different parameter values

  • Open your project on app.valohai.com

  • Open the latest execution

  • Click Copy

  • Scroll down to the Parameters section

  • Change the value of epochs

  • Click Create execution

Tip

You can also run a new execution with different parameter values from the command line:

vh exec run train-model --adhoc --epochs=10

See also

Next: Download input data