我正在查看Keras Glove单词嵌入示例,但不清楚为什么嵌入矩阵的第一行填充零。
首先,在单词与数组相关联的位置创建嵌入索引。
embeddings_index = {}
with open(os.path.join(GLOVE_DIR,'glove.6B.100d.txt')) as f:
for line in f:
word,coefs = line.split(maxsplit=1)
coefs = np.fromstring(coefs,'f',sep=' ')
embeddings_index[word] = coefs
然后通过查看标记器创建的索引中的单词来创建嵌入矩阵。
# prepare embedding matrix
num_words = min(MAX_NUM_WORDS,len(word_index) + 1)
embedding_matrix = np.zeros((num_words,EMBEDDING_DIM))
for word,i in word_index.items():
if i >= MAX_NUM_WORDS:
continue
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
# words not found in embedding index will be all-zeros.
embedding_matrix[i] = embedding_vector
由于循环将从i=1
开始,因此如果矩阵初始化不同,则第一行将仅包含零和随机数。有跳过第一行的原因吗?