a = torch.tensor([1,2,3,4])
b = torch.tensor([2,3,5,1])
위와 같은 두 텐서가 있을때, 서로 같은지 알고싶다..
크기가 작으니 바로 보면 다르다는 것 알 수 있는데 얘 둘을 비교해보라고 한다면?

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

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

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

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랑 같은 역할인듯?
'프로그래밍 > Pytorch' 카테고리의 다른 글
torch.where()로 tensor내 특정 원소의 위치를 찾기 (0) | 2024.04.14 |
---|---|
Pytorch의 computational graph와 backward()에 대해 이해하기 (0) | 2024.04.13 |
pretrained된 computer vision 모델에서 마지막 linear layer는 제거하고 feature만 뽑는법 (0) | 2024.04.11 |
NLP text data 전처리에서 tokenizing할 때 padding이 필요한 이유 (0) | 2024.03.31 |
NLP huggingface model에 input을 제대로 넣었는데 IndexError: index out of range in self가 나는 이유 (0) | 2024.03.31 |