-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from JuliaGNI/bias_for_linear_layer
Bias for linear layer
- Loading branch information
Showing
4 changed files
with
34 additions
and
4 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,28 @@ | ||
@doc raw""" | ||
A *bias layer* that does nothing more than add a vector to the input. This is needed for *LA-SympNets*. | ||
""" | ||
struct BiasLayer{M, N} <: SympNetLayer{M, N} | ||
end | ||
|
||
function BiasLayer(M::Int) | ||
BiasLayer{M, M}() | ||
end | ||
|
||
function initialparameters(backend::Backend, ::Type{T}, ::BiasLayer{M, M}; rng::AbstractRNG = Random.default_rng(), init_bias = GlorotUniform()) where {M, T} | ||
q_part = KernelAbstractions.zeros(backend, T, M÷2) | ||
p_part = KernelAbstractions.zeros(backend, T, M÷2) | ||
init_bias(rng, q_part) | ||
init_bias(rng, p_part) | ||
return (q = q_part, p = p_part) | ||
end | ||
|
||
function parameterlength(::BiasLayer{M, M}) where M | ||
M | ||
end | ||
|
||
(::BiasLayer{M, M})(z::NT, ps::NT) where {M, AT<:AbstractVector, NT<:NamedTuple{(:q, :p), Tuple{AT, AT}}} = (q = z.q + ps.q, p = z.p + ps.p) | ||
(::BiasLayer{M, M})(z::NT1, ps::NT2) where {M, T, AT<:AbstractVector, BT<:Union{AbstractMatrix, AbstractArray{T, 3}}, NT1<:NamedTuple{(:q, :p), Tuple{AT, AT}}, NT2<:NamedTuple{(:q, :p), Tuple{BT, BT}}} = (q = z.q .+ ps.q, p = z.p .+ ps.p) | ||
|
||
function (d::BiasLayer{M, M})(z::AbstractArray, ps) where M | ||
apply_layer_to_nt_and_return_array(z, d, ps) | ||
end |