gpt4 book ai didi

scikit-learn - Sagemaker批量转换 "ValueError: could not convert string to float"

转载 作者:行者123 更新时间:2023-12-02 03:00:57 27 4
gpt4 key购买 nike

我正在使用 sagemaker 并使用批量转换来运行本地变压器。但是,转换似乎没有调用我的自定义代码。

以下是SKlearn初始化

from sagemaker.sklearn.estimator import SKLearn
source_dir = 'train'
script_path = 'train.py'

sklearn = SKLearn(
entry_point=script_path,
train_instance_type="local_gpu",
source_dir=source_dir,
role=role,
sagemaker_session=sagemaker_session)
sklearn.fit({'train': "file://test.csv"})

train.py是一个Python脚本,用于加载训练数据,并将模型保存到S3

批量转换是:

transformer = sklearn.transformer(instance_count=1,
entry_point=source_dir+"/"+script_path,
instance_type='local_gpu',
strategy='MultiRecord',
assemble_with='Line'
)
transformer.transform("file://test_messages", content_type='text/csv', split_type='Line')
print('Waiting for transform job: ' + transformer.latest_transform_job.job_name)
transformer.wait()

file://test_messages 包含一个 csv,它是字符串列表

完整的错误是

algo-1-6c5rl_1  | 172.18.0.1 - - [30/Jan/2020:14:14:30 +0000] "GET /ping HTTP/1.1" 200 0 "-" "-"
algo-1-6c5rl_1 | 172.18.0.1 - - [30/Jan/2020:14:14:30 +0000] "GET /execution-parameters HTTP/1.1" 404 232 "-" "-"
algo-1-6c5rl_1 | 2020-01-30 14:14:30,846 ERROR - train - Exception on /invocations [POST]
algo-1-6c5rl_1 | Traceback (most recent call last):
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 93, in wrapper
algo-1-6c5rl_1 | return fn(*args, **kwargs)
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/sagemaker_sklearn_container/serving.py", line 56, in default_input_fn
algo-1-6c5rl_1 | return np_array.astype(np.float32) if content_type in content_types.UTF8_TYPES else np_array
algo-1-6c5rl_1 | ValueError: could not convert string to float: 'IMPORTANT - You could be entitled up to �3,160 in compensation from mis-sold PPI on a credit card or loan. Please reply PPI for info or STOP to opt out.'
algo-1-6c5rl_1 |
algo-1-6c5rl_1 | During handling of the above exception, another exception occurred:
algo-1-6c5rl_1 |
algo-1-6c5rl_1 | Traceback (most recent call last):
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
algo-1-6c5rl_1 | response = self.full_dispatch_request()
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
algo-1-6c5rl_1 | rv = self.handle_user_exception(e)
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
algo-1-6c5rl_1 | reraise(exc_type, exc_value, tb)
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
algo-1-6c5rl_1 | raise value
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
algo-1-6c5rl_1 | rv = self.dispatch_request()
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
algo-1-6c5rl_1 | return self.view_functions[rule.endpoint](**req.view_args)
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_transformer.py", line 200, in transform
algo-1-6c5rl_1 | self._model, request.content, request.content_type, request.accept
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_transformer.py", line 227, in _default_transform_fn
algo-1-6c5rl_1 | data = self._input_fn(content, content_type)
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 95, in wrapper
algo-1-6c5rl_1 | six.reraise(error_class, error_class(e), sys.exc_info()[2])
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/six.py", line 692, in reraise
algo-1-6c5rl_1 | raise value.with_traceback(tb)
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 93, in wrapper
algo-1-6c5rl_1 | return fn(*args, **kwargs)
algo-1-6c5rl_1 | File "/miniconda3/lib/python3.7/site-packages/sagemaker_sklearn_container/serving.py", line 56, in default_input_fn
algo-1-6c5rl_1 | return np_array.astype(np.float32) if content_type in content_types.UTF8_TYPES else np_array
algo-1-6c5rl_1 | sagemaker_containers._errors.ClientError: could not convert string to float: 'IMPORTANT - You could be entitled up to �3,160 in compensation from mis-sold PPI on a credit card or loan. Please reply PPI for info or STOP to opt out.'
algo-1-6c5rl_1 | 172.18.0.1 - - [30/Jan/2020:14:14:30 +0000] "POST /invocations HTTP/1.1" 500 290 "-" "-"
.Waiting for transform job: sagemaker-scikit-learn-2020-01-30-14-14-30-490

它似乎无法处理我的字符串。我在 train.py 中有代码来使用 TfidfVectorizer 转换字符串,但该代码没有被调用

最佳答案

我是 AWS SageMaker 的工程师。感谢您提供估算器/转换器设置的详细信息以及完整的错误日志。

查看具体错误,看起来 Scikit-learn 容器在 default_input_fn 中失败。值得庆幸的是,SageMaker Scikit-learn 是开源的,因此我们可以直接获取源代码 sagemaker_sklearn_container/serving.py#L56帮助理解它是如何工作的。

容器选择在发送到模型之前执行“默认”输入函数来处理输入。显然,默认实现不适用于您所需的输入格式。

与训练类似,您需要提供自定义 Python 代码来控制 SageMaker Scikit-learn 如何在服务/推理模式下处理您的模型。如果您想覆盖默认值,则需要在自定义 Python 代码中实现 input_fn。您可以选择将其添加到 train.py 脚本中,或在 Transformer 中传递不同的 Python 文件。

本文档应该有助于编写input_fn:https://sagemaker.readthedocs.io/en/stable/using_sklearn.html#process-input

如果您仍然遇到问题,您可以分享自定义代码中的示例。

关于scikit-learn - Sagemaker批量转换 "ValueError: could not convert string to float",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59988417/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com