30

I want to change list to tensor with tf.convert_to_tensor, data is following:

data=[

array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   1., 0., 0.]), 
array([0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]), 
array([0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
   0., 0., 0.]), 
array([0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
    ]

it didn't work, system says:

ValueError: Can't convert non-rectangular Python sequence to Tensor.

how to solve this problem?

Richard
  • 56,349
  • 34
  • 180
  • 251
andy
  • 1,951
  • 5
  • 16
  • 30

2 Answers2

35

I'm not sure whether they exist in TensorFlow 1 but TensorFlow 2.0 supports RaggedTensors, which the documentation describes as "... the TensorFlow equivalent of nested variable-length lists."

I think it would be trivial to convert your data to RaggedTensors. It might even be as easy as:

data_tensor = tf.ragged.constant(data)

Example:

>>> a = tf.ragged.constant([[1],[2,3]])
>>> a
<tf.RaggedTensor [[1], [2, 3]]>
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Eric McLachlan
  • 3,132
  • 2
  • 25
  • 37
10

You can't. Like the error message says, TensorFlow arrays can not have different sizes along one dimension. Try to use a list of TensorFlow arrays instead or the dataset api.

BlueSun
  • 3,541
  • 1
  • 18
  • 37
  • Here is one example I gave of using the tf.data.Dataset API, but the tensors were all the same shape: https://stackoverflow.com/a/66175008/12763497 – brethvoice Feb 12 '21 at 15:56