[英]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成功运行并保存模型的流水线代码
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)
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_extractor
是AverageWordLengthExtractor
类所在的 Python 文件。 In this case: average_word_length_extractor.py
在这种情况下:
average_word_length_extractor.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.