Version v0.6 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.

Hyperparameter Tuning (Katib)

Using Katib to tune your model’s hyperparameters on Kubernetes

The Katib project is inspired by Google vizier. Katib is a scalable and flexible hyperparameter tuning framework and is tightly integrated with Kubernetes. It does not depend on any specific deep learning framework (such as TensorFlow, MXNet, or PyTorch).

Installing Katib

To run Katib jobs, you must install the required packages as shown in this section. You can do so by following the Kubeflow deployment guide, or by installing Katib directly from its repository:

git clone

Persistent Volumes

If you want to use Katib outside Google Kubernetes Engine (GKE) and you don’t have a StorageClass for dynamic volume provisioning in your cluster, you must create a persistent volume (PV) to bind your persistent volume claim (PVC).

This is the YAML file for a PV:

apiVersion: v1
kind: PersistentVolume
  name: katib-mysql
    type: local
    app: katib
    storage: 10Gi
    - ReadWriteOnce
    path: /data/katib

After deploying the Katib package, run the following command to create the PV:

kubectl create -f

Running examples

After deploying everything, you can run some examples.

Example using random algorithm

You can create an Experiment for Katib by defining an Experiment config file. See the random algorithm example.

kubectl create -f

Running this command launches an Experiment. It runs a series of training jobs to train models using different hyperparameters and save the results.

The configurations for the experiment (hyperparameter feasible space, optimization parameter, optimization goal, suggestion algorithm, and so on) are defined in random-example.yaml.

In this demo, hyperparameters are embedded as args. You can embed hyperparameters in another way (for example, environment values) by using the template defined in TrialTemplate.GoTemplate.RawTemplate. It is written in go template format.

This demo randomly generates 3 hyperparameters:

  • Learning Rate (–lr) - type: double
  • Number of NN Layer (–num-layers) - type: int
  • optimizer (–optimizer) - type: categorical

Check the experiment status:

$ kubectl -n kubeflow describe experiment random-example
Name:         random-example
Namespace:    kubeflow
Annotations:  <none>
API Version:
Kind:         Experiment
  Creation Timestamp:  2019-01-18T16:30:46Z
  Generation:        5
  Resource Version:  1777650
  Self Link:         /apis/
  UID:               687a67f9-1b3e-11e9-a0c2-c6456c1f5f0a
    Algorithm Name:  random
    Algorithm Settings:
  Max Failed Trial Count:  3
  Max Trial Count:         100
    Additional Metric Names:
    Goal:                   0.99
    Objective Metric Name:  Validation-accuracy
    Type:                   maximize
  Parallel Trial Count:     10
    Feasible Space:
      Max:           0.03
      Min:           0.01
    Name:            --lr
    Parameter Type:  double
    Feasible Space:
      Max:           5
      Min:           2
    Name:            --num-layers
    Parameter Type:  int
    Feasible Space:
    Name:            --optimizer
    Parameter Type:  categorical
  Trial Template:
    Go Template:
      Template Spec:
        Config Map Name:       trial-template
        Config Map Namespace:  kubeflow
        Template Path:         mnist-trial-template
  Completion Time:  2019-06-20T00:12:07Z
    Last Transition Time:  2019-06-19T23:20:56Z
    Last Update Time:      2019-06-19T23:20:56Z
    Message:               Experiment is created
    Reason:                ExperimentCreated
    Status:                True
    Type:                  Created
    Last Transition Time:  2019-06-20T00:12:07Z
    Last Update Time:      2019-06-20T00:12:07Z
    Message:               Experiment is running
    Reason:                ExperimentRunning
    Status:                False
    Type:                  Running
    Last Transition Time:  2019-06-20T00:12:07Z
    Last Update Time:      2019-06-20T00:12:07Z
    Message:               Experiment has succeeded because max trial count has reached
    Reason:                ExperimentSucceeded
    Status:                True
    Type:                  Succeeded
  Current Optimal Trial:
        Name:   Validation-accuracy
        Value:  0.982483983039856
    Parameter Assignments:
      Name:          --lr
      Value:         0.026666666666666665
      Name:          --num-layers
      Value:         2
      Name:          --optimizer
      Value:         sgd
  Start Time:        2019-06-19T23:20:55Z
  Trials:            100
  Trials Succeeded:  100
Events:                 <none>

The demo should start an experiment and run three jobs with different parameters. When the spec.Status.Condition changes to Completed, the experiment is finished.

TensorFlow operator example

To run the TensorFlow operator example, you must install a volume.

If you are using GKE and default StorageClass, you must create this PVC:

apiVersion: v1
kind: PersistentVolumeClaim
  name: tfevent-volume
  namespace: kubeflow
    type: local
    app: tfjob
    - ReadWriteOnce
      storage: 10Gi

If you are not using GKE and you don’t have StorageClass for dynamic volume provisioning in your cluster, you must create a PVC and a PV:

kubectl create -f

kubectl create -f

Now you can run the TensorFlow operator example:

kubectl create -f

You can check the status of the experiment:

kubectl -n kubeflow describe experiment tfjob-example

PyTorch example

This is an example for the PyTorch operator:

kubectl create -f

You can check the status of the experiment:

kubectl -n kubeflow describe experiment pytorchjob-example

Monitoring results

You can monitor your results in the Katib UI. If you installed Kubeflow using the deployment guide, you can access the Katib UI at

https://<your kubeflow endpoint>/katib/

For example, if you deployed Kubeflow on GKE, your endpoint would be


Otherwise, you can set port-forwarding for the Katib UI service:

kubectl port-forward svc/katib-ui -n kubeflow 8080:80

Now you can access the Katib UI at this URL: http://localhost:8080/katib/.


Delete the installed components:


If you created a PV for Katib, delete it:

kubectl delete -f

If you created a PV and PVC for the TensorFlow operator, delete it:

kubectl delete -f
kubectl delete -f

Metrics collector

Katib has a metrics collector to take metrics from each trial. Katib collects metrics from stdout of each trial. Metrics should print in the following format: {metrics name}={value}. For example, when your objective value name is loss and the metrics are recall and precision, your training container should print like this:

epoch 1:

epoch 2:

Katib periodically launches CronJobs to collect metrics from pods.