-
Notifications
You must be signed in to change notification settings - Fork 11
Support various dimensions in CosineSimilarity #157
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
제목이 52자가 넘는데 제목을 좀 더 명료하게 작성해주세요. 표현하고 싶은 내용은 본문으로 옮기면 됩니다.
tests/test_cosine_similarity.py
Outdated
factory_kwargs = {"device": device} | ||
x_dims = [(z_size, y_size, x_size), (y_size, x_size), (x_size,)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pytest.mark.parametrize
가 tuple
로 정의되는게 더 좋을 것 같아요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아래와 같은 형태로 수정하였습니다.
@pytest.mark.parametrize(
"x_dim",
[(1431, 500, 200), (21, 6400), (86,)],
)
3d3ad00
to
3dd3b38
Compare
커밋/PR 제목을 Add input dimension of CosineSimiliarity로 수정하고 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰를 하기 전에 용어에 대한 정리를 해야할 것 같습니다.
dim
은 dimension의 약자로 차원을 나타내는 용어입니다. 그래서 tensor.dim()
을 하면 텐서가 가지고 있는 차원이 반환됩니다. 그리고 1차원, 2차원, 3차원을 표시할때도 dim
이나 axis
를 사용해서 표현합니다.
shape
은 텐서의 모양을 의미합니다. 예를 들어 3차원 텐서의 모양은 [16, 32, 64]
와 같습니다.
정리하자면 dim
은 텐서의 차원이나 텐서에서 특정 차원을 지칭하기 위해 사용되는 단어입니다. 그리고 shape
은 텐서의 모양을 표현하는 단어이며 dim
의 크기를 가집니다.
test_cosine_similarity.py
의 수정사항을 보면 x_dim
이란 변수 이름으로 (1431, 500, 200), (21, 6400), (86,)
가 정의되고 있습니다. x_dim
이름이 적절할까요? 아마 적절하지 않다고 생각할 것입니다. x_shape
이나 input_shape
이 더 적절하겠죠? x_dim
은 1, 2, 3과 같은 숫자가 들어와야합니다.
다시 한번 작성한 코드를 살펴보시고 수정해 보시겠어요?
3dd3b38
to
d34dbc6
Compare
|
d34dbc6
to
d7e13fb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금의 커밋 제목은 변경 사항을 제대로 전달하지 못하는 것 같습니다. Support various dimensions in CosineSimilarity
는 어떠세요?
tests/test_cosine_similarity.py
Outdated
torch.nn.functional.cosine_similarity(x1, x2, dim=dim), | ||
trident.function.cosine_similarity(x1, x2, dim=dim), | ||
) | ||
for dim in range(len(x_shape)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
모든 dim
을 테스트할 필요가 있나요? 테스트 할 dim
을 파라미터로 넘기면 더 좋을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
특정 dim
만 파라미터로 지정해서 테스트하도록 수정하였습니다.
d7e13fb
to
2e39eea
Compare
|
tests/test_cosine_similarity.py
Outdated
return (x_shape[0], x_shape[1]) | ||
|
||
output_shape = get_output_shape(x_shape, dim) | ||
grad_output = torch.randn(output_shape, **factory_kwargs) if output_shape != "scalar" else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grad_output이 None인 경우가 정상적인 경우인가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
backward()
에 스칼라를 넣으면 다음과 같은 오류가 발생하여 None
처리를 하였습니다.
TypeError: gradients can be either Tensors or None, but got str
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1크기의 텐서를 만들어서 넣어야할 것 같은데요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아래와 같이 1크기의 텐서가 들어가도록 수정하였습니다.
def get_output_shape(x_shape, dim):
if len(x_shape) == 1:
return ()
elif len(x_shape) == 2:
return x_shape[1] if dim == 0 else x_shape[0]
else:
if dim == 0:
return (x_shape[1], x_shape[2])
elif dim == 1:
return (x_shape[0], x_shape[2])
else:
return (x_shape[0], x_shape[1])
grad_output = torch.randn(get_output_shape(x_shape, dim), **factory_kwargs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
tests/test_cosine_similarity.py
Outdated
return (x_shape[0], x_shape[1]) | ||
|
||
output_shape = get_output_shape(x_shape, dim) | ||
grad_output = torch.randn(output_shape, **factory_kwargs) if output_shape != "scalar" else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1크기의 텐서를 만들어서 넣어야할 것 같은데요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
2e39eea
to
a0e0cfe
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
🙏 Describe the pull request
CosineSimiliarity에 3차원 보다 작은 입력을 지원합니다.
✅ Checklist