在 Keras 中实现 Word2Vec 有几种方式。
方法 1:使用预训练的 Word2Vec 模型
如果你想使用预训练的 Word2Vec 模型,你可以使用 Gensim 库加载预训练的模型,然后将其转换为 Keras 的 Embedding 层。
from gensim.models import Word2Vec
# Load the pre-trained Word2Vec model
model = Word2Vec.load("word2vec.model")
# Get the word embeddings from the pre-trained model
embeddings = model.wv
# Convert the word embeddings into a matrix
embedding_matrix = embeddings.vectors
# Create a Keras Embedding layer using the matrix
embedding_layer = Embedding(input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights=[embedding_matrix],
trainable=False)