-
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.
- Loading branch information
Showing
8 changed files
with
174 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
module Basis | ||
|
||
using ..Boltz: _unsqueeze1 | ||
using ConcreteStructs: @concrete | ||
using Markdown: @doc_str | ||
|
||
@concrete struct GeneralBasisFunction{name} | ||
f | ||
n::Int | ||
end | ||
|
||
function Base.show(io::IO, basis::GeneralBasisFunction{name}) where {name} | ||
print(io, "Basis.$(name)(order=$(basis.n))") | ||
end | ||
|
||
function (basis::GeneralBasisFunction{name, F})(x::AbstractArray) where {name, F} | ||
return basis.f.(1:(basis.n), _unsqueeze1(x)) | ||
end | ||
|
||
@doc doc""" | ||
Chebyshev(n) | ||
Constructs a Chebyshev basis of the form $[T_{0}(x), T_{1}(x), \dots, T_{n-1}(x)]$ where | ||
$T_j(.)$ is the $j^{th}$ Chebyshev polynomial of the first kind. | ||
## Arguments | ||
- `n`: number of terms in the polynomial expansion. | ||
""" | ||
Chebyshev(n) = GeneralBasisFunction{:Chebyshev}(__chebyshev, n) | ||
|
||
@inline __chebyshev(i, x) = @fastmath cos(i * acos(x)) | ||
|
||
@doc doc""" | ||
Sin(n) | ||
Constructs a sine basis of the form $[\sin(x), \sin(2x), \dots, \sin(nx)]$. | ||
## Arguments | ||
- `n`: number of terms in the sine expansion. | ||
""" | ||
Sin(n) = GeneralBasisFunction{:Sin}(@fastmath(sin∘*), n) | ||
|
||
@doc doc""" | ||
Cos(n) | ||
Constructs a cosine basis of the form $[\cos(x), \cos(2x), \dots, \cos(nx)]$. | ||
## Arguments | ||
- `n`: number of terms in the cosine expansion. | ||
""" | ||
Cos(n) = GeneralBasisFunction{:Cos}(@fastmath(cos∘*), n) | ||
|
||
@doc doc""" | ||
Fourier(n) | ||
Constructs a Fourier basis of the form | ||
$F_j(x) = j is even ? cos((j÷2)x) : sin((j÷2)x)$ => $[F_0(x), F_1(x), \dots, F_n(x)]$. | ||
## Arguments | ||
- `n`: number of terms in the Fourier expansion. | ||
""" | ||
Fourier(n) = GeneralBasisFunction{:Fourier}(__fourier, n) | ||
|
||
@inline function __fourier(i, x) | ||
s, c = @fastmath sincos(i * x / 2) | ||
return ifelse(iseven(i), c, s) | ||
end | ||
|
||
@doc doc""" | ||
Legendre(n) | ||
Constructs a Legendre basis of the form $[P_{0}(x), P_{1}(x), \dots, P_{n-1}(x)]$ where | ||
$P_j(.)$ is the $j^{th}$ Legendre polynomial. | ||
## Arguments | ||
- `n`: number of terms in the polynomial expansion. | ||
""" | ||
Legendre(n) = GeneralBasisFunction{:Legendre}(__legendre_poly, n) | ||
|
||
## Source: https://github.com/ranocha/PolynomialBases.jl/blob/master/src/legendre.jl | ||
@inline function __legendre_poly(i, x) | ||
p = i - 1 | ||
a = one(x) | ||
b = x | ||
|
||
p ≤ 0 && return a | ||
p == 1 && return b | ||
|
||
for j in 2:p | ||
a, b = b, @fastmath(((2j - 1) * x * b - (j - 1) * a)/j) | ||
end | ||
|
||
return b | ||
end | ||
|
||
@doc doc""" | ||
Polynomial(n) | ||
Constructs a Polynomial basis of the form $[1, x, \dots, x^(n-1)]$. | ||
## Arguments | ||
- `n`: number of terms in the polynomial expansion. | ||
""" | ||
Polynomial(n) = GeneralBasisFunction{:Polynomial}(__polynomial, n) | ||
|
||
@inline __polynomial(i, x) = x^(i - 1) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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,40 @@ | ||
@doc doc""" | ||
TensorProductLayer(model, out_dim::Int; init_weight = randn32) | ||
Constructs the Tensor Product Layer, which takes as input an array of n tensor product | ||
basis, $[B_1, B_2, \dots, B_n]$ a data point x, computes | ||
$$z_i = W_{i, :} ⨀ [B_1(x_1) ⨂ B_2(x_2) ⨂ \dots ⨂ B_n(x_n)]$$ | ||
where $W$ is the layer's weight, and returns $[z_1, \dots, z_{out}]$. | ||
## Arguments | ||
- `basis_fns`: Array of TensorProductBasis $[B_1(n_1), \dots, B_k(n_k)]$, where $k$ | ||
corresponds to the dimension of the input. | ||
- `out_dim`: Dimension of the output. | ||
- `init_weight`: Initializer for the weight matrix. Defaults to `randn32`. | ||
""" | ||
function TensorProductLayer(basis_fns, out_dim::Int; init_weight::F=randn32) where {F} | ||
dense = Lux.Dense( | ||
prod(Base.Fix2(getproperty, :n), basis_fns) => out_dim; use_bias=false, init_weight) | ||
return Lux.@compact(; basis_fns=Tuple(basis_fns), dense, | ||
out_dim, dispatch=:TensorProductLayer) do x::AbstractArray # I1 x I2 x ... x T x B | ||
x_ = Lux._eachslice(x, Val(ndims(x) - 1)) # [I1 x I2 x ... x B] x T | ||
@argcheck length(x_) == length(basis_fns) | ||
|
||
y_ = mapfoldl(_kron, zip(basis_fns, x_)) do (fn, xᵢ) | ||
eachcol(reshape(fn(xᵢ), :, prod(size(xᵢ)))) | ||
end # [[D₁ x ... x Dₙ] x (I1 x I2 x ... x B)] | ||
|
||
@return reshape(dense(stack(y_)), size(x)[1:(end - 2)]..., out_dim, size(x)[end]) | ||
end | ||
end | ||
|
||
# CUDA `kron` exists only for `CuMatrix` so we define `kron` directly by converting to | ||
# a matrix | ||
@noinline _kron(a, b) = map(__kron, a, b) | ||
@noinline function __kron(a, b) | ||
@show size(a), size(b) | ||
return vec(kron(reshape(a, :, 1), reshape(b, 1, :))) | ||
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