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

Apply isapprox elementwise #185

Merged
merged 3 commits into from
Jan 26, 2024
Merged

Apply isapprox elementwise #185

merged 3 commits into from
Jan 26, 2024

Conversation

thomasfaingnaert
Copy link
Member

Applying isapprox elementwise rather than on the entire matrix catches incorrect output better, since by default, isapprox on matrices uses the Frobenius ($L_2$) norm, $$||M|| = \sqrt{\sum_{i=1}^N \sum_{j=1}^N |a_{ij} |^2}$$, which scales with the matrix size, so larger matrices tolerate larger errors with a relative tolerance:

julia> A = rand(Float32, (4, 4));

julia> B = copy(A);

julia> isapprox(A, B)
true

julia> B[1] *= 1.01;

julia> isapprox(A, B)
false

julia> A = rand(Float32, (2048, 2048));

julia> B = copy(A);

julia> isapprox(A, B)
true

julia> B[1] *= 1.01;

julia> isapprox(A, B)
true

We could use absolute tolerance, or switch to the $L_\infty$-norm instead $$||M|| = \max_{i,j} |a_{ij}|$$

by passing a custom norm function to isapprox, which does handle inaccuracies better:

julia> using LinearAlgebra

julia> A = rand(Float32, (2048, 2048));

julia> B = copy(A);

julia> isapprox(A, B)
true

julia> isapprox(A, B; norm=M->LinearAlgebra.norm(M, Inf))
true

julia> B[1] *= 1.01;

julia> isapprox(A, B)
true

julia> isapprox(A, B; norm=M->LinearAlgebra.norm(M, Inf))
false

But let's just go for the easiest approach and apply isapprox elementwise instead.

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

Attention: 53 lines in your changes are missing coverage. Please review.

Comparison is base (3052b52) 34.94% compared to head (3d45196) 32.04%.
Report is 5 commits behind head on master.

Files Patch % Lines
src/operator.jl 6.25% 30 Missing ⚠️
src/config.jl 8.00% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #185      +/-   ##
==========================================
- Coverage   34.94%   32.04%   -2.90%     
==========================================
  Files          11       11              
  Lines         933      958      +25     
==========================================
- Hits          326      307      -19     
- Misses        607      651      +44     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@thomasfaingnaert thomasfaingnaert merged commit ef7f0a1 into master Jan 26, 2024
4 checks passed
@thomasfaingnaert thomasfaingnaert deleted the tf/change-approx branch January 26, 2024 15:42
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.

1 participant