尝试训练 spacy 模型时出现值错误

xiaoxingxing nlp 482

原文标题Value Error when trying to train a spacy model

我尝试训练一个 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)
~\AppData\Local\Temp/ipykernel_16120/3494358196.py in <module>
      4 
      5 # Training the model
----> 6 train_model(nlp, train_data, optimizer, batch_size, epochs)

~\AppData\Local\Temp/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 

~\anaconda3\lib\site-packages\spacy\language.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. 

原文链接:https://stackoverflow.com//questions/71467995/value-error-when-trying-to-train-a-spacy-model

回复

我来回复
  • furas的头像
    furas 评论

    根据文档对版本做了一些改动3.x,现在直接使用batch不拆分texts, labels = zip(*batch)

                for batch in batches:
                    nlp.update(batch, sgd=optimizer, losses=losses)
    

    就这样。

    2年前 0条评论