AttributeError:模块“__main__”没有属性“AverageWordLengthExtractor”

Posted

技术标签:

【中文标题】AttributeError:模块“__main__”没有属性“AverageWordLengthExtractor”【英文标题】:AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor' 【发布时间】:2019-09-14 09:09:32 【问题描述】:

我在管道代码中创建了一个自定义转换器类AverageWordLengthExtractor,并在成功运行后保存了模型。现在,当我尝试使用烧瓶应用程序加载模型时,它给出了AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor'

成功运行并保存模型的管道代码

自定义类

class AverageWordLengthExtractor(BaseEstimator, TransformerMixin):
    
    def __init__(self):
        pass
    def average_word_length(self, text):
        return np.mean([len(word) for word in text.split( ) if word not in stopWords])
    def fit(self, x, y=None):
        return self
    def transform(self, x , y=None):
        return pd.DataFrame(pd.Series(x).apply(self.average_word_length)).fillna(0)

保存模型

def save_model(model, model_filepath):
    # Save best grid search pipeline to file
    dump_file = model_filepath
    joblib.dump(model, dump_file, compress=1)

以上代码运行成功。

现在,我正在尝试使用烧瓶加载模型。

app = Flask(__name__)
....
....
# load model
model = joblib.load("../models/classifier2.pkl")

我正在尝试使用此模型进行预测,但它给出了错误,

$ python run.py
Traceback (most recent call last):
  File "run.py", line 33, in <module>
    model = joblib.load("../models/classifier2.pkl")
  File "C:\Users\609775743\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\externals\joblib\numpy_pickle.py", line 578, in load
    obj = _unpickle(fobj, filename, mmap_mode)
  File "C:\Users\609775743\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\externals\joblib\numpy_pickle.py", line 508, in _unpickle
    obj = unpickler.load()
  File "C:\Users\609775743\AppData\Local\Continuum\anaconda3\lib\pickle.py", line 1050, in load
    dispatch[key[0]](self)
  File "C:\Users\609775743\AppData\Local\Continuum\anaconda3\lib\pickle.py", line 1338, in load_global
    klass = self.find_class(module, name)
  File "C:\Users\609775743\AppData\Local\Continuum\anaconda3\lib\pickle.py", line 1392, in find_class
    return getattr(sys.modules[module], name)
AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor'

注意:代码在没有自定义类的情况下也能正常工作。

【问题讨论】:

【参考方案1】:

您是否在烧瓶应用程序中导入了 AverageWordLengthExtractor 类? 例如

import joblib
from average_word_length_extractor import AverageWordLengthExtractor
app = Flask(__name__)
# ...
# ...
# load model
model = joblib.load("../models/classifier2.pkl")

average_word_length_extractor 是您的 AverageWordLengthExtractor 类所在的 Python 文件。在这种情况下:average_word_length_extractor.py

【讨论】:

以上是关于AttributeError:模块“__main__”没有属性“AverageWordLengthExtractor”的主要内容,如果未能解决你的问题,请参考以下文章

Pydoop mapreduce“AttributeError:模块'wordcount_minimal'没有属性'__main__'”

pickle/joblib AttributeError:模块'__main__'在pytest中没有属性'thing'

Pydoop mapreduce“AttributeError:module'wordcount_minimal'没有属性'__main__'”

Unpickling 保存的 pytorch 模型会引发 AttributeError: Can't get attribute 'Net' on <module '__main__' 尽管内联

Windows 上的 Python DEAP 和多处理:AttributeError

Python测试时AttributeError: module '__main__' has no attribute...报错解决方案