All Products
Search
Document Center

Platform For AI:Quick start: Interactive modeling with DSW

Last Updated:Oct 21, 2026

Develop, train, and deploy deep learning models in the cloud by using the built-in Notebook or VSCode in Data Science Workshop (DSW). This topic uses MNIST handwritten digit recognition as an example to demonstrate the complete workflow.

Note

MNIST handwritten digit recognition is a classic introductory task in deep learning. The goal is to train a model to recognize the 10 handwritten digits from 0 to 9.

Prerequisites

PAI is activated and a workspace is created in your Alibaba Cloud account. If PAI is not activated, go to the PAI console, select the target region in the upper-left corner of the page, and complete the one-click authorization and activation.

Billing

The DSW instance and the EAS model service in this topic are created in a public resource group and billed on a pay-as-you-go basis. For billing details, see DSW billing and EAS billing.

Create a DSW instance

  1. Go to the DSW page.

    1. Go to the PAI console.

    2. Select the target region in the upper-left corner of the page.

    3. Click Workspaces in the left-side navigation pane to go to the target workspace.

    4. In the left-side navigation pane, choose Model Training > Interactive Modeling (DSW) > Create an instance..

  2. In the Configure Instance wizard, configure the following parameters and keep the default values for other parameters:

    • Resource Type: Select Public Resources (pay-as-you-go).

    • Instance Type: Select ecs.gn7i-c8g1.2xlarge.

      If this instance type is unavailable, select another GPU instance type.
    • Image config: Select Alibaba Cloud Image, search for and select 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: Stores model development files. This topic uses OSS. Click OSS, click the image icon, select a Bucket, and create a directory (for example, pai_test).

      If no bucket is available in the current region, follow these steps to create one:

      (Optional) Create an OSS bucket

      1. OSS is activated.

      2. Go to the OSS console and click Create Bucket. Enter a Bucket Name, select the same region as PAI for Region, keep the default values for other parameters, and click Complete Creation.

        image

      • Uri: oss://**********oss-cn-hangzhou-internal.aliyuncs.com/pai_test/

      • Mount Path: /mnt/data/

  3. Click OK to create the instance.

    If the instance fails to start, see Create a DSW instance for troubleshooting.

Develop a model in DSW

  1. Click Open in the instance list to open the DSW development environment. On the launcher, click JupyterLab to create a Notebook development environment.

  2. Write the model code. This topic provides the MNIST training code: download mnist.ipynb, and then click the image icon in the upper-left corner of the Notebook to upload the file.

    image

  3. Train the model. Open mnist.ipynb, locate the cell that contains the training code, and click the image icon to run the cell. The code automatically downloads the MNIST dataset to the dataSet directory and saves the trained model to the output directory. Training takes about 10 minutes.

    image

    image

    The training output shows the accuracy of the model on the validation set, which reflects its generalization capability. In this example, the validation set accuracy is 98%. Proceed to the next step.

  4. View the training curves. Run the following cell and open the TensorBoard URL http://localhost:6006/.

    image

    TensorBoard displays the train_loss (training set loss) and validation_loss (validation set loss) curves.

    image

    After you view the curves, click the image icon in the cell to stop TensorBoard. If TensorBoard is not stopped, subsequent cells cannot be run.

    (Optional) Tune hyperparameters based on the loss curves

    Evaluate the training result based on how the loss values change:

    • Both train_loss and validation_loss continue to decrease before training ends (underfitting).

      Increase num_epochs (training epochs) or raise learning_rate (learning rate) to reduce underfitting.

    • train_loss continues to decrease but validation_loss rises before training ends (overfitting).

      Decrease num_epochs or lower learning_rate to prevent overfitting.

    • Both train_loss and validation_loss level off before training ends (good fit).

      The model has reached the desired state. Proceed to the next step.

  5. Test the model. Run the following cell to display 20 test images along with their actual labels and the model predictions.

    image

    Sample output:

    image

  6. Back up the model files. The DSW instance in this topic is created in a public resource group, and files are stored on a free cloud disk. If the instance is stopped for more than 15 days, the content on the cloud disk is automatically cleared. Copy the model files to OSS for later deployment with EAS.

    image

    Go to the OSS console to view the copied files:

    image

After the model is developed, you can deploy it as an online service. For more information, see Deploy the model service with EAS.

Important

The DSW instance in this topic is created in a public resource group and billed on a pay-as-you-go basis. When the instance is no longer needed, stop or delete it on the right side of the instance list page to avoid ongoing charges.

Deploy the 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.

  1. Write the web interface for the model and copy it to OSS. This topic provides the web interface code and the copy commands. Run the following cell to complete this step.

    image

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

    image

    Test the interface. On the launcher page, click Terminal to open a terminal and run python request_web.py. The following result is returned:

    {"prediction": 7}
    Note

    Accessing the web service in DSW over the Internet requires a VPC, a NAT gateway, and an Elastic IP (EIP). For more information, see Access services in an instance over the public network.

  3. Create the 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 keep the default values for other parameters:

    • Resource Type: Select Public Resources. For Instance Type, select ecs.gn7i-c8g1.2xlarge.

      If this instance type is unavailable, select another GPU instance type.
    • Deployment Method: Image-based Deployment

    • Image Configuration: Select Official Image, search for and select torcheasyrec:1.2.0-pytorch2.11.0-gpu-py311-cu126-ubuntu22.04.

      In a production environment, you typically develop and test code in DSW first, and then package the container environment and push it to Container Registry (ACR) for EAS to use during model deployment. For more information, see Create a DSW instance image.
    • Mount storage: The model files and web interface code are copied to OSS. Click OSS and select the corresponding path.

      • Uri: oss://*/pai_test/

      • Mount Path: /mnt/data/

    • Command to Run: web.py is mounted to /mnt/data/. The run command is python /mnt/data/web.py

    • Port Number: Enter port 9000, which is used by web.py.

    • Third-party Library Settings: Enter bottle torchvision.

      The selected official image does not include the bottle or torchvision libraries (as verified by testing). Therefore, enter bottle torchvision in the third-party library configuration.

    Click Deploy to create the service. It takes about 5 minutes to create the service. When the status changes to Running, the service is deployed.

  4. Get the calling information. On the service details page, click View Endpoint Information to obtain the Internet Endpoint and Token.

  5. Call the service. Replace the Internet Endpoint and Token in the following code with the 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 5
    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 to obtain the binary data
    img_response = requests.get(image_url, timeout=10)
    # Check whether the request succeeds based on the status code
    img_response.raise_for_status()
    img_bytes = img_response.content
    
    # In the header, replace <EAS_TOKEN> with the actual token
    # In production, we recommend that you set the token as an environment variable to prevent sensitive information leakage
    # For information about how to configure environment variables, see https://www.alibabacloud.com/help/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems
    headers = {"Authorization": "Token"}
    # Send the binary data as the body of the POST request to the model service
    resp = requests.post('public endpoint/predict_image', data=img_bytes, headers=headers)
    print(resp.json())

    The following result is returned:

    {"prediction": 7}
Important

The EAS service in this topic is created in a public resource group and billed on a pay-as-you-go basis. When the service is no longer needed, stop or delete it on the right side of the service list page to avoid ongoing charges.

References