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 intro to parameters

  • A Valohai parameter can be a string, an int, a float or a flag (=bool).

  • The default values for parameters are define in your code. These can be changed when creating an execution from the UI, command-line or API.

  • Parameters get passed to each Valohai execution as command-line arguments (e.g. train.py --epochs=5)

Update train.py to parameterize epochs.

  • Create a dictionary to pass valohai.prepare your parameters and their default values

  • Read the parameter value during an execution with valohai.parameters('myparam').value

 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
import numpy as np
import tensorflow as tf
import valohai


valohai.prepare(
    step='train-model',
    image='tensorflow/tensorflow:2.6.0',
    default_parameters={
        'epochs': 5,
    },
)

input_path = 'mnist.npz'
with np.load(input_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)
])

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam',
            loss=loss_fn,
            metrics=['accuracy'])

model.fit(x_train, y_train, epochs=valohai.parameters('epoch').value)

model.evaluate(x_test,  y_test, verbose=2)

output_path = valohai.outputs().path('model.h5')
model.save(output_path)

Add another parameter

Update train.py to parameterize learning_rate.

  • Create a dictionary to pass valohai.prepare the default values of your parameters

  • Read the parameter value during an execution with valohai.parameters('myparam').value

 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
import numpy as np
import tensorflow as tf
import valohai


valohai.prepare(
    step='train-model',
    image='tensorflow/tensorflow:2.6.0',
    default_parameters={
        'learning_rate': 0.001,
        'epochs': 5,
    },
)

input_path = 'mnist.npz'
with np.load(input_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)
])

optimizer = tf.keras.optimizers.Adam(learning_rate=valohai.parameters('learning_rate').value)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer=optimizer,
            loss=loss_fn,
            metrics=['accuracy'])

model.fit(x_train, y_train, epochs=valohai.parameters('epoch').value)

model.evaluate(x_test,  y_test, verbose=2)

output_path = valohai.outputs().path('model.h5')
model.save(output_path)

Run in Valohai

Update your valohai.yaml - Config File with vh yaml step. This will generate a parameters section in your step.

vh yaml step train.py

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