简体   繁体   English

AttributeError: 模块 '__main__' 没有属性 'AverageWordLengthExtractor'

[英]AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor'

I have created one custom transformer class AverageWordLengthExtractor in my pipeline code and saved the model after running it successfully.我在我的管道代码中创建了一个自定义转换器类AverageWordLengthExtractor并在成功运行后保存了模型。 Now, when I am trying to load the model using a flask app, it is giving AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor'现在,当我尝试使用烧瓶应用程序加载模型时,它给出了AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor'

pipeline code that runs successfully and saves the model成功运行并保存模型的流水线代码

custom class自定义类

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)

saving the model保存模型

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

this above code runs successfully.上面的代码运行成功。

Now, I am trying to load the model using flask.现在,我正在尝试使用烧瓶加载模型。

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

I am trying to predict using this model, but it is giving the error,我正在尝试使用此模型进行预测,但它给出了错误,

$ 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'

note : The code works fine without a custom class.注意:代码在没有自定义类的情况下工作正常。

have you imported the class AverageWordLengthExtractor in you flask app?您是否在烧瓶应用程序中导入了AverageWordLengthExtractor类? eg例如

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

average_word_length_extractor is the Python file where your AverageWordLengthExtractor class is. average_word_length_extractorAverageWordLengthExtractor类所在的 Python 文件。 In this case: average_word_length_extractor.py在这种情况下: average_word_length_extractor.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:模块'__main__'没有属性'cleaner' - AttributeError: module '__main__' has no attribute 'cleaner' unittest:AttributeError:模块'__main__'没有属性'C:\ ...' - unittest: AttributeError: module '__main__' has no attribute 'C:\…' pickle/joblib AttributeError: 模块 &#39;__main__&#39; 在 pytest 中没有属性 &#39;thing&#39; - pickle/joblib AttributeError: module '__main__' has no attribute 'thing' in pytest TensorFlow:模块&#39;__main__&#39;没有属性&#39;main&#39; - TensorFlow: module '__main__' has no attribute 'main' 查找“pip”的模块规范时出错(AttributeError:模块“__main__”没有属性“__file__”) - Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__') Python多处理错误:AttributeError:模块&#39;__main__&#39;没有属性&#39;__spec__&#39; - Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__' Pydoop mapreduce“ AttributeError:模块&#39;wordcount_minimal&#39;没有属性&#39;__main__&#39;” - Pydoop mapreduce “AttributeError: module 'wordcount_minimal' has no attribute '__main__'” 用于单元测试的python3:AttributeError:模块&#39;__main__&#39;没有属性“内核......” - python3 for unit test: AttributeError: module '__main__' has no attribute “kernel…” AttributeError:无法获取属性“tokenizer”<module '__main__'> - AttributeError: Can't get attribute 'tokenizer' on <module '__main__'> AttributeError:无法获取属性“DataFrameSelector”<module '__main__'></module> - AttributeError: Can't get attribute 'DataFrameSelector' on <module '__main__'>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM