1

我希望为 SageMaker 中的服务端点提供一些超参数。训练实例可以使用以下超参数访问输入参数:

estimator = TensorFlow(entry_point='autocat.py',
                       role=role,
                       output_path=params['output_path'],
                       code_location=params['code_location'],
                       train_instance_count=1,
                       train_instance_type='ml.c4.xlarge',
                       training_steps=10000,
                       evaluation_steps=None,
                       hyperparameters=params)

但是,在部署端点时,无法传入用于控制函数中数据处理的input_fn(serialized_input, content_type)参数。

将参数传递给服务实例的最佳方式是什么?类中定义的source_dir参数是否sagemaker.tensorflow.TensorFlow复制到服务实例?如果是这样,我可以使用 config.yml 或类似的。

4

3 回答 3

1

啊,我遇到了与您类似的问题,我需要从 S3 下载一些东西以在 input_fn 中使用以进行推理。就我而言,它是一本字典。

三个选项:

  1. 使用您的 config.yml 方法,并在任何函数声明之前从您的入口点文件中下载并导入 s3 文件。这将使其可用于 input_fn
  2. 继续使用超参数方法,下载并导入矢量化器,serving_input_fn并通过全局变量使其可用,以便可以input_fn访问它。
  3. 训练前从 s3 下载文件,直接包含在 source_dir 中。

仅当您不需要在初始训练后单独更改矢量化器时,选项 3 才有效。

不管你做什么,不要直接在 input_fn 中下载文件。我犯了那个错误,性能很糟糕,因为每次调用端点都会导致 s3 文件被下载。

于 2018-04-13T03:56:54.357 回答
0

超参数用于训练阶段,以允许您调整(超参数优化 - HPO)您的模型。一旦你有一个训练有素的模型,推理就不需要这些超参数。

当您想将功能传递给服务实例时,您通常在调用端点 API 调用的每个请求的 BODY 中执行此操作(例如,请参见此处:https ://docs.aws.amazon.com/sagemaker/latest/dg /tf-example1-invoke.html)或调用 SageMaker python SDK 中的预测包装器(https://github.com/aws/sagemaker-python-sdk/tree/master/src/sagemaker/tensorflow)。您可以在示例笔记本中看到此类示例(https://github.com/awslabs/amazon-sagemaker-examples/blob/master/advanced_functionality/tensorflow_iris_byom/tensorflow_BYOM_iris.ipynb

于 2018-03-24T15:11:58.080 回答
0

是的,一种选择是将您的配置文件添加source_dirinput_fn.

另一种选择是使用serving_input_fn(hyperparameters). 该函数将 TensorFlow 模型转换为 TensorFlow 服务模型。例如:

def serving_input_fn(hyperparameters):

    # gets the input shape from the hyperparameters
    shape = hyperparameters.get('input_shape', [1, 7])

    tensor = tf.placeholder(tf.float32, shape=shape)
    # returns the ServingInputReceiver object.

    return build_raw_serving_input_receiver_fn({INPUT_TENSOR_NAME: tensor})()

于 2018-04-09T04:33:16.013 回答