AI开发平台MODELARTS-使用ModelArts SDK:示例代码

时间:2024-06-17 17:55:57

示例代码

  • 创建训练作业
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    from modelarts.session import Session
    from modelarts.estimator import Estimator
    session = Session()
    estimator = Estimator(
                          modelarts_session=session,
                          framework_type='PyTorch',                                     # AI引擎名称
                          framework_version='PyTorch-1.0.0-python3.6',                  # AI引擎版本
                          code_dir='/obs-bucket-name/src/',                             # 训练脚本目录
                          boot_file='/obs-bucket-name/src/pytorch_sentiment.py',        # 训练启动脚本目录 
                          log_url='/obs-bucket-name/log/',                              # 训练日志目录
                          hyperparameters=[
                                           {"label":"classes",
                                            "value": "10"},    
                                           {"label":"lr",
                                            "value": "0.001"}
                                           ],
                          output_path='/obs-bucket-name/output/',                         # 训练输出目录
                          train_instance_type='modelarts.vm.xxx.xxx',                  # 训练环境规格
                          train_instance_count=1,                                       # 训练节点个数
                          job_description='pytorch-sentiment with ModelArts SDK')       # 训练作业描述
    job_instance = estimator.fit(inputs='/obs-bucket-name/data/train/', wait=False, job_name='my_training_job')
    
  • 查询模型列表
    1
    2
    3
    4
    from modelarts.session import Session 
    from modelarts.model import Model
    session = Session() 
    model_list_resp = Model.get_model_list(session, model_status="published", model_name="digit", order="desc")
    
  • 查询服务详情
    1
    2
    3
    4
    5
    from modelarts.session import Session
    from modelarts.model import Predictor
    session = Session()
    predictor_instance = Predictor(session, service_id="input your service_id")
    predictor_info_resp = predictor_instance.get_service_info()
    
support.huaweicloud.com/devtool-modelarts/modelarts_30_0030.html