AWS Lambda上管道故障的解除连接

2024-09-30 08:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,如果这个解决方案已经发布了,我很抱歉,我已经看了这么多的帖子,没有找到解决这个问题的方法

我有一个在我的机器上运行良好的Python类,以便训练并运行ML模型

我想在AWS Lambda上执行运行(并且只执行运行),但在pickle.load期间失败,因为它似乎找不到管道使用的转换函数(虽然它们在代码中可用)

"Can't get attribute 'myMLClass' on <module 'main' from '/var/runtime/awslambda/bootstrap.py'>", "errorType": "AttributeError"

代码在Python3.6中,它使用sklearn管道、XGBOOST和Pickle来保存/加载模型

以下是它的概述:

import stuff

class myMLClass:

    def myTransformation(self, dataframe): 
        #myTransformations

    def train_model(self)
        preprocessor = FunctionTransformer(myTransformation, validate=False)
        xgb = XGBClassifier()
        pipeline_xgb = make_pipeline(preprocessor, xgb)
        #[XGB Stuff: fit, predict,...]
        with open("myTrainedPipeline.pkl", 'wb') as file:
            pickle.dump(pipeline_xgb, file)

    def run_model(self)
        with open("myTrainedPipeline.pkl", 'rb') as file:
            pipeline = pickle.load(file)    ==> Which trigger the error

def main():
    myObject = myMLClass("DEV")
    myObject.run_model()

同样,同样的课程在我的机器上训练和跑步都很好。我知道Pickle没有序列化转换函数,只是对它的一个引用,这就是为什么我包括了training&;在同一类下运行操作

注意:如果我尝试从main()函数中取消勾选,问题是相同的

非常感谢你的帮助

亚历克斯


Tags: 函数代码模型self机器model管道pipeline
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:32

仅供参考,由于本页的解释,我能够解决此问题:

https://www.stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html

基本上,我需要将主模块和模块分成两个不同的文件

我相信这是因为AWS Lambda的工作方式,尤其是bootstrap.py。拆分之后,代理明确需要导入专用模块,因为导入是在main中声明的

相关问题 更多 >

    热门问题