View source on GitHub |
Applies the SPSA algorithm.
tfq.optimizers.spsa_minimize(
expectation_value_function,
initial_position,
tolerance=1e-05,
max_iterations=200,
alpha=0.602,
learning_rate=1.0,
perturb=1.0,
gamma=0.101,
blocking=False,
allowed_increase=0.5,
seed=None,
name=None
)
The SPSA algorithm can be used to minimize a noisy function. See:
Usage:
Here is an example of optimize a function which consists the summation of a few quadratics.
n = 5 # Number of quadratics
coefficient = tf.random.uniform(minval=0, maxval=1, shape=[n])
min_value = 0
func = func = lambda x : tf.math.reduce_sum(np.power(x, 2) * coefficient)
# Optimize the function with SPSA, start with random parameters
result = tfq.optimizers.spsa_minimize(func, np.random.random(n))
result.converged
tf.Tensor(True, shape=(), dtype=bool)
result.objective_value
tf.Tensor(0.0013349084, shape=(), dtype=float32)
Args | |
---|---|
expectation_value_function
|
Python callable that accepts a real
valued tf.Tensor with shape [n] where n is the number of function
parameters. The return value is a real tf.Tensor Scalar
(matching shape [1] ).
|
initial_position
|
Real tf.Tensor of shape [n] . The starting
point, or points when using batching dimensions, of the search
procedure. At these points the function value and the gradient
norm should be finite.
|
tolerance
|
Scalar tf.Tensor of real dtype. Specifies the tolerance
for the procedure. If the supremum norm between two iteration
vector is below this number, the algorithm is stopped.
|
learning_rate
|
Scalar tf.Tensor of real dtype.
Specifies the learning rate.
|
alpha
|
Scalar tf.Tensor of real dtype. Specifies scaling of the
learning rate.
|
perturb
|
Scalar tf.Tensor of real dtype. Specifies the size of the
perturbations.
|
gamma
|
Scalar tf.Tensor of real dtype. Specifies scaling of the
size of the perturbations.
|
blocking
|
Boolean. If true, then the optimizer will only accept updates that improve the objective function. |
allowed_increase
|
Scalar tf.Tensor of real dtype. Specifies maximum
allowable increase in objective function (only applies if blocking
is true).
|
seed
|
(Optional) Python integer. Used to create a random seed for the perturbations. |
name
|
(Optional) Python str . The name prefixed to the ops created
by this function. If not supplied, the default name 'minimize'
is used.
|
Returns | |
---|---|
optimizer_results
|
A SPSAOptimizerResults object contains the result of the optimization process. |