Python 空间分类器:';unicode';对象没有属性';到#u数组';
我试图用spaCy编写一个最小的文本分类器。我编写了以下代码片段来训练文本分类程序(而不训练整个NLP管道): 但当我运行它时,它返回一个错误。这是我启动它时它给我的回溯:Python 空间分类器:';unicode';对象没有属性';到#u数组';,python,nlp,classification,spacy,Python,Nlp,Classification,Spacy,我试图用spaCy编写一个最小的文本分类器。我编写了以下代码片段来训练文本分类程序(而不训练整个NLP管道): 但当我运行它时,它返回一个错误。这是我启动它时它给我的回溯: Traceback (most recent call last): File "C:\Users\Reuben\Desktop\Classification\Classification\Training.py", line 16, in <module> textcat.update([doc1
Traceback (most recent call last):
File "C:\Users\Reuben\Desktop\Classification\Classification\Training.py", line
16, in <module>
textcat.update([doc1, doc2], [gold1, gold2], losses=losses, sgd=optimizer)
File "pipeline.pyx", line 838, in spacy.pipeline.TextCategorizer.update
File "D:\Program Files\Anaconda2\lib\site-packages\thinc\api.py", line 61, in
begin_update
X, inc_layer_grad = layer.begin_update(X, drop=drop)
File "D:\Program Files\Anaconda2\lib\site-packages\thinc\api.py", line 176, in
begin_update
values = [fwd(X, *a, **k) for fwd in forward]
File "D:\Program Files\Anaconda2\lib\site-packages\thinc\api.py", line 258, in
wrap
output = func(*args, **kwargs)
File "D:\Program Files\Anaconda2\lib\site-packages\thinc\api.py", line 61, in
begin_update
X, inc_layer_grad = layer.begin_update(X, drop=drop)
File "D:\Program Files\Anaconda2\lib\site-packages\spacy\_ml.py", line 95, in
_preprocess_doc
keys = [doc.to_array(LOWER) for doc in docs]
AttributeError: 'unicode' object has no attribute 'to_array'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Reuben\Desktop\Classification\Classification\Training.py”,第行
16,在
textcat.update([doc1,doc2],[gold1,gold2],损耗=损耗,sgd=优化器)
文件“pipeline.pyx”,第838行,位于spacy.pipeline.TextCategorizer.update中
文件“D:\Program Files\Anaconda2\lib\site packages\thinc\api.py”,第61行,在
开始更新
十、 inc\u layer\u grad=layer.begin\u更新(X,drop=drop)
文件“D:\Program Files\Anaconda2\lib\site packages\thinc\api.py”,第176行,在
开始更新
值=[fwd(X,*a,**k)表示前进中的fwd]
文件“D:\Program Files\Anaconda2\lib\site packages\thinc\api.py”,第258行,在
包
输出=func(*args,**kwargs)
文件“D:\Program Files\Anaconda2\lib\site packages\thinc\api.py”,第61行,在
开始更新
十、 inc\u layer\u grad=layer.begin\u更新(X,drop=drop)
文件“D:\Program Files\Anaconda2\lib\site packages\spacy\\u ml.py”,第95行,在
_预处理文档
keys=[文档中文档的文档到_数组(下)]
AttributeError:“unicode”对象没有“to_数组”属性
我如何解决这个问题?显然
textcat
需要黄金值,而黄金值是由GoldParse
生成的,而不是纯文本值。工作版本如下所示:
import spacy
from spacy.pipeline import TextCategorizer
from spacy.gold import GoldParse
nlp = spacy.load('en')
doc1 = nlp(u'This is my first document in the dataset.')
doc2 = nlp(u'This is my second document in the dataset.')
gold1 = GoldParse(doc=doc1, cats={'Category1': 1, 'Category2': 0})
gold2 = GoldParse(doc=doc2, cats={'Category1': 0, 'Category2': 1})
textcat = TextCategorizer(nlp.vocab)
textcat.add_label('Category1')
textcat.add_label('Category2')
losses = {}
optimizer = textcat.begin_training()
textcat.update([doc1, doc2], [gold1, gold2], losses=losses, sgd=optimizer)
感谢评论中的@abarnert帮助我调试此问题。你能将回溯作为文本而不是模糊的屏幕截图发布吗?好主意。我现在就编辑这篇文章。好的,显然问题是你传入的
textcat.update
调用的四个字符串(unicode
对象)中至少有一个应该是其他类型的对象。我不知道是哪种类型,只是它需要一个to_array
方法(Python的内置类型都没有,也没有numpy/scipy/pandas或其他类似的东西)。我猜这是spacy本身的某种类型。另外,我从第一个例子中注意到,他们正在做doc1=nlp(u“我的薯条超级恶心”)
,而你在做doc1=u'这是我在数据集中的第一个文档。
。那么,您是否只需要在某些或所有这些字符串上调用nlp
。这听起来是一个很好的猜测,所以我现在就来试试。如果没有阅读该函数的文档(或者使用gold
子模块的示例),我绝对不会猜到。很高兴你找到了。(但在将来,如果函数没有任何抱怨,那么直接查看文档或源代码通常是找到它期望的结果的最快方法。)
import spacy
from spacy.pipeline import TextCategorizer
from spacy.gold import GoldParse
nlp = spacy.load('en')
doc1 = nlp(u'This is my first document in the dataset.')
doc2 = nlp(u'This is my second document in the dataset.')
gold1 = GoldParse(doc=doc1, cats={'Category1': 1, 'Category2': 0})
gold2 = GoldParse(doc=doc2, cats={'Category1': 0, 'Category2': 1})
textcat = TextCategorizer(nlp.vocab)
textcat.add_label('Category1')
textcat.add_label('Category2')
losses = {}
optimizer = textcat.begin_training()
textcat.update([doc1, doc2], [gold1, gold2], losses=losses, sgd=optimizer)