New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
nn.CosineSimilarity returns value larger than 1 #78064
Comments
cc @nikitaved |
We have made a patch for cosine similarity which, I guess, is going to get exposed in the upcoming release. Could you please try with a nightly release and/or the current master? In [12]: ll = ((t1 / t1.norm()) * (t2 / t2.norm())).sum()
In [13]: ll > 1
Out[13]: tensor(False)
I will try to come up with an update that uses |
Yeah, thanks for pointing out the problem. Do you have any suggestions to solve that or get rid of this? |
@nikitaved note that current cosine_similarity implementation breaks #18057 again, probably we should just do what scipy does, the link's in that issue |
@ngimel , I have mentioned that in my post. Note, that using |
@skyelves , if you do not expect to backprop through inputs with close to zero norms, you could try the code sample from my very first message, adapted to your case (i.e. |
Scipy produces correct cosine similarity on equal inputs, which is currently broken, so I wouldn't call it "worse". Also, on cuda |
@ngimel , the SciPy implementation is what is implemented in PyTorch 1.10. That one was failing for other inputs. I am just not sure we can guarantee stable behavior for all types of inputs. For example, the SciPy implementation will suffer from precision degradation for inputs with large norms. |
I understand (although 1.10 didn't implement scipy exactly, it was still using |
Oh, that is a good point. We can indeed scale inputs prior to norm computation, as this similarity is homogeneous in each argument. Maybe this will solve the issue, I will give it a shot. |
But still, maybe at the end of the day we should just clamp outs so that they are no bigger than one. Gradient computation is a different matter, so still having accurate forward matters (as that clamping would have to be in no autograd context, as it is now) |
Actually I need to backprop so as to train the NN. |
Hi, I am encountering the same issue and also need to backdrop. I wonder has this one been solved in any recent release, or is there a workaround so far? |
@subercui , not yet. I am submitting a fix now. |
馃悰 Describe the bug
nn.CosineSimilarity returns value larger than 1
When I was computing cosine similarity, it returned a tensor([1.0000]). However, it's larger than 1, which leads to the runtimeError of BCELoss.
To reproduce the bug
It produces the RuntimeError:
Versions
Collecting environment information...
PyTorch version: 1.10.2
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A
OS: macOS 11.6 (x86_64)
GCC version: Could not collect
Clang version: 12.0.0
CMake version: version 3.15.5
Libc version: N/A
Python version: 3.7.5 (default, Oct 25 2019, 10:52:18) [Clang 4.0.1 (tags/RELEASE_401/final)] (64-bit runtime)
Python platform: Darwin-20.6.0-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Versions of relevant libraries:
[pip3] numpy==1.21.5
[pip3] torch==1.10.2
[pip3] torch-cluster==1.6.0
[pip3] torch-geometric==2.0.4
[pip3] torch-scatter==2.0.9
[pip3] torch-sparse==0.6.13
[pip3] torch-spline-conv==1.2.1
[conda] blas 1.0 mkl
[conda] mkl 2019.0 pypi_0 pypi
[conda] mkl-service 2.3.0 py37h9ed2024_1
[conda] mkl_fft 1.3.0 py37h4a7008c_2
[conda] mkl_random 1.2.1 py37hb2f4e1b_2
[conda] numpy 1.17.3 pypi_0 pypi
[conda] numpy-base 1.21.5 py37h3b1a694_1
[conda] pyg 2.0.4 py37_torch_1.10.0_cpu pyg
[conda] pytorch 1.10.2 cpu_py37h903acac_0
[conda] pytorch-cluster 1.6.0 py37_torch_1.10.0_cpu pyg
[conda] pytorch-scatter 2.0.9 py37_torch_1.10.0_cpu pyg
[conda] pytorch-sparse 0.6.13 py37_torch_1.10.0_cpu pyg
[conda] pytorch-spline-conv 1.2.1 py37_torch_1.10.0_cpu pyg
[conda] torch 1.4.0 pypi_0 pypi
[conda] torch-geometric 2.0.5 pypi_0 pypi
cc @albanD @mruberry @jbschlosser @walterddr @kshitij12345
The text was updated successfully, but these errors were encountered: