[torch] How to connect several tensors? (with source code)

One, cat

In PyTorch, to add elements to a tensor, you usually need to create a new tensor, and then add elements to the new tensor. PyTorch tensors are immutable, so elements cannot be appended directly like lists. Here's one way how to add elements to a tensor:

import torch

# 初始的 tensor
x = torch.tensor([0.6580, -1.0969, -0.4614])

# 要添加的元素
new_element = torch.tensor([1.0000, 2.0000, 3.0000])

# 创建一个新的 tensor,将原始 tensor 和新元素拼接在一起
result = torch.cat((x, new_element), dim=0)

print(result)

Our output is:

tensor([ 0.6580, -1.0969, -0.4614,  1.0000,  2.0000,  3.0000])

Two, stack

Our above result is: tensor([ 0.6580, -1.0969, -0.4614, 1.0000, 2.0000, 3.0000]), but what if I want to convert the result to the following format?

tensor([[ 0.6580, -1.0969, -0.4614],
        [ 1.0000,  2.0000,  3.0000]])

How can I achieve this? You can use the stack function.

import torch

x = torch.tensor([0.6580, -1.0969, -0.4614])
y = torch.tensor([1.0000, 2.0000, 3.0000])

# Concatenate x and y as rows (along dimension 0) to form a 2x3 tensor
result = torch.stack((x, y))

print(result)

Our output is:

tensor([[ 0.6580, -1.0969, -0.4614],
        [ 1.0000,  2.0000,  3.0000]])

But if we continue to add, when the dimensions of the tensor are different, the addition will fail:

result = torch.stack((result, x))
result

The code will report an error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
d:\CodeProject\37.帮助大家看代码\demo.ipynb 单元格 18 in 1
----> 1 result = torch.stack((result, x))
      2 result

RuntimeError: stack expects each tensor to be equal size, but got [2, 3] at entry 0 and [3] at entry 1

Prompt us that the dimensions of the tensor must be the same.

Are the functions limited?

3. How does cat perform line-by-line merging?

If you want to append tensors one by one, you can use PyTorch's torch.cat function to create a new tensor each time you append. Here is an example showing how to append tensors one by one:

import torch

# 创建一个空的 tensor
result = torch.tensor([])

# 逐个追加 tensor
x = torch.tensor([0.6580, -1.0969, -0.4614])
result = torch.cat((result, x.view(1, -1)), dim=0)

y = torch.tensor([1.0000, 2.0000, 3.0000])
result = torch.cat((result, y.view(1, -1)), dim=0)

# 打印结果
print(result)

In this example, we first create an empty tensor result, and then append x and y one by one. Note the use of view(1, -1) to transform each tensor into a row vector to ensure that they can be connected together correctly. Finally, the result contains tensors that are appended one by one.

You can continue to use a similar method to append more tensors one by one.

The output of the above is:

tensor([[ 0.6580, -1.0969, -0.4614],
        [ 1.0000,  2.0000,  3.0000]])

Let's test whether we can append more tensors one by one:

result = torch.cat((result, x.view(1, -1)), 0)
result

We tested it four times and the results are as follows:

tensor([[ 0.6580, -1.0969, -0.4614],
        [ 1.0000,  2.0000,  3.0000],
        [ 0.6580, -1.0969, -0.4614],
        [ 0.6580, -1.0969, -0.4614],
        [ 0.6580, -1.0969, -0.4614],
        [ 0.6580, -1.0969, -0.4614]])

Guess you like

Origin blog.csdn.net/wzk4869/article/details/132710522