{"record":{"id":"cd56768067f3dc32","repo":"hankcs/HanLP","slug":"unsupported-init-init","errorCode":null,"errorMessage":"Unsupported init {init}","messagePattern":"Unsupported init (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hanlp/layers/embeddings/util.py","lineNumber":62,"sourceCode":"    ids = []\n\n    unk_id_offset = 0\n    for word, idx in vocab.token_to_idx.items():\n        word_id = pret_vocab.get(word, None)\n        # Retry lower case\n        if word_id is None:\n            word_id = pret_vocab.get(word.lower(), None)\n        if word_id is None:\n            word_id = len(pret_vocab) + unk_id_offset\n            unk_id_offset += 1\n        ids.append(word_id)\n    if unk_id_offset:\n        unk_embeds = torch.zeros(unk_id_offset, pret_matrix.size(1))\n        if init and init != 'zeros':\n            if init == 'uniform':\n                init = embedding_uniform\n            else:\n                raise ValueError(f'Unsupported init {init}')\n            unk_embeds = init(unk_embeds)\n        pret_matrix = torch.cat([pret_matrix, unk_embeds])\n    ids = torch.LongTensor(ids)\n    embedding = pret_matrix.index_select(0, ids)\n    if normalize == 'norm':\n        embedding /= (torch.norm(embedding, dim=1, keepdim=True) + 1e-12)\n    elif normalize == 'l2':\n        embedding = torch.nn.functional.normalize(embedding, p=2, dim=1)\n    elif normalize == 'std':\n        embedding /= torch.std(embedding)\n    else:\n        raise ValueError(f'Unsupported normalization method {normalize}')\n    return embedding\n\n\ndef build_word2vec_with_vocab(embed: Union[str, int],\n                              vocab: Vocab,\n                              extend_vocab=True,","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/layers/embeddings/util.py#L44-L80","documentation":"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'.","triggerScenarios":"Passing init='normal' or init='xavier' etc. to build_word2vec_with_vocab / build_embeddings with unk_id_offset > 0.","commonSituations":"Copying config snippets from other libraries expecting xavier/normal init names; typo like init='uniform ' or 'Uniform'.","solutions":["Use init='zeros' (default) or init='uniform'","Remove the init option to get zeros","If you need custom init, initialize the UNK rows after building the embedding"],"exampleFix":"# before\nembed = build_word2vec_with_vocab(path, vocab, init='normal')  # error\n# after\nembed = build_word2vec_with_vocab(path, vocab, init='uniform')","handlingStrategy":"validation","validationCode":"assert init in (None, 'zeros', 'uniform'), f'bad init: {init}'","typeGuard":"def valid_init(init) -> bool:\n    return init in (None, 'zeros', 'uniform')","tryCatchPattern":null,"preventionTips":["Stick to documented init values","Omit init unless UNK noise is desired"],"tags":["hanlp","word2vec","embedding-init","config-validation"],"backgroundTag":"invalid-config-value","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}