I have created one custom transformer class AverageWordLengthExtractor
in my pipeline code and saved the model after running it successfully. Now, when I am trying to load the model using a flask app, it is giving 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'
: The code works fine without a custom class.:代码在没有自定义类的情况下工作正常。
have you imported the class AverageWordLengthExtractor
in you flask app? 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. In this case: average_word_length_extractor.py
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.