'NoneType' 对象没有属性 'boto_region_name'
'NoneType' object has no attribute 'boto_region_name'
我已经为这个问题纠结了一段时间,似乎无法理解这个问题的根源。我正在 Sagemaker 上部署模型,但在这行代码中遇到错误:
sm_model.deploy(initial_instance_count=1, instance_type='ml.m4.2xlarge', endpoint_name=endpoint_name)
Jupyter Notebook 输出以下错误。注意:第 269 行不是我笔记本中的代码,它只是我通过上面的 model.deploy 命令获得的参考。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
267 return self.image
268
--> 269 region_name = self.sagemaker_session.boto_region_name
270 return create_image_uri(
271 region_name,
AttributeError: 'NoneType' object has no attribute 'boto_region_name'
编辑:这只是我用来创建此管道的示例数据集。这是在 sagemaker notebook 实例上。我在下面添加了完整的代码以进行说明。
from sagemaker.model import Model
from sagemaker.pipeline import PipelineModel
import boto3
from time import gmtime, strftime
import boto3
import s3fs
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
%matplotlib inline
sns.set()
import boto3
import sagemaker
from sagemaker import get_execution_role
# Using Amazon S3
s3 = boto3.client('s3')
sage = boto3.client('sagemaker')
session = boto3.session.Session()
sagemaker_session = sagemaker.Session()
# Get a SageMaker-compatible role used by this Notebook Instance.
role = get_execution_role()
#Upload file using AWS session
# S3 prefix
prefix = 'Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1'
train_input = sagemaker_session.upload_data(
path='housing.csv',
bucket=bucket,
key_prefix='{}/{}'.format(prefix, 'train'))
from sagemaker.sklearn.estimator import SKLearn
output_dir = 's3://sagemaker-us-east-1-819182027957/Scikit-keras-NLP-pipeline-Boston-Housing-example-July08-test1/train'
model_dir = 's3://sagemaker-us-east-1-819182027957/Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1/train'
script_path = 'Boston.py'
sklearn_preprocessor = SKLearn(
entry_point=script_path,
role=role,
train_instance_type="ml.c4.xlarge",
sagemaker_session=sagemaker_session,
output_path=output_dir)
sklearn_preprocessor.fit({'train': train_input,'model-dir':model_dir,'output-data-dir':output_dir})
from sagemaker.tensorflow.serving import Model
sagemaker_estimator = Model(model_data = 's3://' + sagemaker_session.default_bucket() + '/Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1/train/Bostonmodel.tar.gz',
role = role)
#####
scikit_learn_inference_model = sklearn_preprocessor.create_model()
#sagemaker_model = sagemaker_estimator.create_model() # Does Not have create_model method
sagemaker_model = sagemaker_estimator
model_name = 'Boston-inf-pipeline-July08-model'
endpoint_name = 'Boston-inf-pipeline-July08-endpoint'
#Build Inference Pipeline
sm_model = PipelineModel(
name=model_name,
role=role,
models=[
scikit_learn_inference_model,
sagemaker_model],
sagemaker_session=sagemaker_session)
sm_model.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge', endpoint_name=endpoint_name)
这意味着您的代码中有些地方不正确。变量 sagemaker_session
没有像您认为的那样分配给它的值。首先检查那个变量的值是否设置正确。
错误是由于您的 sagemaker.tensorflow.serving.Model
没有与之关联的 sagemaker.session.Session
。
将 sagemaker_session=sagemaker_session
添加到您的模型实例化:
from sagemaker.tensorflow.serving import Model
sagemaker_model = Model(model_data='s3://' + sagemaker_session.default_bucket() + '/Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1/train/Bostonmodel.tar.gz',
role=role,
sagemaker_session=sagemaker_session)
我已经为这个问题纠结了一段时间,似乎无法理解这个问题的根源。我正在 Sagemaker 上部署模型,但在这行代码中遇到错误:
sm_model.deploy(initial_instance_count=1, instance_type='ml.m4.2xlarge', endpoint_name=endpoint_name)
Jupyter Notebook 输出以下错误。注意:第 269 行不是我笔记本中的代码,它只是我通过上面的 model.deploy 命令获得的参考。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
267 return self.image
268
--> 269 region_name = self.sagemaker_session.boto_region_name
270 return create_image_uri(
271 region_name,
AttributeError: 'NoneType' object has no attribute 'boto_region_name'
编辑:这只是我用来创建此管道的示例数据集。这是在 sagemaker notebook 实例上。我在下面添加了完整的代码以进行说明。
from sagemaker.model import Model
from sagemaker.pipeline import PipelineModel
import boto3
from time import gmtime, strftime
import boto3
import s3fs
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
%matplotlib inline
sns.set()
import boto3
import sagemaker
from sagemaker import get_execution_role
# Using Amazon S3
s3 = boto3.client('s3')
sage = boto3.client('sagemaker')
session = boto3.session.Session()
sagemaker_session = sagemaker.Session()
# Get a SageMaker-compatible role used by this Notebook Instance.
role = get_execution_role()
#Upload file using AWS session
# S3 prefix
prefix = 'Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1'
train_input = sagemaker_session.upload_data(
path='housing.csv',
bucket=bucket,
key_prefix='{}/{}'.format(prefix, 'train'))
from sagemaker.sklearn.estimator import SKLearn
output_dir = 's3://sagemaker-us-east-1-819182027957/Scikit-keras-NLP-pipeline-Boston-Housing-example-July08-test1/train'
model_dir = 's3://sagemaker-us-east-1-819182027957/Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1/train'
script_path = 'Boston.py'
sklearn_preprocessor = SKLearn(
entry_point=script_path,
role=role,
train_instance_type="ml.c4.xlarge",
sagemaker_session=sagemaker_session,
output_path=output_dir)
sklearn_preprocessor.fit({'train': train_input,'model-dir':model_dir,'output-data-dir':output_dir})
from sagemaker.tensorflow.serving import Model
sagemaker_estimator = Model(model_data = 's3://' + sagemaker_session.default_bucket() + '/Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1/train/Bostonmodel.tar.gz',
role = role)
#####
scikit_learn_inference_model = sklearn_preprocessor.create_model()
#sagemaker_model = sagemaker_estimator.create_model() # Does Not have create_model method
sagemaker_model = sagemaker_estimator
model_name = 'Boston-inf-pipeline-July08-model'
endpoint_name = 'Boston-inf-pipeline-July08-endpoint'
#Build Inference Pipeline
sm_model = PipelineModel(
name=model_name,
role=role,
models=[
scikit_learn_inference_model,
sagemaker_model],
sagemaker_session=sagemaker_session)
sm_model.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge', endpoint_name=endpoint_name)
这意味着您的代码中有些地方不正确。变量 sagemaker_session
没有像您认为的那样分配给它的值。首先检查那个变量的值是否设置正确。
错误是由于您的 sagemaker.tensorflow.serving.Model
没有与之关联的 sagemaker.session.Session
。
将 sagemaker_session=sagemaker_session
添加到您的模型实例化:
from sagemaker.tensorflow.serving import Model
sagemaker_model = Model(model_data='s3://' + sagemaker_session.default_bucket() + '/Scikit-keras-NLP-pipeline-Boston-Housing-example-June08-test1/train/Bostonmodel.tar.gz',
role=role,
sagemaker_session=sagemaker_session)