39
39
from ._numdiff import approx_derivative
40
40
from scipy ._lib ._util import getfullargspec_no_self as _getfullargspec
41
41
from scipy ._lib ._util import (MapWrapper , check_random_state , _RichResult ,
42
- _call_callback_maybe_halt )
42
+ _call_callback_maybe_halt , _transition_to_rng )
43
43
from scipy .optimize ._differentiable_functions import ScalarFunction , FD_METHODS
44
44
from scipy ._lib ._array_api import (array_namespace , xp_atleast_nd ,
45
45
xp_create_diagonal )
@@ -1031,9 +1031,10 @@ def approx_fprime(xk, f, epsilon=_epsilon, *args):
1031
1031
args = args , f0 = f0 )
1032
1032
1033
1033
1034
+ @_transition_to_rng ("seed" , position_num = 6 , replace_doc = False )
1034
1035
def check_grad (func , grad , x0 , * args , epsilon = _epsilon ,
1035
- direction = 'all' , seed = None ):
1036
- """Check the correctness of a gradient function by comparing it against a
1036
+ direction = 'all' , rng = None ):
1037
+ r """Check the correctness of a gradient function by comparing it against a
1037
1038
(forward) finite-difference approximation of the gradient.
1038
1039
1039
1040
Parameters
@@ -1056,14 +1057,31 @@ def check_grad(func, grad, x0, *args, epsilon=_epsilon,
1056
1057
using `func`. By default it is ``'all'``, in which case, all
1057
1058
the one hot direction vectors are considered to check `grad`.
1058
1059
If `func` is a vector valued function then only ``'all'`` can be used.
1059
- seed : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
1060
- If `seed` is None (or `np.random`), the `numpy.random.RandomState`
1061
- singleton is used.
1062
- If `seed` is an int, a new ``RandomState`` instance is used,
1063
- seeded with `seed`.
1064
- If `seed` is already a ``Generator`` or ``RandomState`` instance then
1065
- that instance is used.
1066
- Specify `seed` for reproducing the return value from this function.
1060
+ rng : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
1061
+ If `rng` is passed by keyword, types other than `numpy.random.Generator` are
1062
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
1063
+ If `rng` is already a ``Generator`` instance, then the provided instance is
1064
+ used. Specify `rng` for repeatable function behavior.
1065
+
1066
+ If this argument is passed by position or `seed` is passed by keyword,
1067
+ legacy behavior for the argument `seed` applies:
1068
+
1069
+ - If `seed` is None (or `numpy.random`), the `numpy.random.RandomState`
1070
+ singleton is used.
1071
+ - If `seed` is an int, a new ``RandomState`` instance is used,
1072
+ seeded with `seed`.
1073
+ - If `seed` is already a ``Generator`` or ``RandomState`` instance then
1074
+ that instance is used.
1075
+
1076
+ .. versionchanged:: 1.15.0
1077
+ As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
1078
+ transition from use of `numpy.random.RandomState` to
1079
+ `numpy.random.Generator`, this keyword was changed from `seed` to `rng`.
1080
+ For an interim period, both keywords will continue to work, although only one
1081
+ may be specified at a time. After the interim period, function calls using the
1082
+ `seed` keyword will emit warnings. The behavior of both `seed` and
1083
+ `rng` are outlined above, but only the `rng` keyword should be used in new code.
1084
+
1067
1085
The random numbers generated with this seed affect the random vector
1068
1086
along which gradients are computed to check ``grad``. Note that `seed`
1069
1087
is only used when `direction` argument is set to `'random'`.
@@ -1106,8 +1124,8 @@ def g(w, func, x0, v, *args):
1106
1124
if _grad .ndim > 1 :
1107
1125
raise ValueError ("'random' can only be used with scalar valued"
1108
1126
" func" )
1109
- random_state = check_random_state (seed )
1110
- v = random_state . normal ( 0 , 1 , size = (x0 .shape ))
1127
+ rng_gen = check_random_state (rng )
1128
+ v = rng_gen . standard_normal ( size = (x0 .shape ))
1111
1129
_args = (func , x0 , v ) + args
1112
1130
_func = g
1113
1131
vars = np .zeros ((1 ,))
0 commit comments