invrs-opt 0.6.0__py3-none-any.whl → 0.7.1__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.
@@ -1,150 +0,0 @@
1
- import dataclasses
2
- from typing import Any, Callable, Tuple
3
-
4
- import jax
5
- import jax.numpy as jnp
6
- import optax # type: ignore[import-untyped]
7
- from jax import tree_util
8
- from totypes import types
9
-
10
- from invrs_opt import base, transform
11
-
12
- PyTree = Any
13
- WrappedOptaxState = Tuple[PyTree, PyTree, PyTree]
14
-
15
-
16
- def wrapped_optax(opt: optax.GradientTransformation) -> base.Optimizer:
17
- """Return a wrapped optax optimizer."""
18
- return transformed_wrapped_optax(
19
- opt=opt,
20
- transform_fn=lambda x: x,
21
- initialize_latent_fn=lambda x: x,
22
- )
23
-
24
-
25
- def density_wrapped_optax(
26
- opt: optax.GradientTransformation,
27
- beta: float,
28
- ) -> base.Optimizer:
29
- """Return a wrapped optax optimizer with transforms for density arrays."""
30
-
31
- def transform_fn(tree: PyTree) -> PyTree:
32
- return tree_util.tree_map(
33
- lambda x: transform_density(x) if _is_density(x) else x,
34
- tree,
35
- is_leaf=_is_density,
36
- )
37
-
38
- def initialize_latent_fn(tree: PyTree) -> PyTree:
39
- return tree_util.tree_map(
40
- lambda x: initialize_latent_density(x) if _is_density(x) else x,
41
- tree,
42
- is_leaf=_is_density,
43
- )
44
-
45
- def transform_density(density: types.Density2DArray) -> types.Density2DArray:
46
- transformed = types.symmetrize_density(density)
47
- transformed = transform.density_gaussian_filter_and_tanh(transformed, beta=beta)
48
- # Scale to ensure that the full valid range of the density array is reachable.
49
- mid_value = (density.lower_bound + density.upper_bound) / 2
50
- transformed = tree_util.tree_map(
51
- lambda array: mid_value + (array - mid_value) / jnp.tanh(beta), transformed
52
- )
53
- return transform.apply_fixed_pixels(transformed)
54
-
55
- def initialize_latent_density(
56
- density: types.Density2DArray,
57
- ) -> types.Density2DArray:
58
- array = transform.normalized_array_from_density(density)
59
- array = jnp.clip(array, -1, 1)
60
- array *= jnp.tanh(beta)
61
- latent_array = jnp.arctanh(array) / beta
62
- latent_array = transform.rescale_array_for_density(latent_array, density)
63
- return dataclasses.replace(density, array=latent_array)
64
-
65
- return transformed_wrapped_optax(
66
- opt=opt,
67
- transform_fn=transform_fn,
68
- initialize_latent_fn=initialize_latent_fn,
69
- )
70
-
71
-
72
- def transformed_wrapped_optax(
73
- opt: optax.GradientTransformation,
74
- transform_fn: Callable[[PyTree], PyTree],
75
- initialize_latent_fn: Callable[[PyTree], PyTree],
76
- ) -> base.Optimizer:
77
- """Return a wrapped optax optimizer for transformed latent parameters.
78
-
79
- Args:
80
- opt: The optax `GradientTransformation` to be wrapped.
81
- transform_fn: Function which transforms the internal latent parameters to
82
- the parameters returned by the optimizer.
83
- initialize_latent_fn: Function which computes the initial latent parameters
84
- given the initial parameters.
85
-
86
- Returns:
87
- The `base.Optimizer`.
88
- """
89
-
90
- def init_fn(params: PyTree) -> WrappedOptaxState:
91
- """Initializes the optimization state."""
92
- latent_params = initialize_latent_fn(_clip(params))
93
- params = transform_fn(latent_params)
94
- return params, latent_params, opt.init(latent_params)
95
-
96
- def params_fn(state: WrappedOptaxState) -> PyTree:
97
- """Returns the parameters for the given `state`."""
98
- params, _, _ = state
99
- return params
100
-
101
- def update_fn(
102
- *,
103
- grad: PyTree,
104
- value: float,
105
- params: PyTree,
106
- state: WrappedOptaxState,
107
- ) -> WrappedOptaxState:
108
- """Updates the state."""
109
- del value
110
-
111
- _, latent_params, opt_state = state
112
- _, vjp_fn = jax.vjp(transform_fn, latent_params)
113
- (latent_grad,) = vjp_fn(grad)
114
-
115
- updates, opt_state = opt.update(latent_grad, opt_state)
116
- latent_params = optax.apply_updates(params=latent_params, updates=updates)
117
- latent_params = _clip(latent_params)
118
- params = transform_fn(latent_params)
119
- return params, latent_params, opt_state
120
-
121
- return base.Optimizer(
122
- init=init_fn,
123
- params=params_fn,
124
- update=update_fn,
125
- )
126
-
127
-
128
- def _is_density(leaf: Any) -> Any:
129
- """Return `True` if `leaf` is a density array."""
130
- return isinstance(leaf, types.Density2DArray)
131
-
132
-
133
- def _is_custom_type(leaf: Any) -> bool:
134
- """Return `True` if `leaf` is a recognized custom type."""
135
- return isinstance(leaf, (types.BoundedArray, types.Density2DArray))
136
-
137
-
138
- def _clip(pytree: PyTree) -> PyTree:
139
- """Clips leaves on `pytree` to their bounds."""
140
-
141
- def _clip_fn(leaf: Any) -> Any:
142
- if not _is_custom_type(leaf):
143
- return leaf
144
- if leaf.lower_bound is None and leaf.upper_bound is None:
145
- return leaf
146
- return tree_util.tree_map(
147
- lambda x: jnp.clip(x, leaf.lower_bound, leaf.upper_bound), leaf
148
- )
149
-
150
- return tree_util.tree_map(_clip_fn, pytree, is_leaf=_is_custom_type)
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 The INVRS-IO authors.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,76 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: invrs_opt
3
- Version: 0.6.0
4
- Summary: Algorithms for inverse design
5
- Author-email: "Martin F. Schubert" <mfschubert@gmail.com>
6
- Maintainer-email: "Martin F. Schubert" <mfschubert@gmail.com>
7
- License: MIT License
8
-
9
- Copyright (c) 2023 The INVRS-IO authors.
10
-
11
- Permission is hereby granted, free of charge, to any person obtaining a copy
12
- of this software and associated documentation files (the "Software"), to deal
13
- in the Software without restriction, including without limitation the rights
14
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
- copies of the Software, and to permit persons to whom the Software is
16
- furnished to do so, subject to the following conditions:
17
-
18
- The above copyright notice and this permission notice shall be included in all
19
- copies or substantial portions of the Software.
20
-
21
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
- SOFTWARE.
28
-
29
- Keywords: topology,optimization,jax,inverse design
30
- Requires-Python: >=3.7
31
- Description-Content-Type: text/markdown
32
- License-File: LICENSE
33
- Requires-Dist: jax
34
- Requires-Dist: jaxlib
35
- Requires-Dist: numpy
36
- Requires-Dist: requests
37
- Requires-Dist: optax
38
- Requires-Dist: scipy
39
- Requires-Dist: totypes
40
- Requires-Dist: types-requests
41
- Provides-Extra: dev
42
- Requires-Dist: bump-my-version ; extra == 'dev'
43
- Requires-Dist: darglint ; extra == 'dev'
44
- Requires-Dist: mypy ; extra == 'dev'
45
- Requires-Dist: pre-commit ; extra == 'dev'
46
- Provides-Extra: tests
47
- Requires-Dist: parameterized ; extra == 'tests'
48
- Requires-Dist: pytest ; extra == 'tests'
49
- Requires-Dist: pytest-cov ; extra == 'tests'
50
- Requires-Dist: pytest-subtests ; extra == 'tests'
51
-
52
- # invrs-opt - Optimization algorithms for inverse design
53
- `v0.6.0`
54
-
55
- ## Overview
56
-
57
- The `invrs-opt` package defines an optimizer API intended for topology optimization, inverse design, or AI-guided design. It (currently) implements the L-BFGS-B optimization algorithm along with some variants. The API is intended to be general so that new algorithms can be accommodated, and is inspired by the functional optimizer approach used in jax. Example usage is as follows:
58
-
59
- ```python
60
- initial_params = ...
61
-
62
- optimizer = invrs_opt.lbfgsb()
63
- state = optimizer.init(initial_params)
64
-
65
- for _ in range(steps):
66
- params = optimizer.params(state)
67
- value, grad = jax.value_and_grad(loss_fn)(params)
68
- state = optimizer.update(grad=grad, value=value, params=params, state=state)
69
- ```
70
-
71
- Optimizers in `invrs-opt` are compatible with custom types defined in the [totypes](https://github.com/invrs-io/totypes) package. The basic `lbfgsb` optimizer enforces bounds for custom types, while the `density_lbfgsb` optimizer implements a filter-and-threshold operation for `DensityArray2D` types to ensure that solutions have the correct length scale.
72
-
73
- ## Install
74
- ```
75
- pip install invrs_opt
76
- ```
@@ -1,16 +0,0 @@
1
- invrs_opt/__init__.py,sha256=35pvMpeqvJgU4DizUO5hTzeE9j93prbB9RMtcaoFYwg,496
2
- invrs_opt/base.py,sha256=FdQyPTlWGo03YztI3K2_QBN6Q-v0PeXv6XCyXu_uh_4,1160
3
- invrs_opt/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- invrs_opt/transform.py,sha256=a_Saj9Wq4lvqCJBrg5L2Z9DZ2NVs1xqrHLqha90a9Ws,5971
5
- invrs_opt/experimental/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- invrs_opt/experimental/client.py,sha256=MqC_TguT9IGrG7WW54vwz6QQMylKkbCjHxFPIG9vQMA,4841
7
- invrs_opt/experimental/labels.py,sha256=dQDAMPyFMV6mXnMy295z8Ap205DRdVzysXny_Be8FmY,562
8
- invrs_opt/lbfgsb/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- invrs_opt/lbfgsb/lbfgsb.py,sha256=pfrqCaOMco-eHUQe2q03hbla9D2TYqmMB-07jK4-5Ik,27792
10
- invrs_opt/wrapped_optax/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- invrs_opt/wrapped_optax/wrapped_optax.py,sha256=-ke0MNCb2EB0ntlj5IHIHrvybOVF4m24DM6JI4_Ktcc,4974
12
- invrs_opt-0.6.0.dist-info/LICENSE,sha256=ty6jHPvpyjHy6dbhnu6aDSY05bbl2jQTjnq9u1sBCfM,1078
13
- invrs_opt-0.6.0.dist-info/METADATA,sha256=V4hpzjEovC2CU0IgUBMAgKzPfgqGvLlutQ9X5-FOuIk,3347
14
- invrs_opt-0.6.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
15
- invrs_opt-0.6.0.dist-info/top_level.txt,sha256=hOziS2uJ_NgwaW9yhtOfeuYnm1X7vobPBcp_6eVWKfM,10
16
- invrs_opt-0.6.0.dist-info/RECORD,,
File without changes