hankcs/HanLP · error · ValueError

Unsupported init {init}

Error message

Unsupported init {init}

What it means

When indexing pretrained word2vec/fastText vectors against a vocabulary with unk_id_offset, HanLP can initialize the UNK row(s) with zeros (default) or uniform noise (embedding_uniform). index_word2vec_with_vocab rejects any other init string to avoid silently creating untrained rows. The check only applies when init is truthy and not 'zeros'.

Source

Thrown at hanlp/layers/embeddings/util.py:62

    ids = []

    unk_id_offset = 0
    for word, idx in vocab.token_to_idx.items():
        word_id = pret_vocab.get(word, None)
        # Retry lower case
        if word_id is None:
            word_id = pret_vocab.get(word.lower(), None)
        if word_id is None:
            word_id = len(pret_vocab) + unk_id_offset
            unk_id_offset += 1
        ids.append(word_id)
    if unk_id_offset:
        unk_embeds = torch.zeros(unk_id_offset, pret_matrix.size(1))
        if init and init != 'zeros':
            if init == 'uniform':
                init = embedding_uniform
            else:
                raise ValueError(f'Unsupported init {init}')
            unk_embeds = init(unk_embeds)
        pret_matrix = torch.cat([pret_matrix, unk_embeds])
    ids = torch.LongTensor(ids)
    embedding = pret_matrix.index_select(0, ids)
    if normalize == 'norm':
        embedding /= (torch.norm(embedding, dim=1, keepdim=True) + 1e-12)
    elif normalize == 'l2':
        embedding = torch.nn.functional.normalize(embedding, p=2, dim=1)
    elif normalize == 'std':
        embedding /= torch.std(embedding)
    else:
        raise ValueError(f'Unsupported normalization method {normalize}')
    return embedding


def build_word2vec_with_vocab(embed: Union[str, int],
                              vocab: Vocab,
                              extend_vocab=True,

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Use init='zeros' (default) or init='uniform'
  2. Remove the init option to get zeros
  3. If you need custom init, initialize the UNK rows after building the embedding

Example fix

# before
embed = build_word2vec_with_vocab(path, vocab, init='normal')  # error
# after
embed = build_word2vec_with_vocab(path, vocab, init='uniform')
Defensive patterns

Strategy: validation

Validate before calling

assert init in (None, 'zeros', 'uniform'), f'bad init: {init}'

Type guard

def valid_init(init) -> bool:
    return init in (None, 'zeros', 'uniform')

Prevention

When it happens

Trigger: Passing init='normal' or init='xavier' etc. to build_word2vec_with_vocab / build_embeddings with unk_id_offset > 0.

Common situations: Copying config snippets from other libraries expecting xavier/normal init names; typo like init='uniform ' or 'Uniform'.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27). Data as JSON: /api/errors/cd56768067f3dc32. Report an issue: GitHub.