本文介绍了使用 Tensorflow 在 Keras 中使用稀疏输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将稀疏 numpy 矩阵用于以 tensorflow 作为后端的 keras.模型编译但在拟合时出现错误.代码如下.任何帮助表示赞赏.

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 是

<2404941x337071 sparse matrix of type '<type 'numpy.float64'>'
with 4765705 stored elements in Compressed Sparse Row format>

同样,trainY 是一个 CSR 矩阵

and similarly trainY is a CSR matrix

model.fit(trainX, trainY, verbose=1)

出现以下错误

ValueError: setting an array element with a sequence.

推荐答案

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

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]

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

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.

这篇关于使用 Tensorflow 在 Keras 中使用稀疏输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 12:23