我尝试训练一个spacy模型,但最近我开始出现一些错误,我出现了以下错误,我希望有人能帮助我解决错误
def train_model(model, train_data, optimizer, batch_size, epochs=10):
losses = {}
random.seed(1)
for epoch in range(epochs):
random.shuffle(train_data)
batches = minibatch(train_data, size=batch_size)
for batch in batches:
# Split batch into texts and labels
texts, labels = zip(*batch)
# Update model with texts and labels
nlp.update(texts, labels, sgd=optimizer, losses=losses)
print("Loss: {}".format(losses['textcat']))
return losses['textcat']
optimizer = nlp.begin_training()
batch_size = 5
epochs = 20
# Training the model
train_model(nlp, train_data, optimizer, batch_size, epochs)
以下是显示存在值错误的错误
ValueError
Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_16120/3494358196.py in <module>
4
5 # Training the model
----> 6 train_model(nlp, train_data, optimizer, batch_size, epochs)
~AppDataLocalTemp/ipykernel_16120/3158014372.py in train_model(model, train_data, optimizer, batch_size, epochs)
12
13 # Update model with texts and labels
---> 14 nlp.update(texts, labels, sgd=optimizer, losses=losses)
15 print("Loss: {}".format(losses['textcat']))
16
~anaconda3libsite-packagesspacylanguage.py in update(self, examples, _, drop, sgd, losses, component_cfg, exclude, annotates)
1132 """
1133 if _ is not None:
-> 1134 raise ValueError(Errors.E989)
1135 if losses is None:
1136 losses = {}
ValueError: [E989] `nlp.update()` was called with two positional arguments. This may be due to a backwards-incompatible change to the format of the training data in spaCy 3.0 onwards. The 'update' function should now be called with a batch of Example objects, instead of `(text, annotation)` tuples.
根据文档,他们在版本3.x
中进行了一些更改,现在它直接使用batch
,而不拆分texts, labels = zip(*batch)
。
for batch in batches:
nlp.update(batch, sgd=optimizer, losses=losses)
仅此而已。