title: Why can't my sparse tensor convert to a tensor
body: <p>I want to feed a sparse tensor as input to a simple tensorflow-based recurrent neural network. For that I use the following code:</p>
  <pre><code>model = keras.Sequential()
  model.add(keras.Input(shape=(1, features_dim), sparse=True))
  model.add(keras.layers.RNN(keras.layers.SimpleRNNCell(hidden_layer_dim, activation=&quot;sigmoid&quot;)))
  model.add(keras.layers.Dense(labels_dim, activation='softmax'))
  
  model.compile(optimizer=keras.optimizers.SGD(lr=learning_rate, momentum=0.0, decay=0.0, nesterov=False),
                loss='sparse_categorical_crossentropy',
                metrics=[keras.metrics.SparseCategoricalAccuracy()])
  
  model.fit(
        training_data, 
        training_labels,
        epochs=num_epochs, 
        batch_size=batch_size, 
        shuffle=True
      )
  </code></pre>
  <p><code>features_dim</code> denotes the number features, <code>training_labels</code> is a NumPy array containing the respective labels and training_data is a sparse tensor created from a sparse matrix in COO-format with the shape ([num_entries, 1, num_features]).</p>
  <p>However, when I try to run this I get this error:</p>
  <blockquote>
  <p>TypeError: Failed to convert object of type &lt;class 'tensorflow.python.framework.sparse_tensor.SparseTensor'&gt; to Tensor. Contents: SparseTensor(indices=Tensor(&quot;cond_1/Identity:0&quot;, shape=(None, 3), dtype=int64, device=/job:localhost/replica:0/task:0/device:CPU:0), values=Tensor(&quot;cond_1/Identity_1:0&quot;, shape=(None,), dtype=float64, device=/job:localhost/replica:0/task:0/device:CPU:0), dense_shape=Tensor(&quot;stack:0&quot;, shape=(3,), dtype=int64, device=/job:localhost/replica:0/task:0/device:CPU:0)). Consider casting elements to a supported type.</p>
  </blockquote>
  <p>I'm a bit lost about this error. I was under the impression that TensorFlow can handle sparse data without having to convert it. Furthermore, if a conversion is necessary, I'm not sure why it fails. I can run the code above (without sparse=True) <em>just fine</em>, if I convert the sparsetensor <strong>manually</strong> using <code>tf.sparse.to_dense()</code>. Thus there does not seem to be anything wrong with how I created the sparsetensor from the coo-matrix (at least not as far as I can tell).</p>
  <p>Edit:</p>
  <p>I have now tested the answers provided in
  <a href="https://stackoverflow.com/questions/41538692/using-sparse-matrices-with-keras-and-tensorflow">Using sparse matrices with Keras and Tensorflow</a> extensively. Unfortunately, neither of them solves my problem without sacrificing at least the sparsity of the batch. Is there no other possibility?</p>