如何在Sagemaker训练作业中安装模块?

11

我觉得我的问题表述可能不正确,但是我有一个Jupyter笔记本,在其中启动了一个使用我编写的Python训练脚本进行TensorFlow训练的作业。

该训练脚本需要某些模块。似乎我的Sagemaker训练作业失败了,因为一些模块不存在。

我该如何确保我的训练作业脚本具备所需的所有模块?

编辑

这些模块中的一个示例是keras

奇怪的是,我可以在jupyter笔记本中import keras,但当这个导入语句在我的训练脚本中时,我会收到No module named keras错误。

6个回答

5

如果您想安装多个软件包,一种方法是将其升级到Sagemaker Python SDK v2。这样,您可以在笔记本所在目录中创建一个requirements.txt文件,并运行训练。Sagemaker会自动处理安装。

如果您想保留v1 SDK,请将以下片段添加到入口点脚本中。

import subprocess
import sys

def install(package):
    subprocess.check_call([sys.executable, "-q", "-m", "pip", "install", package])
    
install('keras')

5

模块脚本在docker容器中运行,显然该容器没有安装依赖包。另一方面,Jupyter笔记本已经预先安装了keras。 实现这个目标的简单方法是创建一个包含所有依赖项的requirements.txt文件,并在创建模型时将其传递给它。

env = {
    'SAGEMAKER_REQUIREMENTS': 'requirements.txt', # path relative to `source_dir` below.
}
sagemaker_model = TensorFlowModel(model_data = 's3://mybucket/modelTarFile,
                                  role = role,
                                  entry_point = 'entry.py',
                                  code_location = 's3://mybucket/runtime-code/',
                                  source_dir = 'src',
                                  env = env,
                                  name = 'model_name',
                                  sagemaker_session = sagemaker_session,
                                 )

什么是TensorFlowModel?它和sagemaker.tensorflow是一样的吗?我无法将env参数传递给它。 - kane
TensorFlowModel是SageMaker Python SDK中指定的高级Python类。它负责封装调用SageMaker和在其中部署模型所需的元数据。https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/model.py#L41 - ByungWook
1
谢谢,这个运行得非常好。你知道有没有关于envSAGEMAKER_REQUIRMENTS的文档吗? - alvas

3
EstimatorBase类(以及TensorFlow类)接受参数dependencies,您可以使用以下方式将您的requirements.txt传递给它:
estimator = TensorFlow(
    dependencies=['requirements.txt'],  # copies this file
)

e.g.

estimator = TensorFlow(
    entry_point='src/train.py',
    dependencies=['requirements.txt'],  # copies this file
)

或者

estimator = TensorFlow(
    source_dir='src',  # this copies the entire src folder
    entry_point='train.py',  # when using source_dir has to be directly under that dir
    dependencies=['requirements.txt'],  # copies this file
)

这将把requirements.txt文件与训练代码一起复制到您的sourcedir.tar.gz中。

  • 这可能只适用于较新的镜像版本。我读到在旧版本中,您可能需要将requirements.txt文件放在与训练代码相同的文件夹中。

如果这不起作用,您可以使用pip download在本地下载在requirements.txt中定义的依赖项,然后使用dependencies参数指定您下载依赖项的文件夹。


2
  1. You can upload your requirements.txt file to s3 bucket which can be accessible by sagemaker and download the file to your working directory of the container using boto3. Install the libraries from requirements.txt the entry file.

        import os
        import boto3
    
        s3 = boto3.client('s3')
        s3.download_file('BUCKET_NAME', 'OBJECT_NAME', '/opt/ml/code/requirements.txt')
        os.command('pip install -r /opt/ml/code/requirements.txt')
    
  2. The other way you can do it is by building your own container using bring your own algorithm option provided by aws.

Ref-links:
https://github.com/awslabs/amazon-sagemaker-examples/blob/master/advanced_functionality/scikit_bring_your_own/scikit_bring_your_own.ipynb


1
另一个选项是在你的 entry_point .py 文件中添加。
import os

if __name__ == "__main__":
    os.system('pip install mymodule')
    import mymodule
    # rest of code goes here

这对于像pyparsing这样的简单模块对我很有帮助,但我认为对于keras,最好使用已经预安装了keras的Tensorflow容器,如上所述。

-1

除非是本地模式,否则笔记本实例上的环境与SageMaker上的训练作业环境是独立的。

如果您正在使用自定义的Docker镜像,则很可能您的Docker镜像没有安装Keras。

如果您正在使用SageMaker预定义的TensorFlow容器,那么最有可能是通过以下代码调用:

https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/estimator.py#L170

TensorFlow(entry_point='training_code.py',
           blah,
           blah
          )

然后您需要在该容器中安装依赖项。目前,SageMaker 上 TensorFlow 的训练有两种模式:“框架”和“脚本”模式。


如果通过“框架”模式进行培训,该模式仅适用于1.12及以下版本,则您将受到使用此处定义的keras_model_fn的限制: https://github.com/aws/sagemaker-python-sdk/tree/v1.12.0/src/sagemaker/tensorflow#preparing-the-tensorflow-training-script 通过传递requirements.txt文件来安装依赖项。
在TensorFlow 1.11及以上版本中引入了“脚本模式”: https://github.com/aws/sagemaker-python-sdk/tree/master/src/sagemaker/tensorflow#training-with-tensorflow “脚本”模式不支持Requirements.txt,建议您在用户脚本中安装依赖项,即包含所有Keras代码的Python文件。
如果有任何需要澄清的地方,请告诉我。
例如:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接