Hello, I have an awkward array composed of 2D (awkward) arrays; the function "convert_to_tensor " produces a single tensor, while I would like to transform the array into an array of tensors (each of which would correspond to the inner 2D arrays). Would there be a way to achieve this result?
A Warm welcome to the Tensorflow forum!
To convert an awkward array composed of 2D arrays into an array of tensors where each tensor corresponds to the inner 2D arrays, I would recommend you to use a list comprehension
to convert each inner array to a tensor.Here’s a sample implementation gist for your reference.
Hope this helps.Thank You.
Thanks, and maybe I have not mentioned this in my original question, but I need to convert the inner arrays into tensors without looping over them (i.e. all entries simultaneously). Is there a way to do that?
Thanks for clarifying with more details.I recommend to use ak.to_raggedtensor
to convert the awkward array to ragged tensor(for handling variable length sequence) and then use tf.split
to divide a tensor into multiple sub-tensors along a specified axis.Here is a sample implementation gist for above for your reference and let us know more context of your use case if you have any questions.
In addition, kindly check out this ragged tensor and tf.split for more information.
Thanks, I was able to split the tensor into a list of tensors, but the final output I would like to have is an awkward array, where each entry is a tensor. I tried to convert the list of tensors I got from tf.split to an awkward array of tensors using ak.Array, but that gives me the following error.
ValueError: cannot convert <tf.Tensor: shape=(), dtype=float64, numpy=66.84872436523438> (type EagerTensor) to an array element
So is there another way to do that? (Of course without looping in any way?)