Pytorch에서 두 tensor가 서로 같은지 비교하고 싶다면?

a = torch.tensor([1,2,3,4])
b = torch.tensor([2,3,5,1])

 

위와 같은 두 텐서가 있을때, 서로 같은지 알고싶다..

 

크기가 작으니 바로 보면 다르다는 것 알 수 있는데 얘 둘을 비교해보라고 한다면?

 

etc-image-0

 

 

a == b 해버리면... 예상하는 결과와 다르게 나온다

 

etc-image-1

 

 

이렇게 하면 진짜 True인지 False인지 바로 알기 어렵다.. tensor의 원소 하나하나 True인지 False인지 체크해야하거든..

 

etc-image-2

 

 

torch.all()을 이용해서 boolean tensor의 모든 boolean을 비교해서... 전부 True이면 True이고 하나라도 False이면 False

 

etc-image-3

 

 

 

https://doheejin.github.io/pytorch/2021/02/13/pytorch-function.html

 

[PyTorch] 알아두면 유용한 함수 모음 - 비교(comparison) 함수

비교(comparison)를 위한 함수element-wise로 비교하는 함수로는 torch.eq(같은지) torch.ne(다른지) torch.ge(크거나 같은지) torch.le(작거나 같은지)등이 있고, 텐서 차원에서 비교하는 함수로는 torch.equal, torch.

doheejin.github.io

 

 

단순히 == 말고 torch.eq, torch.ne, torch.ge, torch.le, torch.equal 등등 비교 함수가 있다.

 

eq는 equal(같으면 True)

 

ne는 not equal(다르면 True)

 

ge는 greater_equal(크거나 같으면 True)

 

le는 less_equal(작거나 같으면 True)

 

==은 element wise에서 비교하는 torch.eq랑 같은 역할인듯?

728x90