Use Data Science Workshop (DSW) with its built-in notebook or VS Code to develop, train, and deploy a deep learning model in the cloud. This tutorial uses the MNIST handwritten digit recognition example to demonstrate the end-to-end process.
MNIST handwritten digit recognition is a classic introductory task in deep learning. The goal is to train a model to recognize 10 handwritten digits (0 to 9).
Prerequisites
You have activated PAI and created a workspace with your primary account. If not, go to the PAI console, select a region in the upper-left corner, and follow the on-screen instructions to grant permissions and activate the service.
Billing
This tutorial uses a pay-as-you-go DSW instance and an EAS model service created from public resources. For more information, see DSW billing and EAS billing.
Create a DSW instance
Go to the DSW page.
Go to the PAI console.
In the upper-left corner, select the target region.
In the left-side navigation pane, click Workspaces and select your target workspace.
In the left-side navigation pane, choose .
On the Configure Instance page, configure the following parameters and leave others at their default settings.
Resource Type: Select Public Resources (pay-as-you-go).
Instance Type: Select
ecs.gn7i-c8g1.2xlarge.If this instance type is out of stock, you can select another GPU-accelerated instance.
Image config: Select Alibaba Cloud Image and search for the image
modelscope:1.26.0-pytorch2.6.0-gpu-py311-cu124-ubuntu22.04.Use the same image to avoid environment compatibility issues.
Mount storage: Persistently store your model development files. This tutorial uses Object Storage Service (OSS). Click OSS, click the
icon, select a Bucket, and create a new directory, such as pai_test.If no bucket is available in the current region, follow these steps to create one:
URI:
oss://**********oss-cn-hangzhou-internal.aliyuncs.com/pai_test/Mount Path:
/mnt/data/
Click OK to create the instance.
If the instance fails to start, see Create a DSW instance for troubleshooting.
Develop the model in DSW
In the instance list, click Open to enter the DSW development environment. On the Launcher page, click Create Notebook.
Download the provided MNIST training code, mnist.ipynb, and upload it to DSW by clicking the
icon in the upper-left corner.
Train the model. Open
mnist.ipynb, find the training code cell, and click the
icon to run it. The code automatically downloads the MNIST dataset to the dataSetdirectory and saves the trained model to theoutputdirectory. Training takes about 10 minutes.

The model's accuracy on the validation set, which reflects its generalization ability, is displayed during training. The validation accuracy in this example is 98%, which means you can proceed to the next steps.
View the training curves. Run the following cell and click the TensorBoard URL
http://localhost:6006/.
TensorBoard displays the train_loss (training set loss) and validation_loss (validation set loss) curves.

After viewing the curves, click the
icon in the cell to stop TensorBoard. Subsequent cells cannot run until you stop TensorBoard.Test the model's performance. Run the following cell to display 20 test images with their true labels and the model's predictions.

Sample output:

The DSW instance created in this tutorial uses a temporary cloud disk for file storage. If the instance remains stopped for more than 15 days, the content on the cloud disk is automatically deleted. To permanently store the model files for subsequent deployment with EAS, copy them to OSS.

Go to the OSS console to view the copied files.

After model development is complete, you can deploy the model as an online service. For more information, see Deploy the model as a service by using EAS.
The DSW instance in this tutorial uses public resources and is billed on a pay-as-you-go basis. To avoid ongoing charges, stop or delete the instance on the instance list page when you no longer need it.
Deploy a model service with EAS
Elastic Algorithm Service (EAS) deploys trained models as online inference services or AI web applications. It combines heterogeneous resource support, automatic scaling, one-click stress testing, canary releases, and real-time monitoring to maintain service stability under high concurrency at lower cost.
This tutorial provides a web API for the model. Run the following cell to copy the API code to OSS.

(Optional) Verify the web API in DSW. Run the following cell to install dependencies and start the service.

Test the endpoint. At the top of the page, click WebIDE. In the left-side pane, click
request_web.py, and then click the
icon to run the code.
Returned result:
{"prediction": 7}NoteTo access the web service in DSW from the internet, you must configure a VPC, a NAT gateway, and an EIP. For more information, see Access a service in an instance over the internet.
Create an EAS service. In the left-side navigation pane of the PAI console, click Elastic Algorithm Service (EAS) > Deploy Service > Custom Deployment.
Configure the following parameters and leave others at their default settings:
Deployment Method: Image-based Deployment
Image Configuration: Select Image Address and paste the image address used by DSW.
To avoid environment-related issues, use the same image as in DSW, which has already been verified to run the model code.

Mount storage: The model files and web API code are already copied to OSS. Click OSS and select the corresponding path.
URI: oss://*****/pai_test/
Mount Path: /mnt/data/
Command to Run: Because the
web.pyfile is mounted to/mnt/data/, the run command ispython /mnt/data/web.py.Port Number: Enter the port number used by
web.py, which is9000.Third-party Library Settings: The selected image is missing the bottle library. Add
bottlehere.Resource Type: Select Public Resources. For Instance Type, select
ecs.gn7i-c8g1.2xlarge.Configure a system disk: Set the size to 20 GB.
The image is large. An insufficient system disk size may cause the instance to fail to start. Set the size to 20 GB or more.
Click Deploy to create the service. The service takes about 5 minutes to create. When the status changes to Running, the deployment is successful.
Get the service endpoint and token. On the service details page, click View Endpoint Information to get the Internet Endpoint and Token.
Call the service. Replace the Internet Endpoint and Token in the following code with your actual values, and then run the code.
import requests """ Test image URLs: Label is 7 http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_7_No_0.jpg Label is 2 http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_2_No_1.jpg Label is 1 http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_1_No_2.jpg Label is 0 http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_0_No_3.jpg Label is 4 http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_4_No_4.jpg Label is 9 http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_9_No_5.jpg """ image_url = 'http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_7_No_0.jpg' # The client downloads the image and obtains the binary data. img_response = requests.get(image_url, timeout=10) # Automatically check if the request was successful based on the status code. img_response.raise_for_status() img_bytes = img_response.content # Header information. Replace <your_token> with your actual token. # In production environments, set the token as an environment variable to prevent sensitive information leaks. # For more information about how to configure environment variables, see https://help.aliyun.com/en/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems. headers = {"Authorization": "<your_token>"} # Send the binary data as the body of a POST request to the model service. resp = requests.post('<your_internet_endpoint>/predict_image', data=img_bytes, headers=headers) print(resp.json())Returned result:
{"prediction": 7}
The EAS service in this tutorial uses public resources and is billed on a pay-as-you-go basis. To avoid ongoing charges, stop or delete the service on the service list page when you no longer need it.
Related documents
For troubleshooting DSW startup failures, see Create a DSW instance.
For DSW billing details, see Data Science Workshop (DSW) billing.
For DSW core features, see DSW overview.
To access DSW web services from the internet, see Access a service in an instance over the internet.
For EAS core features, see EAS overview.












