tvboptim 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tvboptim/__init__.py +9 -0
- tvboptim/data/__init__.py +12 -0
- tvboptim/data/loaders.py +99 -0
- tvboptim/execution.py +268 -0
- tvboptim/experimental/__init__.py +15 -0
- tvboptim/experimental/network_dynamics/__init__.py +28 -0
- tvboptim/experimental/network_dynamics/core/__init__.py +6 -0
- tvboptim/experimental/network_dynamics/core/bunch.py +67 -0
- tvboptim/experimental/network_dynamics/core/network.py +686 -0
- tvboptim/experimental/network_dynamics/coupling/__init__.py +22 -0
- tvboptim/experimental/network_dynamics/coupling/base.py +1005 -0
- tvboptim/experimental/network_dynamics/coupling/linear.py +466 -0
- tvboptim/experimental/network_dynamics/coupling/subspace.py +509 -0
- tvboptim/experimental/network_dynamics/coupling/tvb.py +202 -0
- tvboptim/experimental/network_dynamics/dynamics/__init__.py +7 -0
- tvboptim/experimental/network_dynamics/dynamics/base.py +541 -0
- tvboptim/experimental/network_dynamics/dynamics/lorenz.py +207 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/__init__.py +29 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/coombes_byrne.py +154 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/epileptor.py +200 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/generic_2d_oscillator.py +149 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/jansen_rit.py +158 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/kuramoto.py +120 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/larter_breakspear.py +221 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/linear.py +111 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/montbrio_pazo_roxin.py +137 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/sup_hopf.py +131 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/wilson_cowan.py +210 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang.py +124 -0
- tvboptim/experimental/network_dynamics/dynamics/tvb/wong_wang_exc_inh.py +177 -0
- tvboptim/experimental/network_dynamics/external_input/__init__.py +20 -0
- tvboptim/experimental/network_dynamics/external_input/base.py +152 -0
- tvboptim/experimental/network_dynamics/external_input/data.py +203 -0
- tvboptim/experimental/network_dynamics/external_input/parametric.py +270 -0
- tvboptim/experimental/network_dynamics/graph/__init__.py +14 -0
- tvboptim/experimental/network_dynamics/graph/base.py +706 -0
- tvboptim/experimental/network_dynamics/graph/sparse.py +885 -0
- tvboptim/experimental/network_dynamics/graph/utils.py +0 -0
- tvboptim/experimental/network_dynamics/noise/__init__.py +6 -0
- tvboptim/experimental/network_dynamics/noise/base.py +162 -0
- tvboptim/experimental/network_dynamics/noise/gaussian.py +71 -0
- tvboptim/experimental/network_dynamics/result.py +78 -0
- tvboptim/experimental/network_dynamics/solve.py +721 -0
- tvboptim/experimental/network_dynamics/solvers/__init__.py +15 -0
- tvboptim/experimental/network_dynamics/solvers/base.py +46 -0
- tvboptim/experimental/network_dynamics/solvers/diffrax.py +63 -0
- tvboptim/experimental/network_dynamics/solvers/native.py +308 -0
- tvboptim/experimental/network_dynamics/utils/__init__.py +3 -0
- tvboptim/experimental/network_dynamics/utils/printer.py +555 -0
- tvboptim/model.py +111 -0
- tvboptim/observations/__init__.py +3 -0
- tvboptim/observations/observation.py +32 -0
- tvboptim/observations/tvb_monitors/__init__.py +11 -0
- tvboptim/observations/tvb_monitors/bold.py +290 -0
- tvboptim/observations/tvb_monitors/downsampling.py +185 -0
- tvboptim/optim/__init__.py +4 -0
- tvboptim/optim/callbacks.py +333 -0
- tvboptim/optim/optax.py +379 -0
- tvboptim/types/__init__.py +35 -0
- tvboptim/types/parameter.py +769 -0
- tvboptim/types/spaces.py +943 -0
- tvboptim/types/stateutils.py +109 -0
- tvboptim/utils/__init__.py +16 -0
- tvboptim/utils/caching.py +82 -0
- tvboptim/utils/utils.py +265 -0
- tvboptim-0.2.0.dist-info/METADATA +157 -0
- tvboptim-0.2.0.dist-info/RECORD +70 -0
- tvboptim-0.2.0.dist-info/WHEEL +5 -0
- tvboptim-0.2.0.dist-info/licenses/LICENSE +278 -0
- tvboptim-0.2.0.dist-info/top_level.txt +1 -0
tvboptim/optim/optax.py
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import jax
|
|
2
|
+
import optax
|
|
3
|
+
|
|
4
|
+
from tvboptim.types.stateutils import combine_state, partition_state
|
|
5
|
+
|
|
6
|
+
# from tvb_fit.base.parameter import Parameters
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# from tvboptim.optim.callbacks import *
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class OptaxOptimizer:
|
|
13
|
+
"""
|
|
14
|
+
JAX-based parameter optimization using Optax optimizers with automatic differentiation.
|
|
15
|
+
|
|
16
|
+
OptaxOptimizer provides a high-level interface for optimizing model parameters
|
|
17
|
+
using any Optax optimizer (Adam, SGD, RMSprop, etc.). It automatically handles
|
|
18
|
+
parameter partitioning, gradient computation, and state management while
|
|
19
|
+
supporting both forward-mode and reverse-mode automatic differentiation.
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
loss : callable
|
|
24
|
+
Loss function to minimize. Should accept a state parameter and return
|
|
25
|
+
a scalar loss value. Signature: loss(state) -> scalar or (scalar, aux_data)
|
|
26
|
+
if has_aux=True.
|
|
27
|
+
optimizer : optax.GradientTransformation
|
|
28
|
+
Optax optimizer instance (e.g., optax.adam(0.001), optax.sgd(0.01)).
|
|
29
|
+
Defines the optimization algorithm and hyperparameters.
|
|
30
|
+
callback : callable, optional
|
|
31
|
+
Optional callback function called after each optimization step.
|
|
32
|
+
Signature: callback(step, diff_state, static_state, fitting_data, aux_data,
|
|
33
|
+
loss_value, grads) -> (stop_flag, new_diff_state, new_static_state).
|
|
34
|
+
Default is None, see the callbacks module for many useful callbacks.
|
|
35
|
+
has_aux : bool, optional
|
|
36
|
+
Whether the loss function returns auxiliary data along with the loss value.
|
|
37
|
+
If True, loss should return (loss_value, aux_data). Default is False.
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
Examples
|
|
41
|
+
--------
|
|
42
|
+
>>> import jax
|
|
43
|
+
>>> import jax.numpy as jnp
|
|
44
|
+
>>> import optax
|
|
45
|
+
>>> from tvboptim.types.parameter import Parameter
|
|
46
|
+
>>>
|
|
47
|
+
>>> # Define loss function
|
|
48
|
+
>>> def mse_loss(state):
|
|
49
|
+
... prediction = state['weight'] * state['input'] + state['bias']
|
|
50
|
+
... target = 2.5
|
|
51
|
+
... return jnp.mean((prediction - target) ** 2)
|
|
52
|
+
>>>
|
|
53
|
+
>>> # Define parameter state
|
|
54
|
+
>>> state = {
|
|
55
|
+
... 'weight': Parameter("weight", 1.0, free=True),
|
|
56
|
+
... 'bias': Parameter("bias", 0.0, free=True),
|
|
57
|
+
... 'input': 1.5 # Static parameter
|
|
58
|
+
... }
|
|
59
|
+
>>>
|
|
60
|
+
>>> # Create optimizer
|
|
61
|
+
>>> opt = OptaxOptimizer(
|
|
62
|
+
... loss=mse_loss,
|
|
63
|
+
... optimizer=optax.adam(learning_rate=0.01)
|
|
64
|
+
... )
|
|
65
|
+
>>>
|
|
66
|
+
>>> # Run optimization
|
|
67
|
+
>>> final_state, history = opt.run(state, max_steps=1000)
|
|
68
|
+
>>> print(f"Optimized weight: {final_state['weight']}")
|
|
69
|
+
>>>
|
|
70
|
+
>>> # With auxiliary data and callback
|
|
71
|
+
>>> def loss_with_aux(state):
|
|
72
|
+
... pred = state['weight'] * state['input'] + state['bias']
|
|
73
|
+
... loss = jnp.mean((pred - 2.5) ** 2)
|
|
74
|
+
... aux = {'prediction': pred, 'error': pred - 2.5}
|
|
75
|
+
... return loss, aux
|
|
76
|
+
>>>
|
|
77
|
+
>>> def monitor_callback(step, diff_state, static_state, fitting_data,
|
|
78
|
+
... aux_data, loss_value, grads):
|
|
79
|
+
... if step % 100 == 0:
|
|
80
|
+
... print(f"Step {step}: Loss = {loss_value}")
|
|
81
|
+
... # Early stopping condition
|
|
82
|
+
... stop = loss_value < 1e-6
|
|
83
|
+
... return stop, diff_state, static_state
|
|
84
|
+
>>>
|
|
85
|
+
>>> opt_aux = OptaxOptimizer(
|
|
86
|
+
... loss=loss_with_aux,
|
|
87
|
+
... optimizer=optax.adam(0.01),
|
|
88
|
+
... callback=monitor_callback,
|
|
89
|
+
... has_aux=True
|
|
90
|
+
... )
|
|
91
|
+
>>> final_state, history = opt_aux.run(state, max_steps=1000, mode="rev")
|
|
92
|
+
|
|
93
|
+
Notes
|
|
94
|
+
-----
|
|
95
|
+
**Parameter Partitioning:**
|
|
96
|
+
|
|
97
|
+
The optimizer automatically partitions the state into:
|
|
98
|
+
- **diff_state**: Parameters marked as free=True (optimized)
|
|
99
|
+
- **static_state**: Parameters marked as free=False (constant)
|
|
100
|
+
|
|
101
|
+
Only free parameters are optimized, while static parameters remain unchanged
|
|
102
|
+
throughout the optimization process.
|
|
103
|
+
|
|
104
|
+
**Differentiation Modes:**
|
|
105
|
+
|
|
106
|
+
- **"rev"** (default): Reverse-mode AD, efficient for many parameters
|
|
107
|
+
- **"fwd"**: Forward-mode AD, efficient for few parameters or when gradients
|
|
108
|
+
are needed w.r.t. many outputs
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def __init__(self, loss, optimizer, callback=None, has_aux=False):
|
|
112
|
+
self.loss = loss
|
|
113
|
+
self.optimizer = optimizer
|
|
114
|
+
self.callback = callback
|
|
115
|
+
self.has_aux = has_aux
|
|
116
|
+
|
|
117
|
+
def run(self, state, max_steps=1, mode="rev"):
|
|
118
|
+
"""
|
|
119
|
+
Execute parameter optimization for the specified number of steps.
|
|
120
|
+
|
|
121
|
+
Performs gradient-based optimization of free parameters in the state
|
|
122
|
+
using the configured Optax optimizer. Automatically handles parameter
|
|
123
|
+
partitioning, gradient computation, and state updates.
|
|
124
|
+
|
|
125
|
+
Parameters
|
|
126
|
+
----------
|
|
127
|
+
state : PyTree
|
|
128
|
+
Initial parameter state containing both free and static parameters.
|
|
129
|
+
Free parameters (marked with free=True) will be optimized.
|
|
130
|
+
max_steps : int, optional
|
|
131
|
+
Maximum number of optimization steps to perform. Default is 1.
|
|
132
|
+
mode : {"rev", "fwd"}, optional
|
|
133
|
+
Automatic differentiation mode. "rev" for reverse-mode (default),
|
|
134
|
+
"fwd" for forward-mode. Default is "rev".
|
|
135
|
+
|
|
136
|
+
Returns
|
|
137
|
+
-------
|
|
138
|
+
tuple
|
|
139
|
+
A tuple containing:
|
|
140
|
+
|
|
141
|
+
- **final_state** (PyTree): Optimized parameter state with
|
|
142
|
+
updated free parameters and unchanged static parameters.
|
|
143
|
+
- **fitting_data** (dict): Dictionary containing optimization history
|
|
144
|
+
and metadata collected during the optimization process.
|
|
145
|
+
|
|
146
|
+
Notes
|
|
147
|
+
-----
|
|
148
|
+
**Gradient Computation:**
|
|
149
|
+
|
|
150
|
+
The method automatically selects appropriate gradient computation based
|
|
151
|
+
on the mode parameter and loss function characteristics. Reverse-mode
|
|
152
|
+
is typically preferred for parameter optimization scenarios.
|
|
153
|
+
"""
|
|
154
|
+
diff_state, static_state = partition_state(state)
|
|
155
|
+
|
|
156
|
+
def __loss(diff_state, static_state):
|
|
157
|
+
state = combine_state(diff_state, static_state)
|
|
158
|
+
return self.loss(state)
|
|
159
|
+
# return (self.loss(state), (None, None))
|
|
160
|
+
|
|
161
|
+
# _loss = jax.jit(__loss, static_argnums=(1,))
|
|
162
|
+
_loss = jax.jit(__loss)
|
|
163
|
+
|
|
164
|
+
if mode == "rev":
|
|
165
|
+
|
|
166
|
+
def value_and_grad(loss, argnums=0, has_aux=False):
|
|
167
|
+
vgf = jax.value_and_grad(loss, argnums=argnums, has_aux=has_aux)
|
|
168
|
+
if has_aux:
|
|
169
|
+
return vgf
|
|
170
|
+
else:
|
|
171
|
+
|
|
172
|
+
def _fun(*args, **kwargs):
|
|
173
|
+
val, grads = vgf(*args, **kwargs)
|
|
174
|
+
return (val, None), grads
|
|
175
|
+
|
|
176
|
+
return _fun
|
|
177
|
+
|
|
178
|
+
elif mode == "fwd":
|
|
179
|
+
|
|
180
|
+
def value_and_grad(loss, argnums=0, has_aux=False):
|
|
181
|
+
return value_and_grad_fwd(loss, argnums=argnums, has_aux=has_aux)
|
|
182
|
+
|
|
183
|
+
else:
|
|
184
|
+
raise ValueError(f"Unknown mode: {mode}")
|
|
185
|
+
|
|
186
|
+
v_g_fun = value_and_grad(_loss, argnums=0, has_aux=self.has_aux)
|
|
187
|
+
# v_g_fun = jax.jit(value_and_grad(_loss, argnums=0, has_aux=self.has_aux), static_argnums=(1,))
|
|
188
|
+
|
|
189
|
+
def step(diff_state, static_state, opt_state):
|
|
190
|
+
# out = value_and_grad(_loss, argnums=0, has_aux=self.has_aux)(diff_state, static_state)
|
|
191
|
+
out = v_g_fun(diff_state, static_state)
|
|
192
|
+
(loss_value, aux_data), grads = out
|
|
193
|
+
|
|
194
|
+
# if self.has_aux:
|
|
195
|
+
# (loss_value, aux_data), grads = out
|
|
196
|
+
# else:
|
|
197
|
+
# loss_value, grads = out
|
|
198
|
+
# aux_data = None
|
|
199
|
+
def f(p):
|
|
200
|
+
return _loss(diff_state, static_state)
|
|
201
|
+
|
|
202
|
+
updates, opt_state = self.optimizer.update(
|
|
203
|
+
grads, opt_state, diff_state, value=loss_value, grad=grads, value_fn=f
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
diff_state = optax.apply_updates(diff_state, updates)
|
|
207
|
+
return diff_state, grads, opt_state, loss_value, aux_data
|
|
208
|
+
|
|
209
|
+
opt_state = self.optimizer.init(diff_state)
|
|
210
|
+
|
|
211
|
+
fitting_data = dict() # store data during fitting
|
|
212
|
+
for i in range(max_steps):
|
|
213
|
+
diff_state, grads, opt_state, loss_value, aux_data = step(
|
|
214
|
+
diff_state, static_state, opt_state
|
|
215
|
+
)
|
|
216
|
+
# print(format_pytree_as_string(diff_state, hide_none=True, name="FreeState", show_array_values=True))
|
|
217
|
+
# print(format_pytree_as_string(diff_state, hide_none=True, name="FreeState", show_array_values=True))
|
|
218
|
+
# fitting_data.append(diff_state)
|
|
219
|
+
if self.callback is not None:
|
|
220
|
+
stop, diff_state, static_state = self.callback(
|
|
221
|
+
i,
|
|
222
|
+
diff_state,
|
|
223
|
+
static_state,
|
|
224
|
+
fitting_data,
|
|
225
|
+
aux_data,
|
|
226
|
+
loss_value,
|
|
227
|
+
grads,
|
|
228
|
+
)
|
|
229
|
+
if stop:
|
|
230
|
+
print("Stopping due to callback")
|
|
231
|
+
break
|
|
232
|
+
return combine_state(diff_state, static_state), fitting_data
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def value_and_grad_fwd(fun, argnums=0, has_aux=False):
|
|
236
|
+
grad_fun = jax.jacfwd(fun, argnums=argnums, has_aux=has_aux)
|
|
237
|
+
if has_aux:
|
|
238
|
+
|
|
239
|
+
def _fun(*args, **kwargs):
|
|
240
|
+
val, _ = fun(*args, **kwargs)
|
|
241
|
+
grads, aux = grad_fun(*args, **kwargs)
|
|
242
|
+
return (val, aux), grads
|
|
243
|
+
|
|
244
|
+
else:
|
|
245
|
+
|
|
246
|
+
def _fun(*args, **kwargs):
|
|
247
|
+
val = fun(*args, **kwargs)
|
|
248
|
+
grads = grad_fun(*args, **kwargs)
|
|
249
|
+
return (val, None), grads
|
|
250
|
+
|
|
251
|
+
return _fun
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
# class Optimizer():
|
|
255
|
+
|
|
256
|
+
# def __init__(self,
|
|
257
|
+
# gm,
|
|
258
|
+
# observation = None,
|
|
259
|
+
# metric = None,
|
|
260
|
+
# callback = None,
|
|
261
|
+
# optimizer = None
|
|
262
|
+
# ):
|
|
263
|
+
# self.gm = gm
|
|
264
|
+
# self.observation = observation
|
|
265
|
+
# self.metric = metric
|
|
266
|
+
# self.callback = callback
|
|
267
|
+
# self.optimizer = optimizer
|
|
268
|
+
|
|
269
|
+
# def __call__(self, *args, **kwargs):
|
|
270
|
+
# return self.fit(*args, **kwargs)
|
|
271
|
+
|
|
272
|
+
# def validate(self):
|
|
273
|
+
# pred, params = self.gm.run()
|
|
274
|
+
# assert isinstance(self.metric(self.observation, pred, params, params), float), f"Metric must return a scalar float"
|
|
275
|
+
# return
|
|
276
|
+
|
|
277
|
+
# def fit(self):
|
|
278
|
+
# pass
|
|
279
|
+
|
|
280
|
+
# class OptaxOptimizer(Optimizer):
|
|
281
|
+
|
|
282
|
+
# def simulate(self,parameters, ics, metadata):
|
|
283
|
+
# result, ics_new = self.gm.kernel(self.gm.preprocess(parameters), ics, noise = self.gm.noise)
|
|
284
|
+
# prediction = self.gm.observation_model.operation(result, parameters, metadata = metadata)
|
|
285
|
+
# return prediction, ics_new
|
|
286
|
+
|
|
287
|
+
# def loss(self, parameters, ics, metadata):
|
|
288
|
+
# prediction, ics_new = self.simulate(parameters, ics, metadata)
|
|
289
|
+
# loss_value = self.metric(prediction, self.observation, parameters, self.gm.params)
|
|
290
|
+
# return (loss_value, (prediction, ics_new))
|
|
291
|
+
|
|
292
|
+
# def fit(self, iter_max = 1, mode = "rev", initial_parameters = None, initial_conditions = None, update_noise = False):
|
|
293
|
+
# """
|
|
294
|
+
# Fit the prediction of the generative model to the observation.
|
|
295
|
+
|
|
296
|
+
# **Arguments:**
|
|
297
|
+
|
|
298
|
+
# * *iter_max*: Maximum number of iterations
|
|
299
|
+
# * *mode*: 'rev' - reverse mode, 'fwd' - forward mode, 'lin' - linear mode
|
|
300
|
+
# * *initial_parameters*: Initial parameters for the optimization if None then the parameters are taken from the generative model.
|
|
301
|
+
# * *initial_conditions*: Initial conditions for the optimization if None then the initial conditions are taken from the generative model.
|
|
302
|
+
# """
|
|
303
|
+
# @jax.jit
|
|
304
|
+
# def simulate(parameters, ics, noise, metadata):
|
|
305
|
+
# result, ics_new = self.gm.kernel(self.gm.preprocess(parameters), ics, noise = noise)
|
|
306
|
+
# prediction = self.gm.observation_model.operation(result, parameters, metadata = metadata)
|
|
307
|
+
# return prediction, ics_new
|
|
308
|
+
|
|
309
|
+
# @jax.jit
|
|
310
|
+
# def loss(parameters, ics, noise, metadata):
|
|
311
|
+
# prediction, ics_new = simulate(parameters, ics, noise, metadata)
|
|
312
|
+
# loss_value = self.metric(prediction, self.observation, parameters, self.gm.params)
|
|
313
|
+
# return (loss_value, (prediction, ics_new))
|
|
314
|
+
|
|
315
|
+
# if mode == "rev":
|
|
316
|
+
# def value_and_grad(loss, params, argnums=0, has_aux=True):
|
|
317
|
+
# return jax.value_and_grad(loss, argnums=argnums, has_aux=has_aux)
|
|
318
|
+
# elif mode == "rev_state":
|
|
319
|
+
# def value_and_grad(loss, params, argnums=0, has_aux=True):
|
|
320
|
+
# return optax.value_and_grad_from_state(loss, argnums=argnums, has_aux=has_aux)
|
|
321
|
+
# elif mode == "fwd":
|
|
322
|
+
# def value_and_grad(loss, params, argnums=0, has_aux=True):
|
|
323
|
+
# return value_and_grad_fwd(loss, params, argnums=argnums, has_aux=has_aux)
|
|
324
|
+
# else:
|
|
325
|
+
# raise NotImplementedError(f"Mode {mode} not implemented. Must be 'rev', 'fwd', or 'lin'")
|
|
326
|
+
|
|
327
|
+
# def step(parameters, ics, noise, metadata, opt_state):
|
|
328
|
+
# out = value_and_grad(loss, parameters, argnums=0, has_aux=True)(parameters, ics, noise, metadata)
|
|
329
|
+
# (loss_value, (prediction, ics_new)), grads = out
|
|
330
|
+
# def f(p):
|
|
331
|
+
# return loss(p, ics, noise, metadata)[0]
|
|
332
|
+
# updates, opt_state = self.optimizer.update(grads, opt_state, parameters, value=loss_value, grad=grads, value_fn=f)
|
|
333
|
+
# # updates, opt_state = self.optimizer.update(grads, opt_state, parameters)
|
|
334
|
+
# parameters = optax.apply_updates(parameters, updates)
|
|
335
|
+
# return parameters, ics_new, opt_state, loss_value, prediction, grads
|
|
336
|
+
|
|
337
|
+
# if initial_parameters is None:
|
|
338
|
+
# parameters = self.gm.params
|
|
339
|
+
# else:
|
|
340
|
+
# parameters = initial_parameters
|
|
341
|
+
|
|
342
|
+
# if initial_conditions is None:
|
|
343
|
+
# ics = self.gm.initial_conditions
|
|
344
|
+
# else:
|
|
345
|
+
# ics = initial_conditions
|
|
346
|
+
|
|
347
|
+
# opt_state = self.optimizer.init(parameters)
|
|
348
|
+
# metadata = self.gm.observation_model.metadata
|
|
349
|
+
# noise = self.gm.noise
|
|
350
|
+
# fitting_data = dict() # a place to store data during fitting
|
|
351
|
+
# for i in range(iter_max):
|
|
352
|
+
# # ics need to be converted to tuple, otherwise recompilation will be triggered each iteration - alternative: unpack named tuple in state, history.
|
|
353
|
+
# parameters, ics_new, opt_state, loss_value, prediction, grads = step(parameters, tuple(ics), noise, metadata, opt_state)
|
|
354
|
+
# if update_noise:
|
|
355
|
+
# if hasattr(parameters, "nsig"):
|
|
356
|
+
# # print(f"Updating noise nsig = {parameters.nsig.value}")
|
|
357
|
+
# noise = self.gm.noise_generator(nsig = parameters.nsig.value)
|
|
358
|
+
# else:
|
|
359
|
+
# # print("Updating noise")
|
|
360
|
+
# noise = self.gm.noise_generator()
|
|
361
|
+
# # Ensure ICs stay finite
|
|
362
|
+
# if np.isfinite(ics_new[0]).all() and np.isfinite(ics_new[1]).all():
|
|
363
|
+
# ics = ics_new
|
|
364
|
+
# if self.callback is not None:
|
|
365
|
+
# stop, parameters, ics, metadata = self.callback(i, parameters, ics, metadata, fitting_data, self.gm, prediction, loss_value, grads)
|
|
366
|
+
# if stop:
|
|
367
|
+
# print("Stopping due to callback")
|
|
368
|
+
# break
|
|
369
|
+
|
|
370
|
+
# return parameters, ics, metadata, fitting_data
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
# def value_and_grad_fwd(fun, params, argnums=0, has_aux=False):
|
|
374
|
+
# grad_fun = jax.jacfwd(fun)
|
|
375
|
+
# def _fun(*args, **kwargs):
|
|
376
|
+
# val = fun(params)
|
|
377
|
+
# grads = grad_fun(*args, **kwargs)
|
|
378
|
+
# return val, grads
|
|
379
|
+
# return _fun
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from .parameter import (
|
|
2
|
+
BoundedParameter,
|
|
3
|
+
MaskedParameter,
|
|
4
|
+
NormalizedParameter,
|
|
5
|
+
Parameter,
|
|
6
|
+
SigmoidBoundedParameter,
|
|
7
|
+
TransformedParameter,
|
|
8
|
+
)
|
|
9
|
+
from .spaces import AbstractAxis, DataAxis, GridAxis, Space, UniformAxis
|
|
10
|
+
from .stateutils import (
|
|
11
|
+
collect_parameters,
|
|
12
|
+
combine_state,
|
|
13
|
+
mark_parameters,
|
|
14
|
+
partition_state,
|
|
15
|
+
show_parameters,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"BoundedParameter",
|
|
20
|
+
"MaskedParameter",
|
|
21
|
+
"NormalizedParameter",
|
|
22
|
+
"Parameter",
|
|
23
|
+
"SigmoidBoundedParameter",
|
|
24
|
+
"TransformedParameter",
|
|
25
|
+
"AbstractAxis",
|
|
26
|
+
"DataAxis",
|
|
27
|
+
"GridAxis",
|
|
28
|
+
"Space",
|
|
29
|
+
"UniformAxis",
|
|
30
|
+
"collect_parameters",
|
|
31
|
+
"combine_state",
|
|
32
|
+
"mark_parameters",
|
|
33
|
+
"partition_state",
|
|
34
|
+
"show_parameters",
|
|
35
|
+
]
|