1

所以,我是 NLP 的新手,我正在尝试使用 spacy_transformers 训练一个文本分类器。此代码已显示可以运行,但它会在我的计算机上引发错误。作为旁注,这可能是由于我在 cpu 上运行它的脂肪引起的吗?

def train_classifier(n_epoch:int=5, 
                     train_data:list=None, 
                     val_text:tuple=None, 
                     val_label:list=None, 
                     batch_size:int=32, 
                     lr:float=1e-3):
    train_stats = []
    dropout = decaying(0.2, 0.1, 0.3) # Gradually decrease dropout rate from 0.2 to 0.1
    # Cyclic triangular rate (https://arxiv.org/abs/1506.01186)
    learn_rates = cyclic_triangular_rate(
        lr / 3, lr * 3, 2 * len(train_data) // batch_size
    )

    for epoch in range(n_epoch):
        random.shuffle(train_data)
        batches = minibatch(train_data, size=batch_size)
        losses = {}

        for batch in batches:
            optimizer.trf_lr = next(learn_rates)
            texts, cats = zip(*batch)
            nlp.update(
                texts, 
                cats, 
                drop = next(dropout),
                sgd = optimizer,
                losses=losses)




然后当我通过函数


train_classifier(n_epoch=10, train_data=train_data, val_text=val_text, val_label=val_label, batch_size=32, lr=2e-6)

我收到以下错误

<ipython-input-55-5bb071ef310c> in train_classifier(n_epoch, train_data, val_text, val_label, batch_size, lr)
      6                      lr:float=1e-3):
      7     train_stats = []
----> 8     dropout = decaying(0.2, 0.1, 0.3) # Gradually decrease dropout rate from 0.2 to 0.1
      9     # Cyclic triangular rate (https://arxiv.org/abs/1506.01186)
     10     learn_rates = cyclic_triangular_rate(

NameError: name 'decaying' is not defined

4

1 回答 1

2

我对 spacy 一点也不熟悉,但是经过几次谷歌搜索后,看起来这个函数可能依赖于 spacy 的util.decaying函数。如果没有将衰减函数加载到内存中,此train_classifier函数将抛出一个NameError. 该decaying函数的代码如下,可以在这里找到。

def decaying(start, stop, decay):
    """Yield an infinite series of linearly decaying values."""

    curr = float(start)
    while True:
        yield max(curr, stop)
        curr -= decay
于 2020-03-25T03:36:15.830 回答