I have a list of tensors created by

[some_function(x) for x in something]

and this list becomes like this

[<tf.Tensor: shape=(), dtype=string, numpy=b'I have gone and you will go'>, <tf.Tensor: shape=(), dtype=string, numpy=b'we will go'>]

But I want to have an object like this

<tf.Tensor: shape=(3,), dtype=string, numpy=array([b'I have gone and you will go', b'we will go'], dtype=object)>

Which is a tensor of list of strings (Opposite to the previous one). Is there any way to this, assuming that I have access to all these tensors, i.e. some_function(x) for x in something separately.


1 Answer 1


In order to stack list of tf tensors you could use the tf function stack hence the name

for ur case,

tf.stack([some_function(x) for x in something],axis=0)

or you could stack them in numpy and then convert the array to a tensor, to do so using numpy np.stack inputting a list and axis


Your Answer

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