使用自定义 Word2Vec 嵌入代替 GloVe

问题描述

我正在使用 seq2seq 模型生成文本,其中使用了 glove 嵌入。我想在此代码中使用嵌入的自定义 Word2Vec(CBOW/Gensim)。任何人都可以帮助使用我的自定义嵌入而不是 glove 吗?

    def initialize_embeddings(self):
        """Reads the glove word-embeddings and creates embedding matrix and word to index and index to word mapping."""
        
        # load the word embeddings
        self.word2vec = {}
        with open(glove_path%self.EMbedDING_DIM,'r') as file:
            for line in file:
                vectors = line.split()
                self.word2vec[vectors[0]] = np.asarray(vectors[1:],dtype="float32")```

                
        ```# get the embeddings matrix
        self.num_words = min(self.MAX_VOCAB_SIZE,len(self.word2idx)+1)
        self.embeddings_matrix = np.zeros((self.num_words,self.EMbedDING_DIM))
        
        for word,idx in self.word2idx.items():
            if idx <= self.num_words:
                word_embeddings = self.word2vec.get(word)
                if word_embeddings is not None:
                    self.embeddings_matrix[idx] = word_embeddings
                    
        self.idx2word = {v:k for k,v in self.word2idx.items()}

代码用于将 glove 嵌入转换为 Word2Vec。我想加载我自己的 Word2Vec 嵌入。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)