prtools 1.4.1__tar.gz → 1.4.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.
- {prtools-1.4.1/prtools.egg-info → prtools-1.4.2}/PKG-INFO +1 -1
- {prtools-1.4.1 → prtools-1.4.2}/prtools/__init__.py +1 -1
- {prtools-1.4.1 → prtools-1.4.2}/prtools/backend/_jax.py +5 -5
- {prtools-1.4.1 → prtools-1.4.2}/prtools/fourier.py +3 -6
- {prtools-1.4.1 → prtools-1.4.2}/prtools/jax.py +15 -16
- {prtools-1.4.1 → prtools-1.4.2/prtools.egg-info}/PKG-INFO +1 -1
- {prtools-1.4.1 → prtools-1.4.2}/prtools.egg-info/SOURCES.txt +2 -1
- prtools-1.4.2/tests/test_jax.py +18 -0
- {prtools-1.4.1 → prtools-1.4.2}/LICENSE +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/NOTICE +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/README.rst +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/array.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/backend/__init__.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/backend/_base.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/backend/_numpy.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/convolve.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/cost.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/misc.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/segmented.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/shape.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/sparse.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/stats.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools/zernike.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools.egg-info/dependency_links.txt +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools.egg-info/requires.txt +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/prtools.egg-info/top_level.txt +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/pyproject.toml +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/setup.cfg +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/tests/test_array.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/tests/test_convolve.py +0 -0
- {prtools-1.4.1 → prtools-1.4.2}/tests/test_fourier.py +0 -0
|
@@ -44,11 +44,11 @@ class Numpy(BackendLibrary):
|
|
|
44
44
|
a = self.module.asarray(a)
|
|
45
45
|
return self.module.sum(a, *args, **kwargs)
|
|
46
46
|
|
|
47
|
-
def take(self, a, indices, *args, **kwargs):
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
#def take(self, a, indices, *args, **kwargs):
|
|
48
|
+
# # jax numpy.take expects an array input for a and indices
|
|
49
|
+
# a = self.module.asarray(a)
|
|
50
|
+
# indices = self.module.asarray(indices)
|
|
51
|
+
# return self.module.take(a, indices, *args, **kwargs)
|
|
52
52
|
|
|
53
53
|
|
|
54
54
|
class Scipy(BackendLibrary):
|
|
@@ -100,11 +100,8 @@ def _dftcore(f, alpha, shape, shift, offset, axes, unitary, forward, out):
|
|
|
100
100
|
|
|
101
101
|
f = np.asarray(f)
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
m, n = in_shape
|
|
107
|
-
M, N = out_shape
|
|
103
|
+
(M, N), axes = _cook_nd_args(f, shape, axes)
|
|
104
|
+
m, n = (f.shape[axes[0]], f.shape[axes[1]])
|
|
108
105
|
|
|
109
106
|
alpha_row, alpha_col = np.broadcast_to(alpha, (2,))
|
|
110
107
|
shift_row, shift_col = np.broadcast_to(shift, (2,))
|
|
@@ -158,7 +155,7 @@ def _cook_nd_args(a, s=None, axes=None):
|
|
|
158
155
|
else:
|
|
159
156
|
raise ValueError("Array must have ndim == 2 or 3")
|
|
160
157
|
else:
|
|
161
|
-
s = np.take(a.shape, axes)
|
|
158
|
+
s = np.take(np.asarray(a.shape), np.asarray(axes))
|
|
162
159
|
s = list(s)
|
|
163
160
|
if axes is None:
|
|
164
161
|
axes = list(range(-len(s), 0))
|
|
@@ -31,8 +31,7 @@ class JaxOptimizeResult:
|
|
|
31
31
|
state: Any #: Optimizer state
|
|
32
32
|
|
|
33
33
|
|
|
34
|
-
def lbfgs(fn, x0, gtol=None, maxiter=None, callback=None,
|
|
35
|
-
fn_kwargs=None):
|
|
34
|
+
def lbfgs(fn, x0, gtol=None, maxiter=None, callback=None, fn_kwargs=None):
|
|
36
35
|
"""Minimize a scalar function of one or more variables using the L-BFGS
|
|
37
36
|
algorithm
|
|
38
37
|
|
|
@@ -43,10 +42,10 @@ def lbfgs(fn, x0, gtol=None, maxiter=None, callback=None, fn_args=None,
|
|
|
43
42
|
|
|
44
43
|
.. code:: python
|
|
45
44
|
|
|
46
|
-
fn(x,
|
|
45
|
+
fn(x, **fn_kwargs)
|
|
47
46
|
|
|
48
|
-
where ``x`` is a 1-D array with shape (n,) and ``
|
|
49
|
-
|
|
47
|
+
where ``x`` is a 1-D array with shape (n,) and ``fn_kwargs`` are
|
|
48
|
+
optional keyword arguments.
|
|
50
49
|
x0 : jax.Array
|
|
51
50
|
Initial guess
|
|
52
51
|
gtol : float
|
|
@@ -61,10 +60,9 @@ def lbfgs(fn, x0, gtol=None, maxiter=None, callback=None, fn_args=None,
|
|
|
61
60
|
callback(intermediate_result: JaxOptimizeResult)
|
|
62
61
|
|
|
63
62
|
where ``intermediate_result`` is a :class:`JaxOptimizeResult`.
|
|
64
|
-
fn_args : iterable or None
|
|
65
|
-
Extra positional arguments passed to ``fn()``
|
|
66
63
|
fn_kwargs : dict or None
|
|
67
|
-
Extra keyword arguments passed to the
|
|
64
|
+
Extra keyword arguments passed to the objective function as
|
|
65
|
+
``fn(x, **fn_kwargs)``
|
|
68
66
|
|
|
69
67
|
Returns
|
|
70
68
|
-------
|
|
@@ -83,7 +81,6 @@ def lbfgs(fn, x0, gtol=None, maxiter=None, callback=None, fn_args=None,
|
|
|
83
81
|
if not any((gtol, maxiter)):
|
|
84
82
|
raise ValueError('At least one termination tolerance must be specified.')
|
|
85
83
|
|
|
86
|
-
fn_args = [] if fn_args is None else fn_args
|
|
87
84
|
fn_kwargs = {} if fn_kwargs is None else fn_kwargs
|
|
88
85
|
|
|
89
86
|
opt = optax.lbfgs()
|
|
@@ -93,11 +90,12 @@ def lbfgs(fn, x0, gtol=None, maxiter=None, callback=None, fn_args=None,
|
|
|
93
90
|
params, state = carry
|
|
94
91
|
# NOTE: passing *args and **kwargs to value_and_grad_fun is very
|
|
95
92
|
# poorly documented in optax (as of v0.2.6 - 10/2025) but this
|
|
96
|
-
# seems to work for now
|
|
97
|
-
value, grad = value_and_grad_fn(params,
|
|
98
|
-
|
|
93
|
+
# seems to work for now.
|
|
94
|
+
value, grad = value_and_grad_fn(params, state=state, **fn_kwargs)
|
|
95
|
+
# NOTE: opt.update() doesn't have any mechanism for accepting *args so
|
|
96
|
+
# we're limited to using **kwargs here
|
|
99
97
|
updates, state = opt.update(
|
|
100
|
-
grad, state, params, value=value, grad=grad, value_fn=fn)
|
|
98
|
+
grad, state, params, value=value, grad=grad, value_fn=fn, **fn_kwargs)
|
|
101
99
|
if callback:
|
|
102
100
|
res = JaxOptimizeResult(
|
|
103
101
|
n=otu.tree_get(state, 'count')-1,
|
|
@@ -151,9 +149,10 @@ def _multi_dot_three(a, b, c, axes, out):
|
|
|
151
149
|
iter_axis = _iter_axis(axes)
|
|
152
150
|
return jax.vmap(_multi_dot, in_axes=[None, iter_axis, None], out_axes=iter_axis)(a, b, c)
|
|
153
151
|
|
|
152
|
+
|
|
154
153
|
def _multi_dot(a, b, c):
|
|
155
|
-
|
|
156
|
-
|
|
154
|
+
# wrapper function to support vmap call signature
|
|
155
|
+
return jax.numpy.linalg.multi_dot((a, b, c))
|
|
157
156
|
|
|
158
157
|
|
|
159
158
|
def _iter_axis(axes):
|
|
@@ -169,4 +168,4 @@ def _ndrebin(a, f):
|
|
|
169
168
|
|
|
170
169
|
|
|
171
170
|
def _rebin(a, f):
|
|
172
|
-
return jax.numpy.reshape(a, (a.shape[0]//f, f, a.shape[1]//f, f)).sum(-1).sum(1)
|
|
171
|
+
return jax.numpy.reshape(a, (a.shape[0]//f, f, a.shape[1]//f, f)).sum(-1).sum(1)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import jax.numpy as jnp
|
|
3
|
+
|
|
4
|
+
import prtools
|
|
5
|
+
|
|
6
|
+
def test_lbfgs_fn_kwargs():
|
|
7
|
+
prtools.use('jax')
|
|
8
|
+
|
|
9
|
+
def fn(x, a, b):
|
|
10
|
+
return (x[0] + a)**2 + (x[1] + b)**2
|
|
11
|
+
|
|
12
|
+
a = 2
|
|
13
|
+
b = -5
|
|
14
|
+
x0 = jnp.zeros(2)
|
|
15
|
+
|
|
16
|
+
res = prtools.jax.lbfgs(fn, x0, gtol=1e-5, maxiter=25,
|
|
17
|
+
fn_kwargs={'a': a, 'b': b})
|
|
18
|
+
assert(np.allclose(res.x, (-a, -b)))
|
|
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
|