I used this Keras word-level text generation example as a base for my own work.
As I tried to train data on it, I got a MemoryError in line 59:
X = np.zeros((len(sentences), maxlen, len(words)), dtype=np.bool)
as a big matrix is initialized with zeros. The same zero initialization will happen to y:
y = np.zeros((len(sentences), len(words)), dtype=np.bool)
Later on the matrices will be filled like that (one hot encoding):
for i, sentence in enumerate(sentences):
for t, word in enumerate(sentence.split()):
X[i, t, word_indices[word]] = 1
y[i, word_indices[next_words[i]]] = 1
How can I use sparse matrices to save memory and make it executable?
I looked at scipy's sparse matrix, but it looked like they only support 2D matrices.
I also looked at Tensorflow's sparse tensor but I think they don't support zero initialization to fill them in later (like I need).