Tensorflow自动编码器稀疏数据

时间:2020-08-28 07:38:09

标签: python tensorflow sparse-matrix

我目前正在调整我的代码以使其能够处理稀疏数据,而我被困在此自动编码器中:

model = keras.Sequential([
    keras.Input(shape=(input_dim, 1), sparse=True),
    keras.layers.LSTM(outer_hidden_dim, activation='relu', return_sequences=True, kernel_regularizer=keras.regularizers.l2(0.00)),
    keras.layers.LSTM(inner_hidden_dim, activation='relu', return_sequences=False),
    keras.layers.RepeatVector(training_data.shape[1]),
    keras.layers.LSTM(inner_hidden_dim, activation='relu', return_sequences=True),
    keras.layers.LSTM(outer_hidden_dim, activation='relu', return_sequences=True),
    keras.layers.TimeDistributed(keras.layers.Dense(input_dim))
  ])

model.compile(optimizer='adam', loss='mae')

model.fit(
  training_data,
  training_data,
  batch_size=batch_size, 
  shuffle=True,
  epochs=epochs,
  callbacks=[statistics, tensorboard_callback]
)

作为训练数据,我使用从COO矩阵创建的SparseTensor:

training_data = tf.sparse.reorder(tf.SparseTensor(
  indices=np.array([training_data.row, training_data.col]).T,
  values=training_data.data,
  dense_shape=training_data.shape
))
training_data = tf.sparse.reshape(training_data, [-1, 1, input_dim])

当我尝试运行它时,出现以下错误:

Failed to convert object of type <class 
'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: 
SparseTensor(indices=Tensor("input_1/indices:0", shape=(None, 3), dtype=int64), 
values=Tensor("input_1/values:0", shape=(None,), dtype=float64), 
dense_shape=Tensor("input_1/shape:0", shape=(3,), dtype=int64)). Consider casting elements 
to a supported type.

我尝试使用自定义训练循环;但是,我在那里也遇到相同的错误。我可以在训练循环中对每个批次进行致密化处理,但这将使我为什么首先要处理稀疏数据的目的变得不成立。任何帮助将不胜感激。

0 个答案:

没有答案