Using JupyterLab to Develop and Debug Code Online
During AI model development, developers typically require a robust interactive development environment to support diverse development needs, such as writing notebooks, operating terminals, and editing Markdown text. However, traditional development environments often fail to meet these diverse requirements, resulting in low development efficiency. How can developers improve the efficiency and flexibility of AI model development? ModelArts supports opening notebook instances online via the JupyterLab tool to develop AI models based on engines such as PyTorch, TensorFlow, and MindSpore. This not only satisfies developers' diverse needs but also significantly enhances development efficiency.
Prerequisites
You have created a notebook instance and the instance is in the Running state. For details, see Creating a Notebook Instance.
Constraints
- There is no strict limit on the size of files loaded in JupyterLab. However, the actual processing capability is affected by system resources and configurations. If a large text file, for example, larger than 100 MB, is opened in the browser on the left of JupyterLab, the memory may be insufficient, the GUI may respond slowly, or the kernel may be interrupted. To ensure smooth and stable usage, you are advised to open a single file no larger than 100 MB.
- The system can process at most 40 JupyterLab requests (average) per second for a long time or at most 10 JupyterLab requests in a short time. Generally, if more than 10 operations are performed on JupyterLab in the same resource pool within a short period of time, flow control will be triggered.
Using JupyterLab
- Log in to the ModelArts console. In the navigation pane on the left, perform the following operations:
- New console: Choose Model Build > Notebook.
- Old console: Choose Development Space > Notebook.
- In the Operation column of the target notebook instance, click Access Environment. In the Access Method dialog box, click Access on the right of JupyterLab Access in the WebIDE tab.
- On the ModelArts Launcher page, perform required operations. For details, see JupyterLab Documentation.
The notebook and console kernels and versions displayed on the ModelArts Launcher page vary depending on the AI engine based on which a notebook instance is created. Figure 2 shows an example only. Obtain the notebook and console kernels and versions on the management console.
- Upload training data and code files to JupyterLab. For details, see Uploading Files from a Local Path to JupyterLab. You can also create a file in JupyterLab to debug code. For details, see JupyterLab Code Example.Figure 3 Button for uploading a file
- In the navigation pane on the left, double-click the uploaded code file, compile the file in JupyterLab, and debug it. For details about how to use JupyterLab, see Common Functions of JupyterLab.
If your code file is in .py format, open a new .ipynb file and run the %load main.py command to load the content of the .py file to the .ipynb file for encoding and debugging.
- In JupyterLab, call the ModelArts SDK to create a training job for in-cloud training.
For details, see Creating a Training Job.
JupyterLab Code Example
This example demonstrates basic data processing and visualization. It generates some random data, fits it using a linear regression model, and finally plots the resulting graph.
- In the Notebook area on the ModelArts Launcher page, click the target runtime environment.
The following section uses the PyTorch runtime environment as an example.
Figure 4 Opening PyTorch
- Execute the command below to check whether the numpy and pandas libraries are installed. If they are not installed, the specified versions will be installed automatically (version numbers can be modified as needed).
!pip show numpy pandas || pip install -q numpy==1.24.2 pandas==1.5.1
As shown in the figure below, the appearance of version information indicates that the numpy and pandas libraries are already installed.
Figure 5 Viewing library information
To the left of the command block, click
to execute the command. - Run the code below.
This code generates some random data, fits it using a linear regression model, and plots the results.
# Import necessary libraries import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # Generate sample data np.random.seed(0) # Set the random seed to ensure reproducibility X = 2 * np.random.rand(100, 1) # Generate 100 random numbers in the range 0 to 2 y = 4 + 3 * X + np.random.randn(100, 1) # Generate the target variable with some noise # Create a linear regression model model = LinearRegression() # Train the model model.fit(X, y) # Predict X_new = np.array([[0], [2]]) # Generate new input data points y_pred = model.predict(X_new) # Use the model to make predictions # Plot the results plt.scatter(X, y, color='blue', label='Data') # Plot the original data points plt.plot(X_new, y_pred, color='red', linewidth=2, label='Linear Regression') # Plot the linear regression line plt.xlabel('X') # Set the x-axis label plt.ylabel('y') # Set the y-axis label plt.legend() # Show the legend plt.show() # Display the plotFigure 6 Execution result
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot
