(为了防止内存错误)如何使用 Tokenize class 在 Keras 中将单词列表热编码为 INTEGER 8 的矩阵
(To prevent Memory Error)How to one hot encode word list to a matrix of INTEGER 8 in Keras using Tokenize class
AS FLOAT64占用更多内存,这是标记化矩阵的默认数据类型,我希望它是INT8,从而节省space。
这就是我说的方法,
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 using np.zeros()
with no dtype
keyword argument which would result in dtype
being set to default value set in function definition,即 float
。我认为选择这种数据类型是为了支持所有形式的转换,例如 tfidf
导致 non-integer 输出。
所以我认为你必须选择:
1.更改源代码
您可以将关键字参数添加到 texts_to_matrix
的定义中,例如 dtype
,并将创建矩阵的 the line 更改为
x = np.zeros((len(sequences), num_words), dtype=dtype)
2.Use另一个预处理工具:
您可以使用其他工具预处理您的文本,然后将其提供给 keras 网络。例如,您可以使用 scikit learn 的 CountVectorizer,例如:
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(dtype=np.int8, ...)
matrix = cv.fit_transform(texts).toarray()
AS FLOAT64占用更多内存,这是标记化矩阵的默认数据类型,我希望它是INT8,从而节省space。
这就是我说的方法,
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 using np.zeros()
with no dtype
keyword argument which would result in dtype
being set to default value set in function definition,即 float
。我认为选择这种数据类型是为了支持所有形式的转换,例如 tfidf
导致 non-integer 输出。
所以我认为你必须选择:
1.更改源代码
您可以将关键字参数添加到 texts_to_matrix
的定义中,例如 dtype
,并将创建矩阵的 the line 更改为
x = np.zeros((len(sequences), num_words), dtype=dtype)
2.Use另一个预处理工具: 您可以使用其他工具预处理您的文本,然后将其提供给 keras 网络。例如,您可以使用 scikit learn 的 CountVectorizer,例如:
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(dtype=np.int8, ...)
matrix = cv.fit_transform(texts).toarray()