Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Creating a Training Job

Updated on 2023-06-27 GMT+08:00

Sample Code

In ModelArts notebook, you do not need to enter authentication parameters for session authentication. For details about session authentication of other development environments, see Session Authentication.

NOTE:

ModelArts SDK cannot be used to create training jobs using algorithms subscribed to in AI Gallery.

  • Example 1: Create a training job using a common AI engine.

    If both framework_type and framework_version are specified in estimator, a training job will be created using a common AI engine.

    from modelarts.session import Session
    from modelarts.train_params import TrainingFiles
    from modelarts.train_params import OutputData
    from modelarts.train_params import InputData
    from modelarts.estimatorV2 import Estimator
    session = Session()
    # Parameters received in the training script (set based on the site requirements):
    
    parameters = [{"name": "mod", "value":"gpu"}, 
                  {"name": "epoc_num", "value":2}]
    estimator = Estimator(session=session,
                          training_files=TrainingFiles(code_dir= "obs://bucket_name/code_dir/", boot_file="boot_file.py"),
                          outputs=[OutputData(obs_path="obs://bucket_name/output/", name="output_dir")],
                          parameters=parameters,
                          framework_type='PyTorch',                      # Common AI engine
                          framework_version='PyTorch-1.4.0-python3.6',   # Version of the AI engine
                          train_instance_type="modelarts.p3.large.public",
                          train_instance_count=1,
                          log_url="obs://bucket_name/log/",
                          env_variables={"USER_ENV_VAR": "customize environment variable"},
                          working_dir="/home/ma-user/modelarts/user-job-dir",
                          local_code_dir="/home/ma-user/modelarts/user-job-dir",
                          job_description='This is an image net train job')
    job_instance = estimator.fit(inputs=[InputData(obs_path="obs://bucket_name/input/", name="data_url")],
                                 job_name="job_name_1")
  • Example 2: Create a training job using a custom image.

    If both user_image_url and user_command are specified in estimator, a training job will be created using a custom image and started using a custom boot command.

    from modelarts.session import Session
    from modelarts.train_params import TrainingFiles
    from modelarts.train_params import OutputData
    from modelarts.train_params import InputData
    from modelarts.estimatorV2 import Estimator
    session = Session()
    # Parameters received in the training script (set based on the site requirements):
    
    parameters = [{"name": "mod", "value":"gpu"}, 
                  {"name": "epoc_num", "value":2}]
    estimator = Estimator(session=session,
                          training_files=TrainingFiles(code_dir= "obs://bucket_name/code_dir/", boot_file="boot_file.py"),
                          outputs=[OutputData(obs_path="obs://bucket_name/output/", name="output_dir")],
                          parameters=parameters,
                          user_image_url="sdk-test/pytorch1_4:1.0.1",     # URL of the custom image
                          user_command="/home/ma-user/anaconda3/envs/PyTorch-1.4/bin/python /home/ma-user/modelarts/user-job-dir/train/test-pytorch.py",  # Custom boot command
                          train_instance_type="modelarts.p3.large.public",
                          train_instance_count=1,
                          log_url="obs://bucket_name/log/",
                          env_variables={"USER_ENV_VAR": "customize environment variable"},
                          working_dir="/home/ma-user/modelarts/user-job-dir",
                          local_code_dir="/home/ma-user/modelarts/user-job-dir",
                          job_description='This is an image net train job')
    job_instance = estimator.fit(inputs=[InputData(obs_path="obs://bucket_name/input/", name="data_url")],
                                 job_name="job_name_2")
  • Example 3: Creating a training job in a dedicated resource pool
    from modelarts.session import Session
    from modelarts.train_params import TrainingFiles
    from modelarts.train_params import OutputData
    from modelarts.train_params import InputData
    from modelarts.estimatorV2 import Estimator
    session = Session()
    # Parameters received in the training script (set based on the site requirements):
    
    parameters = [{"name": "mod", "value":"gpu"}, 
                  {"name": "epoc_num", "value":2}]
    estimator = Estimator(session=session,
                          training_files=TrainingFiles(code_dir= "obs://bucket_name/code_dir/", boot_file="boot_file.py"),
                          outputs=[OutputData(obs_path="obs://bucket_name/output/", name="output_dir")],
                          parameters=parameters,
                          framework_type='PyTorch',                       
                          framework_version='PyTorch-1.4.0-python3.6',    
                          pool_id="your pool id",                                 # Dedicated resource pool ID
                          train_instance_type="modelarts.pool.visual.xlarge",     # VM flavor of the dedicated pool
                          train_instance_count=1,
                          log_url="obs://bucket_name/log/",
                          env_variables={"USER_ENV_VAR": "customize environment variable"},
                          working_dir="/home/ma-user/modelarts/user-job-dir",
                          local_code_dir="/home/ma-user/modelarts/user-job-dir",
                          job_description='This is an image net train job')
    job_instance = estimator.fit(inputs=[InputData(obs_path="obs://bucket_name/input/", name="data_url")],
                                 job_name="job_name_3")
  • Example 4: Create a training job using a dataset.
    from modelarts.session import Session
    from modelarts.train_params import TrainingFiles
    from modelarts.train_params import OutputData
    from modelarts.train_params import InputData
    from modelarts.estimatorV2 import Estimator
    session = Session()
    # Parameters received in the training script (set based on the site requirements):
    parameters = [{"name": "model_name", "value":"s"}, 
                  {"name": "batch-size", "value": 32},
                  {"name": "epochs", "value":100},
                  {"name": "img-size", "value":"640,640"} ]
    estimator = Estimator(session=session,
                          training_files=TrainingFiles(code_dir= "obs://bucket_name/code_dir/", boot_file="boot_file.py"),
                          outputs=[OutputData(obs_path="obs://bucket_name/output/", name="output_dir")],
                          parameters=parameters,
                          framework_type='PyTorch',                       # Common AI engine
                          framework_version='PyTorch-1.4.0-python3.6',    # Version of the AI engine
                          train_instance_type="modelarts.p3.large.public",
                          train_instance_count=1,
                          log_url="obs://bucket_name/log/",
                          working_dir="/home/ma-user/modelarts/user-job-dir",
                          local_code_dir="/home/ma-user/modelarts/user-job-dir",
                          job_description='This is an image net train job')
    job_instance = estimator.fit(dataset_id="your dataset id",
                                 dataset_version_id="your dataset version id",
                                 job_name="job_name_5")

Parameters

Table 1 Estimator request parameters

Parameter

Mandatory

Type

Description

session

Yes

Object

Session object. For details about the initialization method, see Session Authentication.

training_files

No

TrainingFiles Object

Path to the training script in OBS. For details, see Table 2.

outputs

No

Array of OutputData objects

Training output path. For details, see Table 3.

parameters

No

JSON Array

Running parameters of a training job. The format is as follows:

[{"name":"your name", "value": "your value"}]. The value can be a string or an integer.

train_instance_type

Yes

String

Resource flavor selected for a training job. For details, see Obtaining Resource Flavors.

train_instance_count

Yes

Int

Number of compute nodes in a training job

framework_type

No

String

Engine type selected for a training job. For details, see Obtaining Engine Types.

framework_version

No

String

Engine version selected for a training job. For details, see Obtaining Engine Types.

user_image_url

No

String

SWR URL of the custom image used by a training job

user_command

No

String

Command for starting a training job created using a custom image

log_url

No

String

OBS path for storing training job logs, for example, obs://xx/yy/zz/

local_code_dir

No

String

Local directory to the training container to which the algorithm code directory is downloaded. Note:

  • The directory must be under /home.
  • In v1 compatibility mode, this parameter does not take effect.
  • When code_dir is prefixed with file://, this parameter does not take effect.

working_dir

No

String

Work directory where an algorithm is executed. Note that this parameter does not take effect in v1 compatibility mode.

job_description

No

String

Description of a training job

volumes

No

JSON Array

Information of the disks attached for a training job in the following example format:

[{

"nfs": {

"local_path": "/xx/yy/zz",

"read_only": False,

"nfs_server_path": "xxx.xxx.xxx.xxx:/"

}

}]

env_variables

No

Dict

Environment variables of a training job

pool_id

No

String

ID of the resource pool for a training job. To obtain the ID, do as follows: Log in to the ModelArts management console, choose Dedicated Resource Pools in the navigation pane on the left, and view the resource pool ID in the dedicated resource pool list.

Table 2 Parameters for initializing TrainingFiles

Parameter

Mandatory

Type

Description

code_dir

Yes

String

Code directory of a training job, which is an OBS path and must start with obs:/, for example, obs://xx/yy/

boot_file

Yes

String

Boot file of a training job, which must be stored in the code directory. You can enter a relative path, for example, boot_file.py, or an absolute path, for example, obs://xx/yy/boot_file.py.

Table 3 Parameters for initializing OutputData

Parameter

Mandatory

Type

Description

obs_path

Yes

String

OBS path to which data is exported

name

Yes

String

Keyword parameter name of the output data, for example, output_dir

Table 4 fit request parameters

Parameter

Mandatory

Type

Description

inputs

No

Array of InputData Object

Input data of a training job stored in OBS Either inputs or dataset_id/dataset_version_id can be configured.

wait

No

Boolean

Whether to wait for the completion of a training job. It defaults to False.

job_name

No

String

Name of a training job

show_log

No

Boolean

Whether to output training job logs after a job is submitted. It defaults to False.

dataset_id

No

String

Dataset ID of a training job. This parameter must be used with dataset_version_id, but cannot be used with inputs.

dataset_version_id

No

String

Dataset version ID of a training job. This parameter must be used with dataset_id, but cannot be used with inputs.

Table 5 Parameters for initializing InputData

Parameter

Mandatory

Type

Description

obs_path

Yes

String

OBS path to the dataset required by a training job, for example, obs://xx/yy/

name

Yes

String

Keyword parameter name of the input data, for example, data_url.

Table 6 Response for creating a training job

Parameter

Type

Description

TrainingJob

Object

Training object, which contains attributes such as job_id. When you perform operations on a training job, for example, obtain information of, update, or delete a training job, you can use job_instance.job_id to obtain the ID of the training job.

Table 7 Response for the failure to call a training API

Parameter

Type

Description

error_msg

String

Error message when calling an API failed. This parameter is unavailable if an API is successfully called.

error_code

String

Error code when calling an API failed. For details, see Error Codes. This parameter is unavailable if an API is successfully called.

error_solution

String

Solution to an API calling failure. This parameter is unavailable if an API is successfully called.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback