如何在 Amazon Sagemaker 处理作业中安装 python 包?
How to install python packages within Amazon Sagemaker Processing Job?
我正在尝试在 Amazon Sagemekar 中创建一个 Sklearn 处理作业,以便在我进行模型训练之前对我的输入数据执行一些数据转换。
我写了一个自定义 python 脚本 preprocessing.py
来完成需要的工作。我在这个脚本中使用了一些 python 包。 Here is the Sagemaker example I followed.
当我尝试提交处理作业时出现错误 -
............................Traceback (most recent call last):
File "/opt/ml/processing/input/code/preprocessing.py", line 6, in <module>
import snowflake.connector
ModuleNotFoundError: No module named 'snowflake.connector'
我知道我的处理作业找不到这个包,我需要安装它。我的问题是如何使用 Sagemaker 处理作业 API 完成此操作?理想情况下,应该有一种方法可以在 API 调用中定义 requirements.txt
,但我在文档中没有看到这样的功能。
我知道我可以 create a custom Image with relevant packages 并稍后在处理作业中使用此图像,但是对于应该内置的东西来说这似乎工作太多了?
是否有 easier/elegant 方法来安装 Sagemaker 处理作业中所需的包?
一种方法是 call pip from Python:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
另一种方法是使用 SKLearn Estimator(训练作业)来做同样的事情。您可以提供 source_dir
,其中可以包含一个 requirements.txt
文件,这些要求将为您安装
estimator = SKLearn(
entry_point="foo.py",
source_dir="./foo", # no trailing slash! put requirements.txt here
framework_version="0.23-1",
role = ...,
instance_count = 1,
instance_type = "ml.m5.large"
)
我正在尝试在 Amazon Sagemekar 中创建一个 Sklearn 处理作业,以便在我进行模型训练之前对我的输入数据执行一些数据转换。
我写了一个自定义 python 脚本 preprocessing.py
来完成需要的工作。我在这个脚本中使用了一些 python 包。 Here is the Sagemaker example I followed.
当我尝试提交处理作业时出现错误 -
............................Traceback (most recent call last):
File "/opt/ml/processing/input/code/preprocessing.py", line 6, in <module>
import snowflake.connector
ModuleNotFoundError: No module named 'snowflake.connector'
我知道我的处理作业找不到这个包,我需要安装它。我的问题是如何使用 Sagemaker 处理作业 API 完成此操作?理想情况下,应该有一种方法可以在 API 调用中定义 requirements.txt
,但我在文档中没有看到这样的功能。
我知道我可以 create a custom Image with relevant packages 并稍后在处理作业中使用此图像,但是对于应该内置的东西来说这似乎工作太多了?
是否有 easier/elegant 方法来安装 Sagemaker 处理作业中所需的包?
一种方法是 call pip from Python:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
另一种方法是使用 SKLearn Estimator(训练作业)来做同样的事情。您可以提供 source_dir
,其中可以包含一个 requirements.txt
文件,这些要求将为您安装
estimator = SKLearn(
entry_point="foo.py",
source_dir="./foo", # no trailing slash! put requirements.txt here
framework_version="0.23-1",
role = ...,
instance_count = 1,
instance_type = "ml.m5.large"
)