Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with tl.clip for the PyTorch and TensorFlow backends #355

Merged
merged 5 commits into from Feb 11, 2022

Conversation

MarieRoald
Copy link
Contributor

For both PyTorch and TensorFlow, the values for a_max and a_min are computed by TensorLy whenever their values are None. However, the way these values are computed leads to erroneous behaviour. With PyTorch, this occurs when a_min > max(tensor) and a_max=None. I noticed this for PyTorch when I tried to clip a negative tensor to be non-negative. Here is an example:

tl.set_backend("pytorch")
x = tl.tensor([-1, -1, -1])
tl.clip(x, 0, None)
tensor([-1., -1., -1.])

For TensorFlow, the error occurs when a_min=None and a_max<min(tensor). Here is an example:

tl.set_backend("tensorflow")
x = tl.tensor([1, 1, 1])
tl.clip(x, None, 0)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>

@codecov
Copy link

codecov bot commented Jan 8, 2022

Codecov Report

Merging #355 (c4e2615) into main (87b435b) will increase coverage by 0.03%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #355      +/-   ##
==========================================
+ Coverage   88.16%   88.19%   +0.03%     
==========================================
  Files         105      105              
  Lines        6058     6058              
==========================================
+ Hits         5341     5343       +2     
+ Misses        717      715       -2     
Impacted Files Coverage Δ
tensorly/decomposition/_cp.py 87.59% <0.00%> (-0.37%) ⬇️
tensorly/tenalg/proximal.py 67.38% <0.00%> (+0.23%) ⬆️
tensorly/tenalg/tests/test_proximal.py 100.00% <0.00%> (+0.70%) ⬆️
tensorly/decomposition/_cmtf_als.py 92.59% <0.00%> (+3.70%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 87b435b...c4e2615. Read the comment docs.

@JeanKossaifi
Copy link
Member

Thanks @MarieRoald, I always love your PRs: clear problem, clear fix and matching unit-tests! 🌠
Merging.

@JeanKossaifi JeanKossaifi merged commit 4f9b501 into tensorly:main Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants