0
votes

in tensorflow I want to do the following:

  • receive N 1D tensors
  • concat them as a big 1D tensor of shape [m]
  • call a function that process this tensor and generates a tensor of shape [m]
  • split the resulting tensor in N 1D tensors

However at graph creation time, I don't know the size of each of the 1D tensors, which creates issues. Here's a snippet of what I'm doing:

    def stack(tensors):
    sizes = tf.convert_to_tensor([t.shape[0].value for t in tensors])
    tensor_stacked = tf.concat(tensors, axis=0)
    res = my_function(tensor_stacked)
    return tf.split(res, sizes, 0)


tensor_A = tf.placeholder(
    tf.int32,
    shape=[None],
    name=None
)

tensor_B = tf.placeholder(
    tf.int32,
    shape=[None],
    name=None
) 

res = stack([tensor_A, tensor_B])

This will fail on the "concat" line with the message

TypeError: Failed to convert object of type to Tensor. Contents: [None, None]. Consider casting elements to a supported type.

Is there any way I can do this in tensorflow ? At graph-time the "sizes" variables will always contain unknown sizes because the length of the 1D tensors is never known

1

1 Answers

0
votes

Ok, in the meantime I found the answer

Apparently it's enough to replace the call to tensor.shape[0] to tf.shape(tensor)[0]

So now I have:

def stack(tensors):
    sizes = tf.convert_to_tensor([tf.shape(t)[0] for t in tensors])
    print(sizes)
    tensor_stacked = tf.concat(tensors, axis=0)
    res = my_function(tensor_stacked)
    return tf.split(res, sizes, 0)