pythonmachine-learningspacyspacy-3spacy-transformers

SpaCy transformer NER training – zero loss on transformer, not trained


I am training a SpaCy pipeline with ['transformer', 'ner'] components, ner trains well, but transformer is stuck on 0 loss, and, I am assuming, is not training.

Here is my config:

[paths]
vectors = "en_core_web_trf"
init_tok2vec = null
train = "/home/sxdadmin/spacy/input/train.spacy"
dev = "/home/sxdadmin/spacy/input/dev.spacy"

[system]
gpu_allocator = "pytorch"
seed = 0

[nlp]
lang = "en"
pipeline = ["transformer", "ner"]
batch_size = 512
disabled = []
before_creation = null
after_creation = null
after_pipeline_creation = null
tokenizer = {"@tokenizers":"spacy.Tokenizer.v1"}
vectors = {"@vectors":"spacy.Vectors.v1"}

######################################################################
[components]
######################################################################

[components.transformer]
factory = "transformer"
max_batch_items = 4096

[components.transformer.model]
@architectures = "spacy-transformers.TransformerModel.v1"
name = "bert-base-cased"
tokenizer_config = {"use_fast": true}

[components.transformer.model.get_spans]
@span_getters = "spacy-transformers.doc_spans.v1"

[components.transformer.set_extra_annotations]
@annotation_setters = "spacy-transformers.null_annotation_setter.v1"

######################################################################

[components.ner]
factory = "ner"
incorrect_spans_key = null
moves = null
scorer = {"@scorers":"spacy.ner_scorer.v1"}
update_with_oracle_cut_size = 100

[components.ner.model]
@architectures = "spacy.TransitionBasedParser.v2"
state_type = "ner"
extra_state_tokens = false
hidden_width = 64
maxout_pieces = 2
use_upper = true
nO = null

######################################################################
[corpora]
######################################################################

[corpora.train]
@readers = "spacy.Corpus.v1"
path = ${paths.train}
max_length = 3000
gold_preproc = false
limit = 0
augmenter = null

[corpora.dev]
@readers = "spacy.Corpus.v1"
path = ${paths.dev}
max_length = 3000
gold_preproc = false
limit = 0
augmenter = null

######################################################################
[training]
######################################################################

dev_corpus = "corpora.dev"
train_corpus = "corpora.train"
seed = 0
gpu_allocator = "pytorch"
dropout = 0.1
accumulate_gradient = 1
patience = 1600
max_epochs = 0
max_steps = 20000
eval_frequency = 200
frozen_components = []
annotating_components = []
before_to_disk = null
before_update = null

######################################################################

[training.batcher]
@batchers = "spacy.batch_by_words.v1"
discard_oversize = false
tolerance = 0.2
get_length = null

[training.batcher.size]
@schedules = "compounding.v1"
start = 64
stop = 512
compound = 1.001
t = 0.0

######################################################################

[training.logger]
@loggers = "spacy.ConsoleLogger.v1"
progress_bar = false

[training.optimizer]
@optimizers = "Adam.v1"
beta1 = 0.9
beta2 = 0.999
L2_is_weight_decay = true
L2 = 0.01
grad_clip = 1.0
use_averages = false
eps = 0.00000001
learn_rate = 0.001

[training.score_weights]
ents_f = 1.0
ents_p = 0.0
ents_r = 0.0
ents_per_type = null

######################################################################
[pretraining]
######################################################################

[initialize]
vectors = "en_core_web_lg"
init_tok2vec = null
vocab_data = null
lookups = null
before_init = null
after_init = null

[initialize.components]
[initialize.components.transformer]
[initialize.tokenizer]

and the output:

enter image description here

All warnings are met, the famous Bert's max_length of 512 tokens is achieved by text segmentation. Data was previously tested on [tok2vec, ner] setup.

Please help.


Solution

  • Transformers are touchy when it comes to exactness of settings. I ended up using command to generate config.cfg for me. That way I know where exactly I deviated from working version if something goes wrong, as long as I change params one by one.

    !python -m spacy init config      \
        "/path/to/project/config.cfg" \
        --lang en                     \
        --pipeline transformer,ner    \
        --optimize accuracy           \
        --gpu                         \
        --force
    

    This worked right away:

    enter image description here