I am trying to use Sparse numpy matrix for keras with tensorflow as backend. The model compiles but while fit, gives an error. Code is as follows. Any help is appreciated.
from keras.layers import Dense, Input
from keras.models import Model
inputs = Input(shape=(trainX.shape[1],), sparse=True)
outputs = Dense(trainY.shape[1], activation='softmax')(inputs)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
trainX is
<2404941x337071 sparse matrix of type '<type 'numpy.float64'>'
with 4765705 stored elements in Compressed Sparse Row format>
and similarly trainY is a CSR matrix
model.fit(trainX, trainY, verbose=1)
gives following error
ValueError: setting an array element with a sequence.
It is possible to use sparse matrices as inputs to a Keras model if you write a custom training loop. In the example below, the model takes a sparse matrix as an input and outputs a dense matrix.
from keras.layers import Dense, Input
from keras.models import Model
import scipy
import numpy as np
trainX = scipy.sparse.random(1024, 1024)
trainY = np.random.rand(1024, 1024)
inputs = Input(shape=(trainX.shape[1],), sparse=True)
outputs = Dense(trainY.shape[1], activation='softmax')(inputs)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
steps = 10
for i in range(steps):
# For simplicity, we directly use trainX and trainY in this example
# Usually, this is where batches are prepared
print(model.train_on_batch(trainX, trainY))
# [3549.2546, 0.0]
# ...
# [3545.6448, 0.0009765625]
From your example, it seems that you would like your output to be a sparse matrix too. This is more difficult as your model needs to output a sparse matrix and your loss has to be computable with sparse matrices. Moreover, I believe Keras does not support sparse outputs yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With