728x90

참고링크 : https://pytorch.org/docs/stable/generated/torch.ne.html

 

torch.ne

import torch

torch.ne(input, other,*, out=None)

# tensor을 반환한다.

 

Parameter:

  • input(tensor) - 비교할 텐서
  • other(Tensor or float) - 비교할 텐서 또는 값

Keyword Arguments:

  • out(Tensor, optional) - 반환 텐서

Returns:

  • input과 output이 같으면 False, 다르면 True를 반환한다.

 

Example:

>>> import torch
>>> torch.ne(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
tensor([[False, True], [True, False]])

torch.eq

 

참고링크 : https://pytorch.org/docs/stable/generated/torch.ne.html

 

import torch

torch.eq(input, other,*, out=None)

# tensor을 반환한다.

 

Parameter:

  • input(tensor) - 비교할 텐서
  • other(Tensor or float) - 비교할 텐서 또는 값

Keyword Arguments:

  • out(Tensor, optional) - 반환 텐서

Returns:

  • input과 output이 같으면 True, 다르면 False를 반환한다.

 

왜 사용하는가

저 같은 경우는 Sequen 모델에서 가변 길이 Sequence를 처리할 때 사용을 했습니다.

torch.ne -> Mask Index인 경우 같으면 False로 바꾸어 유효한 데이터만 True로 반환되도록 함수를 사용했습니다.

torch.eq -> 유효한 데이터 중에서 값이 똑같은 Index들은 정답을 맞췄기에 True로 반환되도록 하여 정확도 계산에 사용되도록 했습니다.

두 함수는 필요없는 데이터와 필요있는 데이터를 구분하고 필요한 데이터 중에서 올바른 데이터만 계산하는 accuracy를 구할 때 사용했습니다.

728x90