Log KFP Metrics

You can further decorate the Python code to instruct the Kubeflow Pipeline to log Kubeflow Pipeline Metrics during execution.

Please follow along in your own copy of our notebook as we complete the steps below.
1. Import has_metrics from kale.dsk

First, you will need to import the has_metrics decorator from kale.sdk so that it can be used in your code as well as the logging function log_metric.  You will also need to import an additional metric from sklearn *accuracy_score. *Add the following lines to the list of imports:

from kale.sdk import has_metrics

from kale.sdk.logging import log_metric

from sklearn.metrics import accuracy_score

[2. Create a log metric function]{style=“font-weight: 600;”}

You will need to create a function that logs the desired metrics, to do so you will introduce a new function evaluate which will output the model quality metrics. Add a new function, before the @pipeline definition.

[def evaluate(model, x_test, y_test):]{face=“monospace, serif” style=“font-family: monospace, serif;”}

[    y_pred = model.predict(x_test)]{face=“monospace, serif” style=“font-family: monospace, serif;”}

[    accuracy = accuracy_score(y_test, y_pred)]{face=“monospace, serif” style=“font-family: monospace, serif;”}

[    log_metric(name=“accuracy”, value=accuracy)]{face=“monospace, serif” style=“font-family: monospace, serif;”}

Now decorate the function with

@has_metrics

@step(name="model_evaluation")

Kale understands that @has_metrics indicates the step will produce Kubeflow Pipeline Metrics during compilation and runtime.

[]{face=“monospace, serif” style=“font-family: monospace, serif;”}

3. Update model_training to return model

Now you need to update the model_training function to return the actual model by replacing

print(model.predict(x_test))

with

return model

as seen below

[4. Call model_evaluation from @pipeline step]{style=“font-weight: 600;”}

Update the @pipeline step to:

  1. Assign the output of train() to the variable model

  2. Call evaluate_model while passing the model from the train() output

Update the code in the @pipeline step to replace train(x, x_test, y, iters) with the following:

model = train(x, x_test, y, iters)

evaluate(model, x_test, y_test)

as seen in the below image

5. Open a new terminal

Select Terminal from the launcher to open a new terminal.

6. Deploy Code as Kubeflow Pipeline

Make sure you are in the directory with the Python code, kale-sdk-datavol-1 and execute the following to deploy the code as a Kubeflow Pipeline.

python3 -m kale kale_sdk.py --kfp

7. Review Pipeline Run

Navigate to the Kubeflow UI and open the Runs UI

The number of runs will continue to increase throughout the course - notice that the runtime will begin to increase as the code grows and the demand on the Kubeflow Pipeline increases throughout the course.

8. Access the pipeline run

Select the latest binary-classification run to see the Kubeflow Pipeline with the new step.

The green checkmarks indicate that the Kubeflow Pipeline was deployed and executed successfully, the pipeline may take some time to run therefore you will need to wait a few minutes until you can see the KFP Metric output.

9. Review KFP Metric Output

Select Run Output to review the KFP Metric output.

Once in Run Output, you will see any KFP Metrics written during execution:

Please make sure your code is working before proceeding to the next section - if you want to download the complete code you can do so by clicking here.