Path: blob/master/cpd4.6/notebooks/python_sdk/experiments/deep_learning/Use PyTorch to recognize hand-written digits.ipynb
6405 views
Use PyTorch to recognize hand-written digits with ibm-watson-machine-learning
This notebook contains steps and code to demonstrate support of Deep Learning model training and scoring in Watson Machine Learning service. It introduces commands for getting data, training_definition persistance to Watson Machine Learning repository, model training, model persistance, model deployment and scoring.
Some familiarity with Python is helpful. This notebook uses Python 3.9.
Learning goals
The learning goals of this notebook are:
Working with Watson Machine Learning service.
Training Deep Learning models (TensorFlow).
Saving trained models in Watson Machine Learning repository.
Online deployment and scoring of 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.
2.1 Prepare model definition metadata
2.2 Get sample model definition content file from git
Tip: Convert below cell to code and run it to see model deinition's code.
2.3 Publish model definition
List models 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 the model in the 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 Crate online deployment for published model
You can deploy the stored model as a web service (online) by running code in the following cell.
5.2 Get deployments details
5.3 Score deployed model
Prepare sample scoring data to score deployed model.
Hint: You may need to install wget using following command !pip install wget
Build scoring dictionary consisting of two digits and send it to deployed model to get predictions.
List deployments
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 train and score PyTorch models.
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.