guorui
guorui

Reputation: 891

How to merge two tensor?

I want to merge two tensors a and b into c. What I've tried is:

enter image description here

But my expectation is c = [1, 2, 3, 4, 5, 6]. How can I get it?

Upvotes: 2

Views: 16649

Answers (2)

Mahsa Yazdani
Mahsa Yazdani

Reputation: 111

If you want to work with torch tensors, as this link suggests, you can do as following:

import torch

a = torch.Tensor([1,2,3])
b = torch.Tensor([4,5,6])
torch.cat((a,b))

output:

tensor([1., 2., 3., 4., 5., 6.])

and for stacking:

torch.stack((a,b))

output:

tensor([[1., 2., 3.], [4., 5., 6.]])

Upvotes: 0

javidcf
javidcf

Reputation: 59701

tf.stack concatenate the given tensors along a new dimension. If you want to concatenate across an existing dimension, use tf.concat:

c = tf.concat([a, b], axis=0)

Upvotes: 9

Related Questions