# Buggy code
def begin_training(self, get_gold_tuples=None, sgd=None, **cfg):
    """Allocate models, pre-process training data and acquire a trainer and
    optimizer. Used as a contextmanager.

    get_gold_tuples (function): Function returning gold data
    **cfg: Config parameters.
    RETURNS: An optimizer
    """
    if get_gold_tuples is None:
        get_gold_tuples = lambda: []
    # Populate vocab
    else:
        for _, annots_brackets in get_gold_tuples():
            for annots, _ in annots_brackets:
                for word in annots[1]:
                    _ = self.vocab[word]
    contexts = []
    if cfg.get("device", -1) >= 0:
        device = util.use_gpu(cfg["device"])
        if self.vocab.vectors.data.shape[1] >= 1:
            self.vocab.vectors.data = Model.ops.asarray(self.vocab.vectors.data)
    else:
        device = None
    link_vectors_to_models(self.vocab)
    if sgd is None:
        sgd = create_default_optimizer(Model.ops)
    self._optimizer = sgd
    for name, proc in self.pipeline:
        if hasattr(proc, "begin_training"):
            proc.begin_training(
                get_gold_tuples(), pipeline=self.pipeline, sgd=self._optimizer, **cfg
            )
    return self._optimizer


# Traceback (ValueError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Projects/foobar/.env/lib/python3.6/site-packages/spacy/language.py", line 333, in __call__
doc = proc(doc)
File "pipeline.pyx", line 390, in spacy.pipeline.Tagger.__call__
File "pipeline.pyx", line 402, in spacy.pipeline.Tagger.predict
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/neural/_classes/model.py", line 161, in __call__
return self.predict(x)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 55, in predict
X = layer(X)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/neural/_classes/model.py", line 161, in __call__
return self.predict(x)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 293, in predict
X = layer(layer.ops.flatten(seqs_in, pad=pad))
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/neural/_classes/model.py", line 161, in __call__
return self.predict(x)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 55, in predict
X = layer(X)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/neural/_classes/model.py", line 161, in __call__
return self.predict(x)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/neural/_classes/model.py", line 125, in predict
y, _ = self.begin_update(X)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 372, in uniqued_fwd
Y_uniq, bp_Y_uniq = layer.begin_update(X[ind], drop=drop)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 61, in begin_update
X, inc_layer_grad = layer.begin_update(X, drop=drop)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in begin_update
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in <listcomp>
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 258, in wrap
output = func(*args, **kwargs)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in begin_update
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in <listcomp>
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 258, in wrap
output = func(*args, **kwargs)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in begin_update
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in <listcomp>
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 258, in wrap
output = func(*args, **kwargs)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in begin_update
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 176, in <listcomp>
values = [fwd(X, *a, **k) for fwd in forward]
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/api.py", line 258, in wrap
output = func(*args, **kwargs)
File "/Projects/foobar/.env/lib/python3.6/site-packages/thinc/neural/_classes/static_vectors.py", line 67, in begin_update
dotted = self.ops.batch_dot(vectors, self.W)
File "ops.pyx", line 299, in thinc.neural.ops.NumpyOps.batch_dot
ValueError: shapes (4,0) and (300,128) not aligned: 0 (dim 1) != 300 (dim 0)

# Fixed code
def begin_training(self, get_gold_tuples=None, sgd=None, **cfg):
    """Allocate models, pre-process training data and acquire a trainer and
    optimizer. Used as a contextmanager.

    get_gold_tuples (function): Function returning gold data
    **cfg: Config parameters.
    RETURNS: An optimizer
    """
    if get_gold_tuples is None:
        get_gold_tuples = lambda: []
    # Populate vocab
    else:
        for _, annots_brackets in get_gold_tuples():
            for annots, _ in annots_brackets:
                for word in annots[1]:
                    _ = self.vocab[word]
    contexts = []
    if cfg.get("device", -1) >= 0:
        device = util.use_gpu(cfg["device"])
        if self.vocab.vectors.data.shape[1] >= 1:
            self.vocab.vectors.data = Model.ops.asarray(self.vocab.vectors.data)
    else:
        device = None
    link_vectors_to_models(self.vocab)
    if self.vocab.vectors.data.shape[1]:
        cfg["pretrained_vectors"] = self.vocab.vectors.name
    if sgd is None:
        sgd = create_default_optimizer(Model.ops)
    self._optimizer = sgd
    for name, proc in self.pipeline:
        if hasattr(proc, "begin_training"):
            proc.begin_training(
                get_gold_tuples(), pipeline=self.pipeline, sgd=self._optimizer, **cfg
            )
    return self._optimizer