Adam
Adam

Reputation: 55

How to match items in two pytorch tensors

I have two pytorch tensors have the same number of rows, ( tensor1-size is (6,4) and tensor2-size is (6), The first tensor is bounding-box coordinate points, whereas the second tensor is the prediction scores.

tensor1 = 
([[780.8306,  98.1060, 813.8367, 149.8171],
[585.6562, 117.6804, 621.6012, 166.3151],
[ 88.4085, 117.1313, 129.3327, 173.1145],
[223.1263, 239.2682, 255.1892, 270.7897],
[194.4088, 117.9768, 237.3028, 166.1765],
[408.9165, 109.0131, 441.0802, 141.2362]])
tensor2 =
([0.9842, 0.9751, 0.9689, 0.8333, 0.7021, 0.6191])

How could I concatenate every item in tensor2 with its correspondent row in tensor1)?

I want get predilection-score and the bounding box. The output to be something like:

prediction  bounding box
0.9842      780.8306,  98.1060, 813.8367, 149.8171
0.9751      585.6562, 117.6804, 621.6012, 166.3151]
0.96898     88.4085, 117.1313, 129.3327, 173.1145
0.8333      223.1263, 239.2682, 255.1892, 270.7897
0.7021      194.4088, 117.9768, 237.3028, 166.1765
0.6191      408.9165, 109.0131, 441.0802, 141.2362

I appreciate If anyone could help, Thank you

Upvotes: 1

Views: 667

Answers (1)

jodag
jodag

Reputation: 22184

You can use torch.cat to concatenate the columns. Since cat requires that all tensors have the same number of dimensions you will first need to insert a unitary dimension to tensor2 to make it a (6, 1). This can be done a few ways, but the most clear is probably Tensor.unsqueeze(1) which reshapes the tensor to have a unitary dimension at dimension 1.

tensor21 = torch.cat((tensor2.unsqueeze(1), tensor1), dim=1)

Upvotes: 1

Related Questions