How to append an int tensor to a list tensor?

In python, I can use:

A = [1,2]
B = 3

to get [1,2,3]. However, In pytorch,

A = torch.tensor([1,2])
B = torch.tensor(3)

How to get:


Thank you very much

Hello. I believe it could be done in many ways. A reasonable choice for me would be .cat method. Just make sure that the tensor B is also 1-dimensional as A.
This should work:

A = torch.tensor([1,2])
B = torch.tensor([3]),B))

Your B tensor is zero dimesional, so you can’t use function to concatenate them.

1)Concatenate them as python array and convert them to tensor
2)Add dimension with unsqueeze() method and use

1 Like

I use 2) and solve my problem. Thanks

1 Like