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

Approximate equality test #11

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Architecture/DenseLayerOp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ function Base.:(==)(L1::DenseLayerOp, L2::DenseLayerOp)
L1.activation == L2.activation
end

function Base.:isapprox(L1::DenseLayerOp, L2::DenseLayerOp; atol::Real=0,
rtol=nothing)
if dim_in(L1) != dim_in(L2) || dim_out(L1) != dim_out(L2)
return false
end
if isnothing(rtol)
if iszero(atol)
N = promote_type(eltype(L1.weights), eltype(L2.weights),
eltype(L1.bias), eltype(L2.bias))
rtol = Base.rtoldefault(N)
else
rtol = zero(atol)
end
end
return isapprox(L1.weights, L2.weights; atol=atol, rtol=rtol) &&
isapprox(L1.bias, L2.bias; atol=atol, rtol=rtol) &&
L1.activation == L2.activation
end

dim_in(L::DenseLayerOp) = size(L.weights, 2)

dim_out(L::DenseLayerOp) = length(L.bias)
8 changes: 8 additions & 0 deletions src/Architecture/FeedforwardNetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ function Base.:(==)(N1::FeedforwardNetwork, N2::FeedforwardNetwork)
return N1.layers == N2.layers
end

function Base.:isapprox(N1::FeedforwardNetwork, N2::FeedforwardNetwork;
atol::Real=0, rtol=nothing)
if length(N1.layers) != length(N2.layers)
return false
end
return all(isapprox.(N1.layers, N2.layers; atol=atol, rtol=rtol))
end

dim_in(N::FeedforwardNetwork) = dim_in(first(N.layers))

dim_out(N::FeedforwardNetwork) = dim_out(last(N.layers))
14 changes: 14 additions & 0 deletions test/Architecture/DenseLayerOp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ L = DenseLayerOp(W, b, Id())
@test L != DenseLayerOp(W .+ 1, b, Id()) &&
L != DenseLayerOp(W, b .+ 1, Id()) &&
L != DenseLayerOp(W, b, ReLU())
@test L != DenseLayerOp(hcat(1), [1], Id())

# approximate equality
@test L ≈ DenseLayerOp(W, b, Id())
@test L ≈ DenseLayerOp(W .+ 1e-10, b, Id()) &&
L ≈ DenseLayerOp(W, b .+ 1e-10, Id()) &&
!≈(L, DenseLayerOp(W .+ 1e-10, b, Id()); rtol=1e-12) &&
!≈(L, DenseLayerOp(W, b .+ 1e-10, Id()); rtol=1e-12) &&
≈(L, DenseLayerOp(W .+ 1e-1, b, Id()); atol=1) &&
≈(L, DenseLayerOp(W, b .+ 1e-1, Id()); atol=1) &&
!(L ≈ DenseLayerOp(W .+ 1, b, Id())) &&
!(L ≈ DenseLayerOp(W, b .+ 1, Id())) &&
!(L ≈ DenseLayerOp(W, b, ReLU()))
@test !(L ≈ DenseLayerOp(hcat(1), [1], Id()))

# dimensions
@test dim_in(L) == 2
Expand Down
5 changes: 5 additions & 0 deletions test/Architecture/FeedforwardNetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ N2 = FeedforwardNetwork([L1, L2])
@test N1 == FeedforwardNetwork([L1])
@test N1 != FeedforwardNetwork([L2])

# approximate equality
@test N1 ≈ FeedforwardNetwork([L1])
@test N1 ≈ FeedforwardNetwork([DenseLayerOp(W1 .+ 1e-10, b1, ReLU())])
@test !(N1 ≈ FeedforwardNetwork([L2]))

# dimensions
@test dim_in(N1) == 2 && dim_in(N2) == 2
@test dim_out(N1) == 3 && dim_out(N2) == 2
Expand Down