[英]Access Parameter's value directly in AWS Sagemaker Pipeline
Inside a function that returns a Pipeline, where a Parameter is defined, eg (taken from here )在返回管道的函数内部,其中定义了参数,例如(取自此处)
def get_pipeline(...):
foo = ParameterString(
name="Foo", default_value="foo"
)
# pipeline's steps definition here
step = ProcessingStep(name=...,
job_arguments=["--foo", foo]
)
return pipeline = Pipeline(
name=pipeline_name,
parameters=[...],
steps=[...],
sagemaker_session=sagemaker_session,
)
I know I can access the default value of a parameter by simply calling foo.default_value
, but how can I access its value when the default value is overridden at the runtime, eg by using我知道我可以通过简单地调用
foo.default_value
来访问参数的默认值,但是当默认值在运行时被覆盖时我如何访问它的值,例如通过使用
pipeline.start(parameters=dict(Foo='bar'))
? ?
My assumption is that in that case I don't want to read the default value, since it has been overridden, but the Parameter API is very limited and does not provided anything expect for name
and default_value
.我的假设是,在那种情况下,我不想读取默认值,因为它已被覆盖,但参数 API非常有限,并且没有提供
name
和default_value
所需的任何内容。
As written in the documentation :如文档中所写:
Pipeline parameters can only be evaluated at run time.
管道参数只能在运行时进行评估。 If a pipeline parameter needs to be evaluated at compile time, then it will throw an exception.
如果需要在编译时评估管道参数,则会抛出异常。
In addition, there are some limitations: Not all built-in Python operations can be applied to parameters .此外,还有一些限制: 并非所有内置的 Python 操作都可以应用于参数。
An example taken from the link above:从上面的链接中获取的示例:
# An example of what not to do
my_string = "s3://{}/training".format(ParameterString(name="MyBucket", default_value=""))
# Another example of what not to do
int_param = str(ParameterInteger(name="MyBucket", default_value=1))
# Instead, if you want to convert the parameter to string type, do
int_param.to_string()
# A workaround is to use Join
my_string = Join(on="", values=[
"s3://",
ParameterString(name="MyBucket", default_value=""),
"/training"]
)
Personally, I prefer to pass the value directly when you get the pipeline definition before the start:就个人而言,我更喜欢在开始之前获取管道定义时直接传递值:
def get_pipeline(my_param_hardcoded, ...):
# here you can use my_param_hardcoded
my_param = ParameterString(
name="Foo", default_value="foo"
)
# pipeline's steps definition here
return pipeline = Pipeline(
name=pipeline_name,
parameters=[my_param, ...],
steps=[...],
sagemaker_session=sagemaker_session,
)
return pipeline
pipeline = get_pipeline(my_param_hardcoded, ...)
pipeline.start(parameters=dict(Foo=my_param_hardcoded))
Obviously this is not a really elegant way, but I do not think it is conceptually wrong because after all it is a parameter that will be used to manipulate the pipeline and cannot be pre-processed beforehand (eg in a configuration file).显然这不是一种真正优雅的方式,但我不认为这在概念上是错误的,因为毕竟它是一个将用于操纵管道的参数并且不能预先进行预处理(例如在配置文件中)。
An example of use is the creation of a name which can be based on the pipeline_name (which is clearly passed in the get_pipeline() and a pipeline parameter).一个使用示例是创建一个名称,该名称可以基于 pipeline_name(在 get_pipeline() 和管道参数中明确传递)。 For example, if we wanted to create a custom name for a step, it could be given by the concatenation of the two strings, and this cannot happen at runtime but must be done with this trick.
例如,如果我们想为一个步骤创建一个自定义名称,它可以通过两个字符串的连接来给出,这不能在运行时发生,但必须使用这个技巧来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.