-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: add PositiveDefinite
#89
Draft
nicholaskl97
wants to merge
11
commits into
LuxDL:main
Choose a base branch
from
nicholaskl97:positive-definite-container
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−1
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d275ee2
feat: add `PositiveDefinite` and corresponding tests
nicholaskl97 663e5e9
Added `NNlib` import to Positive Definite Container test
nicholaskl97 dde544c
Including and exporting `PositiveDefinite`
nicholaskl97 1ba4321
Fixed `PositiveDefinite` inner constructors
nicholaskl97 46ffc9e
Fixed incorrect function call in `PositiveDefinite` test
nicholaskl97 536eaea
Updated `PositiveDefinite` to account for possibly changing state of …
nicholaskl97 1dd9854
Replaced call to `mapslices` in `PositiveDefinite` with `mapreduce(..…
nicholaskl97 6ba2a24
Fixed broken call to `mapreduce` in `PositiveDefinite`
nicholaskl97 ebf0efe
Fixed typo in `PositiveDefinite` test and removed `==` from test in f…
nicholaskl97 8441c0a
Removed unnecessary definition of `norm2` from `PositiveDefinite`
nicholaskl97 4322db8
Added `ShiftTo` container
nicholaskl97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
""" | ||
PositiveDefinite(model, x0; ψ, r) | ||
PositiveDefinite(model; in_dims, ψ, r) | ||
|
||
Constructs a Lyapunov-Net [gaby2022lyapunovnetdeepneuralnetwork](@citep), which is positive | ||
definite about `x0` whenever `ψ` and `r` meet certain conditions described below. | ||
|
||
For a model `ϕ`, | ||
`PositiveDefinite(ϕ, ψ, r, x0)(x, ps, st) = ψ(ϕ(x, ps, st) - ϕ(x0, ps, st)) + r(x, x0)`. | ||
This results in a model which maps `x0` to `0` and any other input to a positive number | ||
(i.e., a model which is positive definite about `x0`) whenever `ψ` is positive definite | ||
about zero and `r` returns a positive number for any non-equal inputs and zero for equal | ||
inputs. | ||
|
||
## Arguments | ||
- `model`: the underlying model being transformed into a positive definite function | ||
- `x0`: The unique input that will be mapped to zero instead of a positive number | ||
|
||
## Keyword Arguments | ||
- `in_dims`: the number of input dimensions if `x0` is not provided; uses | ||
`x0 = zeros(in_dims)` | ||
- `ψ`: a positive definite function (about zero); defaults to ``ψ(x) = ||x||^2`` | ||
- `r`: a bivariate function such that `r(x0, x0) = 0` and | ||
`r(x, x0) > 0` whenever `x ≠ x0`; defaults to ``r(x, y) = ||x - y||^2`` | ||
|
||
## Inputs | ||
- `x`: will be passed directly into `model`, so must meet the input requirements of that | ||
argument | ||
|
||
## Returns | ||
- The output of the positive definite model | ||
- The state of the positive definite model. If the underlying model changes it state, the | ||
state will be updated according to the call with the input `x`, not with the call using | ||
`x0`. | ||
|
||
## States | ||
- `st`: a `NamedTuple` containing the state of the underlying `model` and the `x0` value | ||
|
||
## Parameters | ||
- Same as the underlying `model` | ||
""" | ||
@concrete struct PositiveDefinite <: AbstractLuxWrapperLayer{:model} | ||
model <: AbstractLuxLayer | ||
x0 <: AbstractVector | ||
ψ <: Function | ||
r <: Function | ||
|
||
function PositiveDefinite(model, x0::AbstractVector; ψ = Base.Fix1(sum, abs2), | ||
r = Base.Fix1(sum, abs2) ∘ -) | ||
return PositiveDefinite(model, x0, ψ, r) | ||
end | ||
function PositiveDefinite(model; in_dims::Integer, ψ = Base.Fix1(sum, abs2), | ||
r = Base.Fix1(sum, abs2) ∘ -) | ||
return PositiveDefinite(model, zeros(in_dims), ψ, r) | ||
end | ||
end | ||
|
||
function LuxCore.initialstates(rng::AbstractRNG, pd::PositiveDefinite) | ||
return (; model=LuxCore.initialstates(rng, pd.model), x0=pd.x0) | ||
end | ||
|
||
function (pd::PositiveDefinite)(x::AbstractVector, ps, st) | ||
out, new_st = pd(reshape(x, :, 1), ps, st) | ||
return vec(out), new_st | ||
end | ||
|
||
function (pd::PositiveDefinite)(x::AbstractMatrix, ps, st) | ||
ϕ0, _ = pd.model(st.x0, ps, st.model) | ||
ϕx, new_model_st = pd.model(x, ps, st.model) | ||
return ( | ||
mapreduce(hcat, zip(eachcol(x), eachcol(ϕx))) do (x, ϕx) | ||
pd.ψ(ϕx - ϕ0) + pd.r(x, st.x0) | ||
end, | ||
merge(st, (; model = new_model_st)) | ||
) | ||
end | ||
|
||
""" | ||
ShiftTo(model, in_val, out_val) | ||
|
||
Vertically shifts the output of `model` to otuput `out_val` when the input is `in_val`. | ||
|
||
For a model `ϕ`, `ShiftTo(ϕ, in_val, out_val)(x, ps, st) = ϕ(x, ps, st) + Δϕ`, | ||
where `Δϕ = out_val - ϕ(in_val, ps, st)`. | ||
|
||
## Arguments | ||
- `model`: the underlying model being transformed into a positive definite function | ||
- `in_val`: The input that will be mapped to `out_val` | ||
- `out_val`: The value that the output will be shifted to when the input is `in_val` | ||
|
||
## Inputs | ||
- `x`: will be passed directly into `model`, so must meet the input requirements of that | ||
argument | ||
|
||
## Returns | ||
- The output of the shifted model | ||
- The state of the shifted model. If the underlying model changes it state, the | ||
state will be updated according to the call with the input `x`, not the call using | ||
`in_val`. | ||
|
||
## States | ||
- `st`: a `NamedTuple` containing the state of the underlying `model` and the `in_val` and | ||
`out_val` values | ||
|
||
## Parameters | ||
- Same as the underlying `model` | ||
""" | ||
@concrete struct ShiftTo <: AbstractLuxWrapperLayer{:model} | ||
model <: AbstractLuxLayer | ||
in_val <: AbstractVector | ||
out_val <: AbstractVector | ||
Comment on lines
+110
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
end | ||
|
||
function LuxCore.initialstates(rng::AbstractRNG, s::ShiftTo) | ||
return (; | ||
model=LuxCore.initialstates(rng, s.model), | ||
in_val=s.in_val, | ||
out_val=s.out_val | ||
) | ||
end | ||
|
||
function (s::ShiftTo)(x::AbstractVector, ps, st) | ||
out, new_st = s(reshape(x, :, 1), ps, st) | ||
return vec(out), new_st | ||
end | ||
|
||
function (s::ShiftTo)(x::AbstractMatrix, ps, st) | ||
ϕ0, _ = s.model(st.in_val, ps, st.model) | ||
Δϕ = st.out_val .- ϕ0 | ||
ϕx, new_model_st = s.model(x, ps, st.model) | ||
return ( | ||
ϕx .+ Δϕ, | ||
merge(st, (; model = new_model_st)) | ||
) | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't store a vector here. Instead pass in a initialization_function (ideally from WeightInitializers.jl) and construct the vector inside
initialstates