differt 0.1.1__tar.gz → 0.1.2__tar.gz
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.
- {differt-0.1.1 → differt-0.1.2}/PKG-INFO +4 -4
- {differt-0.1.1 → differt-0.1.2}/README.md +1 -1
- {differt-0.1.1 → differt-0.1.2}/pyproject.toml +2 -2
- {differt-0.1.1 → differt-0.1.2}/src/differt/rt/_fermat.py +99 -39
- {differt-0.1.1 → differt-0.1.2}/src/differt/rt/_image_method.py +13 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/scene/_triangle_scene.py +19 -13
- {differt-0.1.1 → differt-0.1.2}/src/differt/utils.py +20 -75
- {differt-0.1.1 → differt-0.1.2}/.gitignore +0 -0
- {differt-0.1.1 → differt-0.1.2}/LICENSE.md +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/__init__.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/_version.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/__init__.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/_antenna.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/_constants.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/_fresnel.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/_interaction_type.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/_material.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/_utd.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/em/_utils.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/geometry/__init__.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/geometry/_paths.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/geometry/_triangle_mesh.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/geometry/_utils.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/plotting/__init__.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/plotting/_core.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/plotting/_utils.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/plugins/__init__.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/plugins/_deepmimo_types.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/plugins/deepmimo.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/py.typed +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/rt/__init__.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/rt/_utils.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/scene/__init__.py +0 -0
- {differt-0.1.1 → differt-0.1.2}/src/differt/scene/_sionna.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: DiffeRT
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Differentiable Ray Tracing Toolbox for Radio Propagation Simulations
|
|
5
5
|
Project-URL: Changelog, https://github.com/jeertmans/DiffeRT/releases
|
|
6
6
|
Project-URL: Documentation, https://eertmans.be/DiffeRT
|
|
@@ -21,10 +21,10 @@ Classifier: Programming Language :: Python :: 3.13
|
|
|
21
21
|
Classifier: Topic :: Scientific/Engineering
|
|
22
22
|
Requires-Python: >=3.10
|
|
23
23
|
Requires-Dist: chex>=0.1.86
|
|
24
|
-
Requires-Dist: differt-core==0.1.
|
|
24
|
+
Requires-Dist: differt-core==0.1.2
|
|
25
25
|
Requires-Dist: equinox>=0.12.2
|
|
26
26
|
Requires-Dist: filelock>=3.15.4
|
|
27
|
-
Requires-Dist: jax
|
|
27
|
+
Requires-Dist: jax>=0.4.32
|
|
28
28
|
Requires-Dist: jaxtyping>=0.3.2
|
|
29
29
|
Requires-Dist: numpy>=1.26.1
|
|
30
30
|
Requires-Dist: optax>=0.1.7
|
|
@@ -140,7 +140,7 @@ If you use this software, please cite it as:
|
|
|
140
140
|
author = {Eertmans, Jérome},
|
|
141
141
|
url = {https://github.com/jeertmans/DiffeRT},
|
|
142
142
|
license = {MIT},
|
|
143
|
-
version = {v0.1.
|
|
143
|
+
version = {v0.1.2}
|
|
144
144
|
}
|
|
145
145
|
```
|
|
146
146
|
|
|
@@ -16,10 +16,10 @@ classifiers = [
|
|
|
16
16
|
]
|
|
17
17
|
dependencies = [
|
|
18
18
|
"chex>=0.1.86",
|
|
19
|
-
"differt-core==0.1.
|
|
19
|
+
"differt-core==0.1.2",
|
|
20
20
|
"equinox>=0.12.2",
|
|
21
21
|
"filelock>=3.15.4",
|
|
22
|
-
"jax>=0.4.32
|
|
22
|
+
"jax>=0.4.32", # TODO: fixme, issue with pmap
|
|
23
23
|
"jaxtyping>=0.3.2",
|
|
24
24
|
"numpy>=1.26.1",
|
|
25
25
|
"optax>=0.1.7",
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
from functools import partial
|
|
2
|
-
from typing import Any
|
|
2
|
+
from typing import Any, no_type_check
|
|
3
3
|
|
|
4
|
+
import chex
|
|
4
5
|
import equinox as eqx
|
|
5
6
|
import jax
|
|
6
7
|
import jax.numpy as jnp
|
|
8
|
+
import optax
|
|
7
9
|
from jaxtyping import Array, ArrayLike, Float
|
|
8
10
|
|
|
9
11
|
from differt.geometry import orthogonal_basis
|
|
10
|
-
from differt.utils import minimize
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
@partial(jax.jit, inline=True)
|
|
@@ -29,14 +30,19 @@ def _loss(
|
|
|
29
30
|
o: Float[Array, "*#batch num_objects 3"],
|
|
30
31
|
v: Float[Array, "*#batch num_objects num_dims 3"],
|
|
31
32
|
) -> Float[Array, " *batch"]:
|
|
33
|
+
chex.assert_axis_dimension_gt(p, -1, 0, exception_type=TypeError)
|
|
34
|
+
chex.assert_axis_dimension(
|
|
35
|
+
p, -1, v.shape[-2] * v.shape[-3], exception_type=TypeError
|
|
36
|
+
)
|
|
32
37
|
xyz = _param_to_xyz(p, o, v)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
first_segment = xyz[..., 0, :] - from_
|
|
39
|
+
last_segment = to - xyz[..., -1, :]
|
|
40
|
+
other_segments = jnp.diff(xyz, axis=-2)
|
|
41
|
+
return (
|
|
42
|
+
jnp.linalg.norm(first_segment, axis=-1)
|
|
43
|
+
+ jnp.linalg.norm(last_segment, axis=-1)
|
|
44
|
+
+ jnp.sum(jnp.linalg.norm(other_segments, axis=-1), axis=-1)
|
|
36
45
|
)
|
|
37
|
-
vectors = jnp.diff(paths, axis=-2)
|
|
38
|
-
lengths = jnp.linalg.norm(vectors, axis=-1)
|
|
39
|
-
return jnp.sum(lengths, axis=-1)
|
|
40
46
|
|
|
41
47
|
|
|
42
48
|
@eqx.filter_jit
|
|
@@ -45,7 +51,9 @@ def fermat_path_on_linear_objects(
|
|
|
45
51
|
to_vertices: Float[ArrayLike, "*#batch 3"],
|
|
46
52
|
object_origins: Float[ArrayLike, "*#batch num_objects 3"],
|
|
47
53
|
object_vectors: Float[ArrayLike, "*#batch num_objects num_dims 3"],
|
|
48
|
-
|
|
54
|
+
*,
|
|
55
|
+
steps: int = 10,
|
|
56
|
+
optimizer: optax.GradientTransformationExtraArgs | None = None,
|
|
49
57
|
) -> Float[Array, "*batch num_objects 3"]:
|
|
50
58
|
"""
|
|
51
59
|
Return the ray paths between pairs of vertices, that reflect or diffract on a given list of objects in between.
|
|
@@ -77,13 +85,15 @@ def fermat_path_on_linear_objects(
|
|
|
77
85
|
|
|
78
86
|
It is used as the initial guess of the minimization procedure.
|
|
79
87
|
object_vectors: An array of base vectors describing the objects.
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
steps: The number of optimization steps to perform.
|
|
89
|
+
optimizer: The optimizer to use. If not provided,
|
|
90
|
+
uses :func:`optax.lbfgs`.
|
|
82
91
|
|
|
83
|
-
..
|
|
92
|
+
.. important::
|
|
84
93
|
|
|
85
|
-
The
|
|
86
|
-
|
|
94
|
+
The optimizer should store the gradient in the state. In the future,
|
|
95
|
+
we hope to support optimizers that do not store the gradient in the state,
|
|
96
|
+
such as :func:`optax.adam` or :func:`optax.sgd`.
|
|
87
97
|
|
|
88
98
|
Returns:
|
|
89
99
|
An array of ray paths obtained based on Fermat's principle.
|
|
@@ -166,10 +176,6 @@ def fermat_path_on_linear_objects(
|
|
|
166
176
|
object_origins = jnp.asarray(object_origins)
|
|
167
177
|
object_vectors = jnp.asarray(object_vectors)
|
|
168
178
|
|
|
169
|
-
num_objects = object_origins.shape[-2]
|
|
170
|
-
num_dims = object_vectors.shape[-2]
|
|
171
|
-
num_unknowns = num_objects * num_dims
|
|
172
|
-
|
|
173
179
|
batch = jnp.broadcast_shapes(
|
|
174
180
|
from_vertices.shape[:-1],
|
|
175
181
|
to_vertices.shape[:-1],
|
|
@@ -177,29 +183,83 @@ def fermat_path_on_linear_objects(
|
|
|
177
183
|
object_vectors.shape[:-3],
|
|
178
184
|
)
|
|
179
185
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
186
|
+
num_objects = object_origins.shape[-2]
|
|
187
|
+
|
|
188
|
+
if num_objects == 0:
|
|
189
|
+
# If there are no objects, return empty paths.
|
|
190
|
+
dtype = jnp.result_type(
|
|
191
|
+
from_vertices, to_vertices, object_origins, object_vectors
|
|
192
|
+
)
|
|
193
|
+
return jnp.empty((*batch, 0, 3), dtype=dtype)
|
|
194
|
+
|
|
195
|
+
num_dims = object_vectors.shape[-2]
|
|
196
|
+
|
|
197
|
+
if num_dims == 0:
|
|
198
|
+
# If there are no dimension, return origins.
|
|
199
|
+
dtype = jnp.result_type(
|
|
200
|
+
from_vertices, to_vertices, object_origins, object_vectors
|
|
201
|
+
)
|
|
202
|
+
return jnp.broadcast_to(object_origins, (*batch, num_objects, 3)).astype(dtype)
|
|
203
|
+
|
|
204
|
+
num_unknowns = num_objects * num_dims
|
|
205
|
+
|
|
206
|
+
objective = _loss
|
|
207
|
+
|
|
208
|
+
optimizer = optimizer if optimizer is not None else optax.lbfgs()
|
|
209
|
+
|
|
210
|
+
def minimize(
|
|
211
|
+
x_0: Float[Array, " num_unknowns"],
|
|
212
|
+
from_: Float[Array, "3"],
|
|
213
|
+
to: Float[Array, "3"],
|
|
214
|
+
o: Float[Array, "num_objects 3"],
|
|
215
|
+
v: Float[Array, "num_objects num_dims 3"],
|
|
216
|
+
) -> Float[Array, "num_objects 3"]:
|
|
217
|
+
opt_state = optimizer.init(x_0)
|
|
218
|
+
|
|
219
|
+
value_and_grad = optax.value_and_grad_from_state(objective)
|
|
220
|
+
|
|
221
|
+
@no_type_check
|
|
222
|
+
def update(
|
|
223
|
+
state: tuple[Float[Array, "*batch num_unknowns"], Any],
|
|
224
|
+
_: None,
|
|
225
|
+
) -> tuple[
|
|
226
|
+
tuple[Float[Array, "*batch num_unknowns"], Any], float | Float[Array, " "]
|
|
227
|
+
]:
|
|
228
|
+
x, opt_state = state
|
|
229
|
+
loss_value, grad = value_and_grad(x, from_, to, o, v, state=opt_state)
|
|
230
|
+
updates, opt_state = optimizer.update(
|
|
231
|
+
grad,
|
|
232
|
+
opt_state,
|
|
233
|
+
x,
|
|
234
|
+
value=loss_value,
|
|
235
|
+
grad=grad,
|
|
236
|
+
value_fn=objective,
|
|
237
|
+
from_=from_,
|
|
238
|
+
to=to,
|
|
239
|
+
o=o,
|
|
240
|
+
v=v,
|
|
241
|
+
)
|
|
242
|
+
x = optax.apply_updates(x, updates)
|
|
243
|
+
return (x, opt_state), loss_value
|
|
244
|
+
|
|
245
|
+
(x, _), _ = jax.lax.scan(update, (x_0, opt_state), length=steps)
|
|
246
|
+
return _param_to_xyz(x, o, v)
|
|
188
247
|
|
|
189
248
|
x_0 = jnp.zeros((*batch, num_unknowns))
|
|
190
|
-
x, _ = minimize(
|
|
191
|
-
_loss,
|
|
192
|
-
x_0,
|
|
193
|
-
args=(
|
|
194
|
-
from_vertices,
|
|
195
|
-
to_vertices,
|
|
196
|
-
object_origins,
|
|
197
|
-
object_vectors,
|
|
198
|
-
),
|
|
199
|
-
**kwargs,
|
|
200
|
-
)
|
|
201
249
|
|
|
202
|
-
|
|
250
|
+
for i in reversed(range(len(batch))):
|
|
251
|
+
minimize = jax.vmap(
|
|
252
|
+
minimize,
|
|
253
|
+
in_axes=(
|
|
254
|
+
0,
|
|
255
|
+
0 if from_vertices.ndim > i + 1 else None,
|
|
256
|
+
0 if to_vertices.ndim > i + 1 else None,
|
|
257
|
+
0 if object_origins.ndim > i + 2 else None,
|
|
258
|
+
0 if object_vectors.ndim > i + 3 else None,
|
|
259
|
+
),
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
return minimize(x_0, from_vertices, to_vertices, object_origins, object_vectors)
|
|
203
263
|
|
|
204
264
|
|
|
205
265
|
@eqx.filter_jit
|
|
@@ -232,7 +292,7 @@ def fermat_path_on_planar_mirrors(
|
|
|
232
292
|
be unit vectors. However, we keep the same documentation so it is
|
|
233
293
|
easier for the user to move from one method to the other.
|
|
234
294
|
kwargs: Keyword arguments passed to
|
|
235
|
-
:func:`
|
|
295
|
+
:func:`fermat_path_on_linear_objects`.
|
|
236
296
|
|
|
237
297
|
Returns:
|
|
238
298
|
An array of ray paths obtained using Fermat's principle.
|
|
@@ -290,6 +290,14 @@ def image_method(
|
|
|
290
290
|
mirror_normals.shape[1:],
|
|
291
291
|
)
|
|
292
292
|
|
|
293
|
+
if mirror_vertices.shape[0] == 0:
|
|
294
|
+
# If there are no mirrors, return empty array.
|
|
295
|
+
*batch, _ = batch_and_3
|
|
296
|
+
dtype = jnp.result_type(
|
|
297
|
+
from_vertices, to_vertices, mirror_vertices, mirror_normals
|
|
298
|
+
)
|
|
299
|
+
return jnp.empty((*batch, 0, 3), dtype=dtype)
|
|
300
|
+
|
|
293
301
|
from_vertices = jnp.broadcast_to(from_vertices, batch_and_3)
|
|
294
302
|
to_vertices = jnp.broadcast_to(to_vertices, batch_and_3)
|
|
295
303
|
|
|
@@ -420,6 +428,11 @@ def consecutive_vertices_are_on_same_side_of_mirrors(
|
|
|
420
428
|
vertices, -2, mirror_vertices.shape[-2] + 2, exception_type=TypeError
|
|
421
429
|
)
|
|
422
430
|
|
|
431
|
+
if mirror_vertices.shape[-2] == 0:
|
|
432
|
+
# If there are no mirrors, return empty array.
|
|
433
|
+
dtype = bool if smoothing_factor is None else float
|
|
434
|
+
return jnp.empty(mirror_vertices.shape[:-1], dtype=dtype)
|
|
435
|
+
|
|
423
436
|
# dot_{prev,next} = <(v_{prev,next} - mirror_v), mirror_n>
|
|
424
437
|
|
|
425
438
|
# [num_mirrors]
|
|
@@ -7,9 +7,7 @@ from typing import TYPE_CHECKING, Any, Literal, overload
|
|
|
7
7
|
import equinox as eqx
|
|
8
8
|
import jax
|
|
9
9
|
import jax.numpy as jnp
|
|
10
|
-
from jax.experimental import mesh_utils
|
|
11
10
|
from jax.experimental.shard_map import shard_map
|
|
12
|
-
from jax.sharding import Mesh
|
|
13
11
|
from jax.sharding import PartitionSpec as P
|
|
14
12
|
from jaxtyping import Array, ArrayLike, Bool, Float, Int
|
|
15
13
|
|
|
@@ -279,11 +277,9 @@ def _compute_paths( # noqa: C901, PLR0915
|
|
|
279
277
|
)
|
|
280
278
|
raise ValueError(msg)
|
|
281
279
|
|
|
282
|
-
fun = shard_map(
|
|
280
|
+
fun = shard_map(
|
|
283
281
|
fun,
|
|
284
|
-
|
|
285
|
-
mesh_utils.create_device_mesh((tx_mesh, rx_mesh)), axis_names=("i", "j")
|
|
286
|
-
),
|
|
282
|
+
jax.make_mesh((tx_mesh, rx_mesh), axis_names=("i", "j")),
|
|
287
283
|
in_specs=in_specs,
|
|
288
284
|
out_specs=out_specs,
|
|
289
285
|
)
|
|
@@ -487,12 +483,9 @@ def _compute_paths_sbr(
|
|
|
487
483
|
)
|
|
488
484
|
raise ValueError(msg)
|
|
489
485
|
|
|
490
|
-
fun = shard_map(
|
|
486
|
+
fun = shard_map(
|
|
491
487
|
fun,
|
|
492
|
-
|
|
493
|
-
mesh_utils.create_device_mesh((tx_mesh, ray_mesh)),
|
|
494
|
-
axis_names=("i", "j"),
|
|
495
|
-
),
|
|
488
|
+
jax.make_mesh((tx_mesh, ray_mesh), axis_names=("i", "j")),
|
|
496
489
|
in_specs=in_specs,
|
|
497
490
|
out_specs=out_specs,
|
|
498
491
|
)
|
|
@@ -904,7 +897,7 @@ class TriangleScene(eqx.Module):
|
|
|
904
897
|
confidence_threshold: Float[ArrayLike, " "] = 0.5,
|
|
905
898
|
) -> SBRPaths: ...
|
|
906
899
|
|
|
907
|
-
def compute_paths( # noqa: C901
|
|
900
|
+
def compute_paths( # noqa: C901, PLR0912
|
|
908
901
|
self,
|
|
909
902
|
order: int | None = None,
|
|
910
903
|
*,
|
|
@@ -988,6 +981,9 @@ class TriangleScene(eqx.Module):
|
|
|
988
981
|
|
|
989
982
|
When ``method == 'sbr'``, the number of transmitters times the number of rays
|
|
990
983
|
**must** be a multiple of :func:`jax.device_count`, otherwise an error is raised.
|
|
984
|
+
|
|
985
|
+
.. warning::
|
|
986
|
+
Parallel execution is automatically disabled if ``jax>=0.6`` is used, see `#280 <https://github.com/jeertmans/DiffeRT/issues/280>`_.
|
|
991
987
|
epsilon: Tolelance for checking ray / objects intersection, see
|
|
992
988
|
:func:`rays_intersect_triangles<differt.rt.rays_intersect_triangles>`.
|
|
993
989
|
hit_tol: Tolerance for checking blockage (i.e., obstruction), see
|
|
@@ -1036,8 +1032,18 @@ class TriangleScene(eqx.Module):
|
|
|
1036
1032
|
|
|
1037
1033
|
If ``method == 'sbr'``, ``order`` is required.
|
|
1038
1034
|
|
|
1035
|
+
If ``parallel`` is :data:`True`, and the number of devices is not a divisor of the number of transmitters times the number of receivers, or the number of transmitters times the number of rays.
|
|
1036
|
+
|
|
1039
1037
|
.. [#f1] Passing the squared length/distance is useful to avoid computing square root values, which is expensive.
|
|
1040
|
-
"""
|
|
1038
|
+
""" # noqa: DOC502
|
|
1039
|
+
if parallel and jax.__version_info__ >= (0, 6):
|
|
1040
|
+
msg = (
|
|
1041
|
+
"Parallel execution is automatically disabled when using jax>=0.6, "
|
|
1042
|
+
"as it is not (yet) compatible with the new JAX API. See "
|
|
1043
|
+
"https://github.com/jeertmans/DiffeRT/issues/280."
|
|
1044
|
+
)
|
|
1045
|
+
warnings.warn(msg, UserWarning, stacklevel=2)
|
|
1046
|
+
parallel = False
|
|
1041
1047
|
if (order is None) == (path_candidates is None):
|
|
1042
1048
|
msg = "You must specify one of 'order' or `path_candidates`, not both."
|
|
1043
1049
|
raise ValueError(msg)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""General purpose utilities."""
|
|
2
2
|
|
|
3
|
+
import warnings
|
|
3
4
|
from collections.abc import Callable
|
|
4
5
|
from functools import partial
|
|
5
6
|
from typing import (
|
|
@@ -147,7 +148,14 @@ P = ParamSpec("P")
|
|
|
147
148
|
|
|
148
149
|
|
|
149
150
|
class OptimizeResult(NamedTuple):
|
|
150
|
-
"""
|
|
151
|
+
"""
|
|
152
|
+
A class to hold the result of an optimization, akin to :class:`scipy.optimize.OptimizeResult`.
|
|
153
|
+
|
|
154
|
+
.. deprecated:: 0.1.2
|
|
155
|
+
This class is deprecated and will be removed in the v0.2.0 release.
|
|
156
|
+
See `#283 <https://github.com/jeertmans/DiffeRT/issues/283>`_ for motivation
|
|
157
|
+
and alternatives.
|
|
158
|
+
"""
|
|
151
159
|
|
|
152
160
|
x: Inexact[Array, "*batch n"]
|
|
153
161
|
"""The solution of the optimization."""
|
|
@@ -166,6 +174,11 @@ def minimize(
|
|
|
166
174
|
"""
|
|
167
175
|
Minimize a scalar function of one or more variables.
|
|
168
176
|
|
|
177
|
+
.. deprecated:: 0.1.2
|
|
178
|
+
This function is deprecated and will be removed in the v0.2.0 release.
|
|
179
|
+
See `#283 <https://github.com/jeertmans/DiffeRT/issues/283>`_ for motivation
|
|
180
|
+
and alternatives.
|
|
181
|
+
|
|
169
182
|
The minimization is achieved by computing the
|
|
170
183
|
gradient of the objective function, and performing
|
|
171
184
|
a fixed number of iterations (i.e., ``steps``).
|
|
@@ -201,81 +214,13 @@ def minimize(
|
|
|
201
214
|
|
|
202
215
|
Returns:
|
|
203
216
|
The optimization result.
|
|
204
|
-
|
|
205
|
-
Examples:
|
|
206
|
-
The following example shows how to minimize a basic function.
|
|
207
|
-
|
|
208
|
-
>>> from differt.utils import minimize
|
|
209
|
-
>>> import chex
|
|
210
|
-
>>>
|
|
211
|
-
>>> def f(x, offset=1.0):
|
|
212
|
-
... x = x - offset
|
|
213
|
-
... return jnp.dot(x, x)
|
|
214
|
-
>>>
|
|
215
|
-
>>> x, y = minimize(f, jnp.zeros(10))
|
|
216
|
-
>>> chex.assert_trees_all_close(x, jnp.ones(10), rtol=1e-2)
|
|
217
|
-
>>> chex.assert_trees_all_close(y, 0.0, atol=1e-4)
|
|
218
|
-
>>>
|
|
219
|
-
>>> # It is also possible to pass positional arguments
|
|
220
|
-
>>> x, y = minimize(f, jnp.zeros(10), args=(2.0,))
|
|
221
|
-
>>> chex.assert_trees_all_close(x, 2.0 * jnp.ones(10), rtol=1e-2)
|
|
222
|
-
>>> chex.assert_trees_all_close(y, 0.0, atol=1e-3)
|
|
223
|
-
>>>
|
|
224
|
-
>>> # You can also change the optimizer and the number of steps
|
|
225
|
-
>>> import optax
|
|
226
|
-
>>> optimizer = optax.noisy_sgd(learning_rate=0.003, key=jax.random.key(1234))
|
|
227
|
-
>>> x, y = minimize(
|
|
228
|
-
... f, jnp.zeros(5), args=(4.0,), steps=10000, optimizer=optimizer
|
|
229
|
-
... )
|
|
230
|
-
>>> chex.assert_trees_all_close(x, 4.0 * jnp.ones(5), rtol=1e-2)
|
|
231
|
-
>>> chex.assert_trees_all_close(y, 0.0, atol=1e-3)
|
|
232
|
-
|
|
233
|
-
This example shows how you can minimize on a batch of arrays.
|
|
234
|
-
|
|
235
|
-
>>> from differt.utils import minimize
|
|
236
|
-
>>> import chex
|
|
237
|
-
>>>
|
|
238
|
-
>>> batch = (1, 2, 3)
|
|
239
|
-
>>> n = 10
|
|
240
|
-
>>> key = jax.random.key(1234)
|
|
241
|
-
>>> offset = jax.random.uniform(key, (*batch, n))
|
|
242
|
-
>>>
|
|
243
|
-
>>> def f(x, offset, scale=2.0):
|
|
244
|
-
... x = scale * x - offset
|
|
245
|
-
... return jnp.sum(x * x, axis=-1)
|
|
246
|
-
>>>
|
|
247
|
-
>>> x0 = jnp.zeros((*batch, n))
|
|
248
|
-
>>> x, y = minimize(f, x0, args=(offset,), steps=1000)
|
|
249
|
-
>>> chex.assert_trees_all_close(x, offset / 2.0, rtol=1e-2)
|
|
250
|
-
>>> chex.assert_trees_all_close(y, 0.0, atol=1e-4)
|
|
251
|
-
>>>
|
|
252
|
-
>>> # By default, arguments are expected to have batch
|
|
253
|
-
>>> # dimensions like 'x0'. So, if 'x0' has more than one dimension,
|
|
254
|
-
>>> # 'offset' must be an ndarray with the same shape prefix as 'x0':
|
|
255
|
-
>>> offset = 10.0
|
|
256
|
-
>>> x, y = minimize(
|
|
257
|
-
... f, x0, args=(offset,), steps=1000
|
|
258
|
-
... ) # doctest: +IGNORE_EXCEPTION_DETAIL
|
|
259
|
-
Traceback (most recent call last):
|
|
260
|
-
TypeError: ... Tree leaf '0' is not an ndarray (type=<class 'float'>).
|
|
261
|
-
>>>
|
|
262
|
-
>>> # Passing 'offset' as an ndarray instead:
|
|
263
|
-
>>> x, y = minimize(
|
|
264
|
-
... f, x0, args=(jnp.array(offset),), steps=1000
|
|
265
|
-
... ) # doctest: +IGNORE_EXCEPTION_DETAIL
|
|
266
|
-
Traceback (most recent call last):
|
|
267
|
-
TypeError: ... Tree leaf '1' has a shape of length 0 (shape=()) which
|
|
268
|
-
is smaller than the expected prefix of length 3 (prefix=(1, 2, 3)).
|
|
269
|
-
>>>
|
|
270
|
-
>>> # For static arguments, use functools.partial
|
|
271
|
-
>>> from functools import partial
|
|
272
|
-
>>>
|
|
273
|
-
>>> fp = partial(f, offset=offset)
|
|
274
|
-
>>> x, y = minimize(fp, x0, steps=1000)
|
|
275
|
-
>>> chex.assert_trees_all_close(x, offset * jnp.ones_like(x0) / 2.0, rtol=1e-2)
|
|
276
|
-
>>> chex.assert_trees_all_close(y, 0.0, atol=1e-2)
|
|
277
217
|
"""
|
|
278
|
-
|
|
218
|
+
msg = (
|
|
219
|
+
"This function is deprecated and will be removed in the v0.2.0 release. "
|
|
220
|
+
"See https://github.com/jeertmans/DiffeRT/issues/283 for motivation and alternatives."
|
|
221
|
+
)
|
|
222
|
+
warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
|
|
223
|
+
|
|
279
224
|
x0 = jnp.asarray(x0)
|
|
280
225
|
if x0.ndim > 1 and args:
|
|
281
226
|
chex.assert_tree_has_only_ndarrays(args, exception_type=TypeError)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|