1

My codes:

def f(x):
    try:
        import tensorflow as tf
        # x is (None, 10, 2)
        idx = K.cast(x*15.5+15.5, "int32")
        z = tf.sparse_to_dense(idx, 32, 1.0, 0.0, name='sparse_tensor')
        print('z.shape={0}'.format(z.shape))
    except Exception as e:
        print(e)
    return x[:, :, 0:2]

drop_out = Lambda(lambda x: f(x), 
                  output_shape=drop_output_shape, name='projection')(reshape_out)

x is tensor of (None, 10, 2), where there are 10 indexes/coordinates. Trying to generate a (None, 32, 32) tensor z. I got the following error:

Shape must be rank 1 but is rank 0 for 'projection_14/sparse_tensor' (op: 'SparseToDense') with input shapes: [?,10,2], [], [], [].

How to fix it? Thanks

BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

1

The specific error you've seen is trying to say your output_shape should be a 1-D Tensor, like (32,), rather than 0-D Tensor as you had there, 32. But I worried this simple change will not solve your problem.

One thing I don't understand is why your x is a 3-D tensor when you said you have just 10 indices. Technically speaking, sparse_indices can be a 2-D tensor at most. My understanding of tf.sparse_to_dense is that it's quite similar to making a sparse tensor. So the number 2 in your (10, 2) already decided that the output tensor will be 2-D. The None, like variant sample size, should be handled differently.

Following this logic, another problem you may find is the output_shape should be (32, 32) rather than (32,) as the simple fix mentioned above. The length of the tuple should match the shape (last axis specifically) of sparse_indices.

With all these in mind, I think a tensorflow only MVCE mimicking your example could be:

import numpy as np
import tensorflow as tf

x = tf.placeholder(tf.float32, shape=(10, 2))
idx = tf.cast(x*15.5+15.5, tf.int32)
z = tf.sparse_to_dense(idx, (32, 32), 1.0, 0.0, name='sparse_tensor')

with tf.Session() as sess:
    print(sess.run(
        z, feed_dict={x: np.arange(20, dtype=np.float32).reshape((10, 2))/20})
    )

Just to point out: The tf.sparse_to_dense "FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Create a tf.sparse.SparseTensor and use tf.sparse.to_dense instead."

Y. Luo
  • 5,622
  • 1
  • 18
  • 25
  • thanks. but now I am stuck in https://stackoverflow.com/questions/53694991/keras-tensorflow-how-to-get-differentiable-tensor-from-index-information... – BAE Dec 10 '18 at 00:30