因为 FLOAT64 需要更多的内存,这是标记化矩阵的默认数据类型,我希望它是 INT8,从而节省空间。
这就是我说的方法,
texts_to_matrix(texts):
Return: numpy array of shape (len(texts), num_words).
Arguments:
texts: list of texts to vectorize.
mode: one of "binary", "count", "tfidf", "freq" (default: "binary").
请您参考如下方法:
查看源代码,创建结果矩阵here使用不带 dtype 关键字参数的 np.zeros() 会导致 dtype 被设置为 function definition 中设置的默认值这是 float。我认为选择这种数据类型是为了支持所有形式的转换,例如 tfidf,它会导致非整数输出。 所以我认为你必须选择:
<强>
1。更改源代码 您可以将关键字参数添加到
texts_to_matrix 的定义中,例如
dtype 并更改
the line创建矩阵的地方
强>
x = np.zeros((len(sequences), num_words), dtype=dtype)
2.使用其他工具进行预处理: 您可以使用其他工具预处理您的文本,然后将其提供给 keras 网络。例如,您可以使用 scikit learn 的 CountVectorizer喜欢:
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(dtype=np.int8, ...)
matrix = cv.fit_transform(texts).toarray()
