SpaCy-ValueError:操作数不能与形状(1,2)(1,5)一起广播

Hen*_*ski 5 python spacy multiclass-classification pytorch

与上一篇关于stackoverflow的帖子有关, Model()为参数'nr_class'获取了多个值-SpaCy多分类模型(BERT集成),其中我的问题部分已经解决,我想分享实现解决方案后出现的问题。

如果我删除nr_class参数,则会在此出现此错误:

ValueError: operands could not be broadcast together with shapes (1,2) (1,5)
Run Code Online (Sandbox Code Playgroud)

我实际上以为会发生这种情况,因为我没有指定nr_class参数。它是否正确?

再一次,我的多类模型代码:

nlp = spacy.load('en_pytt_bertbaseuncased_lg')
textcat = nlp.create_pipe(
    'pytt_textcat',
    config={
        "nr_class":5,
        "exclusive_classes": True,
    }
)
nlp.add_pipe(textcat, last = True)

textcat.add_label("class1")
textcat.add_label("class2")
textcat.add_label("class3")
textcat.add_label("class4")
textcat.add_label("class5")
Run Code Online (Sandbox Code Playgroud)

培训的代码如下,并基于此处的示例(https://pypi.org/project/spacy-pytorch-transformers/):

def extract_cat(x):
    for key in x.keys():
        if x[key]:
            return key

# get names of other pipes to disable them during training
n_iter = 250 # number of epochs

train_data = list(zip(train_texts, [{"cats": cats} for cats in train_cats]))


dev_cats_single   = [extract_cat(x) for x in dev_cats]
train_cats_single = [extract_cat(x) for x in train_cats]
cats = list(set(train_cats_single))
recall = {}
for c in cats:
    if c is not None: 
        recall['dev_'+c] = []
        recall['train_'+c] = []



optimizer = nlp.resume_training()
batch_sizes = compounding(1.0, round(len(train_texts)/2), 1.001)

for i in range(n_iter):
    random.shuffle(train_data)
    losses = {}
    batches = minibatch(train_data, size=batch_sizes)
    for batch in batches:
        texts, annotations = zip(*batch)
        nlp.update(texts, annotations, sgd=optimizer, drop=0.2, losses=losses)
    print(i, losses)
Run Code Online (Sandbox Code Playgroud)

因此,我的数据结构如下所示:

[('TEXT TEXT TEXT',
  {'cats': {'class1': False,
    'class2': False,
    'class3': False,
    'class4': True,
    'class5': False}}), ... ]
Run Code Online (Sandbox Code Playgroud)

Hen*_*ski 0

正如 @Milla Well 已经评论过的,答案可以在这里找到(来自 @syllogism_ 的 github 上的错误修复)