在Keras中使用稀疏输入和Tensorflow

raj*_*900 5 numpy keras tensorflow

我试图使用稀疏numpy矩阵作为后端的张量流的keras.模型编译但适合时会出错.代码如下.任何帮助表示赞赏.

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'])
Run Code Online (Sandbox Code Playgroud)

trainX是

<2404941x337071 sparse matrix of type '<type 'numpy.float64'>'
with 4765705 stored elements in Compressed Sparse Row format>
Run Code Online (Sandbox Code Playgroud)

类似地,trainY是CSR矩阵

model.fit(trainX, trainY, verbose=1)
Run Code Online (Sandbox Code Playgroud)

给出以下错误

ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)

Mau*_*Qch 2

如果您编写自定义训练循环,则可以使用稀疏矩阵作为 Keras 模型的输入。在下面的示例中,模型采用稀疏矩阵作为输入并输出密集矩阵。

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]
Run Code Online (Sandbox Code Playgroud)

从你的例子来看,你似乎也希望你的输出是一个稀疏矩阵。这更加困难,因为您的模型需要输出稀疏矩阵,并且您的损失必须可以用稀疏矩阵计算。此外,我相信 Keras 还不支持稀疏输出。