5

I am trying Tensorflow 2.0 alpha preview and was testing the Eager execution . My doubt is that if you have a numpy array of variable size in middle like

input.shape
(10,)

input[0].shape
(109, 16)

input[1].shape
(266, 16)

and so on for the rest of the array , how does one eagerly convert them to tensors.

when I try

tf.convert_to_tensor(input)

or

tf.Variable(input)

I get

ValueError: Failed to convert numpy ndarray to a Tensor (Unable to get element as bytes.).

Converting each sub-array works , but because the sub-array size isn't same , tf.stack doesn't work.

Any help or suggestions ?

4
  • 1
    I think you may want to use a ragged tensor.
    – javidcf
    Mar 21, 2019 at 10:29
  • 1
    I tried ragged , but still the same error. Any other suggestions ? Mar 21, 2019 at 10:37
  • You input appears to be a NumPy array of objects, maybe try converting it to a list instead (list(input))?
    – javidcf
    Mar 21, 2019 at 11:16
  • @jdehesa Ragged tensors using lists requires me to make a list of lists , something I wouldnt like to do with large sizes of my array . Mar 21, 2019 at 16:05

3 Answers 3

3

This was happening to me in eager as well. Looking at the docs here , I ended up trying

tf.convert_to_tensor(input, dtype=tf.float32)

And that worked for me.

1

If you can make lists of arrays, then tf.ragged.stack should do it. You can use it like this for example:

tf.ragged.stack([tf.convert_to_tensor(arr) for arr in arrays], axis=0)

This will stack uneven sized arrays into a RaggedTensor.

0
0

It seems that the only way to work with this is to use lists of lists and then convert them to ragged tensors, since numpy doesnt support ragged arrays very well. Will Update if I find anything new

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.