如何将多于一位的数字添加到 word2vec 词汇表中

How to add numbers with more than one digit to the word2vec-vocabulary

我正在尝试使用 word2vec 获取包含 1043 个节点的列表的嵌入。当我尝试构建词汇表时,我发现 word2vec 使用节点列表并将它们视为单个数字,例如“143”变为“1”、“4”、“3”。

我已经尝试将所有数字作为单个条目查看是否是格式问题并使用 buil_vocab_from_freq 而不是 build_vocab,但这也只会产生错误(对象类型 'int' 没有 len()).

我的代码如下:

from gensim.models import Word2Vec

def generateEmbeddings(all_walks,dimension,min_count):
    model = Word2Vec(min_count = min_count, size = dimension)
    mylist = list(range(1,1043))
    corpus = {}
    j=1
    for i in mylist:
      corpus[str(i)] = j
      j=j+1
    #mylist = [str(i) for i in mylist]
    print(corpus)
    model.build_vocab_from_freq(corpus)
    model.train(mylist, total_examples=model.corpus_count, epochs = 30)
    #if it reaches this point it throws the error "14 not found in vocabulary"
    print(model.wv.most_similar(positive=['14']))
    return model

print(generateEmbeddings(all_walks,128,2))

我想要嵌入例如。数字“14”而不是现在的“1”。感谢您的帮助!

//编辑

我设法解决了这个问题,如果其他人遇到这个特定问题: 您必须将列表格式化为 [["1","102","43"],["54","43"]] 等。 您不能在运行时更改旧列表(或者至少它不像我那样工作),因此您可以在运行时使用

创建一个新列表
new_list = []
    for i in all_walks:
      temp_list = []
      for j in i:
        temp_list.append(str(j))
      new_list.append(temp_list)

根据我们上面的讨论,工作方法将提供 Word2Vec 它期望的那种语料库——一个可迭代的序列,其中每个项目都是 string-tokens 的列表。

所以,list-of-lists-of-strings 会起作用,比如...

[
  ['1','2','3'],
  ['1','2','4'],
  ['10','11','12'],
  ['10','14','15','900']
]

...而不是其中包含原始整数的任何内容(如 list(range(1, 1043))。