3.编程实现将一组句子序列转换成词向量序列形式使得所有句子的长度相同。 def sentences _ to _ indices X word _ to _ index dim max _ len E n Arguments X -- array of sentences strings of shape m 1 word _ to _ index -- a dictiona
def sentences_to_indices(X, word_to_index, dim, max_len, E): m = X.shape[0] # number of sentences 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/hrr7 著作权归作者所有。请勿转载和采集!