'Comparing two 2d tensors in PyTorch
I am quite new to PyTorch. I will like to know how to compare two tensors at the same index location in their respective tensors and output the maximum. For example, given a 4x4 tensors A and B as below:
A =tensor([[0.2846, 0.9896, 0.0401, 0.7612],
[0.1508, 0.1701, 0.3395, 0.4106],
[0.1685, 0.3624, 0.0622, 0.2306],
[0.0880, 0.0559, 0.0294, 0.4382]])
B= tensor([[0.4853, 0.2833, 0.8011, 0.0570],
[0.2024, 0.8363, 0.0883, 0.8944],
[0.2973, 0.1669, 0.4703, 0.3508],
[0.1581, 0.6574, 0.8502, 0.4210]])
I want to be able to compare A[0,0] with B[0,0], A[1,1] with B[1,1]...A[n,n] with B[n,n] and then output the max between the two.
What I have done: I have used nested loops like this :
for i in range(A.size()[0]):
for j in range(A.size()[1]):
for m in range(B.size()[0]):
for n in range(B.size()[1]):
if A[i,j] > B[m,n]:
max_nos = A[i,j]
print(max_nos)
else:
max_nos = B[m,n]
print(max_nos)
I got the result below which is not the result I want(dim 64,1):
tensor(0.4853)
tensor(0.2846)
tensor(0.8011)
tensor(0.2846)
...
...
...
From the tensor A and B above, my expected result should be (dim 16,1):
tensor(0.4853)
tensor(0.9896)
tensor(0.8011)
...
...
Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
