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.import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.utils.data import DataLoader from torchvision import datasets, transforms from torch.utils.tensorboard import SummaryWriter # Hyperparameters batch_size = 64 # Batch size for training learning_rate = 0.01 # Learning rate num_epochs = 5 # Number of training epochs # Check if GPU is available device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')Train Epoch: 3 [57600/60000 (96%)] Loss: 0.116840 Validation Loss: 0.0753, Accuracy: 9750/10000 (98%) Model saved with accuracy: 97.50% Train Epoch: 4 [0/60000 (0%)] Loss: 0.155105 Train Epoch: 4 [6400/60000 (11%)] Loss: 0.025345 Train Epoch: 4 [12800/60000 (21%)] Loss: 0.063321 Train Epoch: 4 [19200/60000 (32%)] Loss: 0.129752 Train Epoch: 4 [25600/60000 (43%)] Loss: 0.021768 Train Epoch: 4 [32000/60000 (53%)] Loss: 0.127890 Train Epoch: 4 [38400/60000 (64%)] Loss: 0.049701 Train Epoch: 4 [44800/60000 (75%)] Loss: 0.025785 Train Epoch: 4 [51200/60000 (85%)] Loss: 0.042838 Train Epoch: 4 [57600/60000 (96%)] Loss: 0.064462 Validation Loss: 0.0666, Accuracy: 9767/10000 (98%) Model saved with accuracy: 97.67% Training complete. writer.close()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 --logdir=/mnt/workspace/output/runs TensorFlow installation not found - running with reduced feature set. NOTE: Using experimental fast data loading logic. To disable, pass "--load_fast=false" and report issues on GitHub. More details: https://github.com/tensorflow/tensorboard/issues/4784 Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all TensorBoard 2.19.0 at http://localhost:6006/ (Press CTRL+C to quit) ^CTensorBoard 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.
Click the Run button on the left to execute the following code:
import torch import torch.nn as nn import torch.nn.functional as F from torchvision import datasets, transforms import matplotlib.pyplot as plt # Define the same model structure as used during training class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5)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.
!cp -r /mnt/workspace/output/ /mnt/data/Go to the OSS console to view the copied files.
The OSS storage path
pai_test/output/contains theruns/folder and thebest_model.pthmodel file.
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
EAS (Elastic Algorithm Service) allows you to deploy trained models as an online inference service or an AI web application. EAS supports heterogeneous resources and combines features like auto scaling, one-click stress testing, canary release, and real-time monitoring to ensure service stability in high-concurrency scenarios at a lower cost.
This tutorial provides a web API for the model. Run the following cell to copy the API code to OSS.
# Download the web API code web.py for model prediction !curl -f -o web.py "http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/web.py" # Download the test code for the model service !curl -f -o request_web.py "http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/request_web.py" # Copy web.py to OSS !cp /mnt/workspace/web.py /mnt/data/web.py(Optional) Verify the web API in DSW. Run the following cell to install dependencies and start the service.
# Install the bottle package !pip install bottle # Start the web.py service !python web.py Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ Requirement already satisfied: bottle in /usr/local/lib/python3.11/site-packages (0.13.4) WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv [notice] A new release of pip is available: 23.3.2 -> 25.1.1 [notice] To updateTest 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.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.jpgReturned 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.
On the Instance List page of DSW, find the target instance and locate the Image Version column. Click the copy icon next to the image address to copy it.
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.
