Python 实现将句子序列转换为词向量序列
def sentences_to_indices(X, word_to_index, dim, max_len, E): m = X.shape[0] # 句子数量 X_indices = np.zeros((m, max_len, dim))
for i in range(m):
sentence_words = X[i].lower().split()
j = 0
for word in sentence_words:
if j >= max_len:
break
if word in word_to_index:
X_indices[i, j, :] = E[word_to_index[word]]
j += 1
return X_indices
原文地址: https://www.cveoy.top/t/topic/oUiU 著作权归作者所有。请勿转载和采集!