Qubix
Qubix

Reputation: 4353

Tensorflow stack vectors out of loop to create a matrix

I have a for loop that creates vectors (tf tensors) of equal length, say

a1 = [0, 2, 4 ... ]
a2 = [1, 4, 6 ... ]
...

and I want to concatenate these vectors into a matrix, along the 0th axis

matrix = [[0,2,4...] , [1,4,6...] ... ]

I can do a

matrix = tf.concat(0, [matrix, a])

inside the for loop. However the first iteration does not work, since matrix does not exist and if I initialize it to a vector, I'm stuck with that vector at the top of the end matrix. Is there a quick way of doing this?

Upvotes: 0

Views: 1179

Answers (1)

P-Gn
P-Gn

Reputation: 24651

You can use tf.stack:

matrix = tf.stack([a1, a2, ...])

Upvotes: 1

Related Questions