-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla_plrnn.jl
199 lines (174 loc) · 5.45 KB
/
vanilla_plrnn.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using Flux: @functor
using Statistics: mean
using ..Datasets
using ..ObservationModels: ObservationModel, init_state
# abstract type
abstract type AbstractPLRNN end
(m::AbstractPLRNN)(z::AbstractVecOrMat) = step(m, z)
(m::AbstractPLRNN)(z::AbstractVecOrMat, s::AbstractVecOrMat) = step(m, z, s)
step(m::AbstractPLRNN, z::AbstractVecOrMat, s::AbstractVecOrMat) = step(m, z) + m.C * s
jacobian(m::AbstractPLRNN, z::AbstractVector) = Flux.jacobian(z -> m(z), z)[1]
jacobian(m::AbstractPLRNN, z::AbstractMatrix) = jacobian.([m], eachcol(z))
abstract type AbstractVanillaPLRNN <: AbstractPLRNN end
mutable struct PLRNN{V <: AbstractVector, M <: AbstractMatrix} <: AbstractVanillaPLRNN
A::V
W::M
h::V
C::Maybe{M}
end
@functor PLRNN
# initialization/constructor
PLRNN(M::Int) = PLRNN(initialize_A_W_h(M)..., nothing)
PLRNN(M::Int, K::Int) = PLRNN(initialize_A_W_h(M)..., Flux.glorot_uniform(M, K))
"""
step(model, z)
Evolve `z` in time for one step according to the model `m` (equation).
`z` is either a `M` dimensional column vector or a `M x S` matrix, where `S` is
the batch dimension.
"""
step(m::PLRNN, z::AbstractVecOrMat) = m.A .* z .+ m.W * relu.(z) .+ m.h
jacobian(m::PLRNN, z::AbstractVector) = Diagonal(m.A) + m.W * Diagonal(z .> 0)
# mean-centered PLRNN
mutable struct mcPLRNN{V <: AbstractVector, M <: AbstractMatrix} <: AbstractVanillaPLRNN
A::V
W::M
h::V
C::Maybe{M}
end
@functor mcPLRNN
# initialization/constructor
mcPLRNN(M::Int) = mcPLRNN(initialize_A_W_h(M)..., nothing)
mcPLRNN(M::Int, K::Int) = mcPLRNN(initialize_A_W_h(M)..., Flux.glorot_uniform(M, K))
mean_center(z::AbstractVecOrMat) = z .- mean(z, dims = 1)
step(m::mcPLRNN, z::AbstractVecOrMat) = m.A .* z .+ m.W * relu.(mean_center(z)) .+ m.h
function jacobian(m::mcPLRNN, z::AbstractVector)
M, type = length(z), eltype(z)
ℳ = type(1 / M) * (M * I - ones(type, M, M))
return Diagonal(m.A) + m.W * Diagonal(ℳ * z .> 0) * ℳ
end
### TODO: Move generate methods to a separate file
### TODO: Adopt N x T data convention (should actually be faster in Julia -> column major memory layout)
@inbounds """
generate(model, z₁, T)
Generate a trajectory of length `T` using PLRNN model `m` given initial condition `z₁`.
"""
function generate(m::AbstractPLRNN, z₁::AbstractVector, T::Int)
# trajectory placeholder
Z = similar(z₁, T, length(z₁))
# initial condition for model
@views Z[1, :] .= z₁
# evolve initial condition in time
@views for t = 2:T
Z[t, :] .= m(Z[t-1, :])
end
return Z
end
@inbounds function generate(m::AbstractPLRNN, z₁::AbstractVector, S::AbstractMatrix, T::Int)
# trajectory placeholder
Z = similar(z₁, T, length(z₁))
# initial condition for model
@views Z[1, :] .= z₁
# evolve initial condition in time
@views for t = 2:T
Z[t, :] .= m(Z[t-1, :], S[t, :])
end
return Z
end
"""
generate(model, observation_model, x₁,[S,] T)
Generate a trajectory of length `T` using PLRNN model `m` given initial condition `x₁`.
"""
function generate(d::Dataset, m::AbstractPLRNN, obs::ObservationModel, x₁::AbstractVector, T::Int)
z₁ = init_state(obs, x₁)
ts = generate(m, z₁, T)
return permutedims(obs(ts'), (2, 1))
end
function generate(
d::ExternalInputsDataset,
m::AbstractPLRNN,
obs::ObservationModel,
x₁::AbstractVector,
S::AbstractMatrix,
T::Int,
)
z₁ = init_state(obs, x₁)
ts = generate(m, z₁, S, T)
return permutedims(obs(ts'), (2, 1))
end
function generate(
d::NuisanceArtifactsDataset,
m::AbstractPLRNN,
obs::ObservationModel,
x₁::AbstractVector,
R::AbstractMatrix,
T::Int,
)
z₁ = init_state(obs, x₁, R[1,:])
ts = generate(m, z₁, T)
return permutedims(obs(ts', R'), (2, 1))
end
function generate(
d::ExternalInputsNuisanceArtifactsDataset,
m::AbstractPLRNN,
obs::ObservationModel,
x₁::AbstractVector,
S::AbstractMatrix,
R::AbstractMatrix,
T::Int,
)
z₁ = init_state(obs, x₁, R[1,:])
ts = generate(m, z₁, S, T)
return permutedims(obs(ts', R'), (2, 1))
end
"""
generate(model, observation_model, x₁,[S,][R,] T)
for the convolutional datasets
"""
function generate(d::DatasetConv, m::AbstractPLRNN, obs::ObservationModel, x₁::AbstractVector, T::Int)
z₁ = init_state(obs, x₁)
ts = generate(m, z₁, T)
ts_conv = hrf_conv(ts, d.hrf)
return permutedims(obs(ts_conv'), (2, 1))
end
function generate(
d::ExternalInputsDatasetConv,
m::AbstractPLRNN,
obs::ObservationModel,
x₁::AbstractVector,
S::AbstractMatrix,
T::Int,
)
z₁ = init_state(obs, x₁)
ts = generate(m, z₁, S, T)
ts_conv = hrf_conv(ts, d.hrf)
return permutedims(obs(ts_conv'), (2, 1))
end
function generate(
d::NuisanceArtifactsDatasetConv,
m::AbstractPLRNN,
obs::ObservationModel,
x₁::AbstractVector,
r₁::AbstractVector,
R::AbstractMatrix,
T::Int,
)
z₁ = init_state(obs, x₁, r₁)
ts = generate(m, z₁, T)
ts_conv = hrf_conv(ts, d.hrf)
return permutedims(obs(ts_conv', R'), (2, 1))
end
function generate(
d::ExternalInputsNuisanceArtifactsDatasetConv,
m::AbstractPLRNN,
obs::ObservationModel,
x₁::AbstractVector,
r₁::AbstractVector,
S::AbstractMatrix,
R::AbstractMatrix,
T::Int,
)
z₁ = init_state(obs, x₁, r₁)
ts = generate(m, z₁, S, T)
ts_conv = hrf_conv(ts, d.hrf)
return permutedims(obs(ts_conv', R'), (2, 1))
end