在Sklearn管道中使用Spacy作为标记器

时间:2018-12-21 12:58:02

标签: python scikit-learn spacy

我试图在更大的scikit学习管道中使用spacy作为标记器,但是始终遇到无法腌制该任务发送给工人的问题。

最小示例:

from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import RandomizedSearchCV
from sklearn.datasets import fetch_20newsgroups
from functools import partial
import spacy


def spacy_tokenize(text, nlp):
    return [x.orth_ for x in nlp(text)]

nlp = spacy.load('en', disable=['ner', 'parser', 'tagger'])
tok = partial(spacy_tokenize, nlp=nlp)

pipeline = Pipeline([('vectorize', CountVectorizer(tokenizer=tok)),
                     ('clf', SGDClassifier())])

params = {'vectorize__ngram_range': [(1, 2), (1, 3)]}

CV = RandomizedSearchCV(pipeline,
                        param_distributions=params,
                        n_iter=2, cv=2, n_jobs=2,
                        scoring='accuracy')

categories = ['alt.atheism', 'comp.graphics']
news = fetch_20newsgroups(subset='train',
                          categories=categories,
                          shuffle=True,
                          random_state=42)

CV.fit(news.data, news.target)

运行此代码,我得到错误:

PicklingError: Could not pickle the task to send it to the workers.

令我困惑的是:

import pickle
pickle.dump(tok, open('test.pkl', 'wb'))

工作正常。

有人知道是否可以将spacy与sklearn交叉验证一起使用? 谢谢!

1 个答案:

答案 0 :(得分:1)

这不是解决方案,而是一种解决方法。似乎spacy和joblib之间存在一些问题:

如果可以将令牌生成器作为函数保存在目录中的单独文件中,然后将其导入到当前文件中,则可以避免此错误。像这样:

  • 自定义文件 .py

    import spacy
    nlp = spacy.load('en', disable=['ner', 'parser', 'tagger'])
    
    def spacy_tokenizer(doc):
        return [x.orth_ for x in nlp(doc)]
    
  • 主要 .py

    #Other code     
    ...
    ... 
    
    from custom_file import spacy_tokenizer
    
    pipeline = Pipeline([('vectorize', CountVectorizer(tokenizer=spacy_tokenizer)),
                         ('clf', SGDClassifier())])
    
    ...
    ...