-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# This file is a part of AdaptiveFlows.jl, licensed under the MIT License (MIT). | ||
|
||
struct ScaleShiftModule <: AbstractFlowModule | ||
A::Matrix{Real} | ||
b::Vector{Real} | ||
end | ||
|
||
export ScaleShiftModule | ||
@functor ScaleShiftModule | ||
|
||
function ScaleShiftModule(stds::AbstractVector, means::AbstractVector) | ||
A = Diagonal(inv.(stds)) | ||
return ScaleShiftModule(A, .- A * means) | ||
end | ||
|
||
function ScaleShiftModule(x::AbstractArray) | ||
stds = vec(std(x, dims = 2)) | ||
means = vec(mean(x, dims = 2)) | ||
ScaleShiftModule(stds, means) | ||
end | ||
|
||
function ChangesOfVariables.with_logabsdet_jacobian(f::ScaleShiftModule, x::Any) | ||
y, ladj = ChangesOfVariables.with_logabsdet_jacobian(MulAdd(f.A, f.b), x) | ||
|
||
return y, fill(ladj, 1, size(y,2)) | ||
end | ||
|
||
(f::ScaleShiftModule)(x::AbstractMatrix) = MulAdd(f.A, f.b)(x) | ||
(f::ScaleShiftModule)(vs::AbstractValueShape) = vs | ||
|
||
function InverseFunctions.inverse(f::ScaleShiftModule) | ||
A = inv(f.A) | ||
return ScaleShiftModule(A, .- A * f.b) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# This file is a part of AdaptiveFlows.jl, licensed under the MIT License (MIT). | ||
|
||
using AdaptiveFlows | ||
using Test | ||
|
||
using ArraysOfArrays | ||
using InverseFunctions | ||
using LinearAlgebra | ||
using Random | ||
using Statistics | ||
using ValueShapes | ||
|
||
# test inputs | ||
n_dims = 4 | ||
n_smpls = 10 | ||
|
||
rng = MersenneTwister(1234) | ||
x = muladd(Diagonal(randn(rng, n_dims)), randn(rng, n_dims, n_smpls), randn(rng, n_dims)) | ||
|
||
smpls = nestedview(x) | ||
vs_test = valshape(x) | ||
|
||
scale_shift_test = ScaleShiftModule(x) | ||
inv_scale_shift_test = inverse(scale_shift_test) | ||
|
||
y_test = scale_shift_test(x) | ||
x_inverted_test = inv_scale_shift_test(y_test) | ||
|
||
stds_test = vec(std(y_test, dims = 2)) | ||
means_test = vec(mean(y_test, dims = 2)) | ||
|
||
@testset "ScaleShiftModule" begin | ||
@test all(isapprox.(stds_test, 1)) && all(isapprox.(means_test, 0, atol = 1f-15)) | ||
@test all(isapprox.(x_inverted_test, x)) | ||
|
||
@test scale_shift_test(vs_test) == vs_test | ||
@test all(isapprox.(with_logabsdet_jacobian(scale_shift_test, x)[2], 10.637223371435223)) | ||
end |