Path: blob/master/cpd4.8/notebooks/python_sdk/experiments/deep_learning/Use Keras and HPO to recognize hand-written digits.ipynb
6405 views
Use Keras and hyperparameter optimization (HPO) to recognize hand-written digits with ibm-watson-machine-learning
This notebook contains steps and code to demonstrate support of Deep Learning experiments in the Watson Machine Learning service. It introduces commands for data retrieval, training definition persistance, experiment training, model persistance, model deployment and scoring.
Some familiarity with Python is helpful. This notebook uses Python 3.10.
Learning goals
The learning goals of this notebook are:
Working with the Watson Machine Learning service.
Training Deep Learning models (TensorFlow).
Saving trained models in Watson Machine Learning repository.
Online deployment and scoring of the trained model.
Contents
This notebook contains the following parts:
Connection to WML
Authenticate the Watson Machine Learning service on IBM Cloud Pack for Data. You need to provide platform url
, your username
and api_key
.
Alternatively you can use username
and password
to authenticate WML services.
Install and import the ibm-watson-machine-learning
package
Note: ibm-watson-machine-learning
documentation can be found here.
Working with spaces
First of all, you need to create a space that will be used for your work. If you do not have space already created, you can use {PLATFORM_URL}/ml-runtime/spaces?context=icp4data
to create one.
Click New Deployment Space
Create an empty space
Go to space
Settings
tabCopy
space_id
and paste it below
Tip: You can also use SDK to prepare the space for your work. More information can be found here.
Action: Assign space ID below
You can use list
method to print all existing spaces.
To be able to interact with all resources available in Watson Machine Learning, you need to set space which you will be using.
For the purpose of this example two Keras model definitions have been prepared:
Multilayer Perceptron (MLP)
Convolution Neural Network (CNN)
2.1 Prepare model definition metadata
2.2 Get sample model definition content files from git (Python scripts with CNN and MLP)
Hint: You need to have wget
installed: pip install wget
Tip: Convert below cell to code and run it to see model deinition's code.
2.3 Publish model definition
List model definitions
Warning: Before executing deep learning experiment make sure that training data is saved in a folder where Watson Machine Learning Accelerator is installed.
3.1 Prepare training metadata
3.2 Train model in background
3.3 Get training id and status
3.4 Get training details
List trainings
Cancel training
You can cancel the training run by calling the method below.
Tip: If you want to delete train runs and results add hard_delete=True
as a parameter.
4.1 Publish model
4.2 Get model details
List stored models
5.1 Create online deployment for published model
5.2 Get deployments details
List deployments
5.3 Score deployed model
Let's plot two digits. Action: Please install matplotlib
, numpy
Our input node expects to get data with shape (784,) so we need to reshape our two digits.
Prepare scoring payload and score.
If you want to clean up all created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
please follow up this sample notebook.
You successfully completed this notebook! You learned how to use ibm-watson-machine-learning-client
to run experiments.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author
Jan Sołtysik, Intern in Watson Machine Learning.
Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.