tfq.optimizers.spsa_minimize
Stay organized with collections
Save and categorize content based on your preferences.
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:
SPSA website
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.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-05-17 UTC.
[null,null,["Last updated 2024-05-17 UTC."],[],[],null,["# tfq.optimizers.spsa_minimize\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/quantum/blob/v0.7.3/tensorflow_quantum/python/optimizers/spsa_minimizer.py#L133-L309) |\n\nApplies the SPSA algorithm. \n\n tfq.optimizers.spsa_minimize(\n expectation_value_function,\n initial_position,\n tolerance=1e-05,\n max_iterations=200,\n alpha=0.602,\n learning_rate=1.0,\n perturb=1.0,\n gamma=0.101,\n blocking=False,\n allowed_increase=0.5,\n seed=None,\n name=None\n )\n\nThe SPSA algorithm can be used to minimize a noisy function. See:\n\n[SPSA website](https://www.jhuapl.edu/SPSA/)\n\n#### Usage:\n\nHere is an example of optimize a function which consists the\nsummation of a few quadratics. \n\n n = 5 # Number of quadratics\n coefficient = tf.random.uniform(minval=0, maxval=1, shape=[n])\n min_value = 0\n func = func = lambda x : tf.math.reduce_sum(np.power(x, 2) * coefficient)\n # Optimize the function with SPSA, start with random parameters\n result = tfq.optimizers.spsa_minimize(func, np.random.random(n))\n result.converged\n tf.Tensor(True, shape=(), dtype=bool)\n result.objective_value\n tf.Tensor(0.0013349084, shape=(), dtype=float32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `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`](https://www.tensorflow.org/api_docs/python/tf/Tensor) Scalar (matching shape `[1]`). |\n| `initial_position` | Real [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/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. |\n| `tolerance` | Scalar [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/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. |\n| `learning_rate` | Scalar [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of real dtype. Specifies the learning rate. |\n| `alpha` | Scalar [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of real dtype. Specifies scaling of the learning rate. |\n| `perturb` | Scalar [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of real dtype. Specifies the size of the perturbations. |\n| `gamma` | Scalar [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of real dtype. Specifies scaling of the size of the perturbations. |\n| `blocking` | Boolean. If true, then the optimizer will only accept updates that improve the objective function. |\n| `allowed_increase` | Scalar [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of real dtype. Specifies maximum allowable increase in objective function (only applies if blocking is true). |\n| `seed` | (Optional) Python integer. Used to create a random seed for the perturbations. |\n| `name` | (Optional) Python `str`. The name prefixed to the ops created by this function. If not supplied, the default name 'minimize' is used. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---------------------|--------------------------------------------------------------------------------|\n| `optimizer_results` | A SPSAOptimizerResults object contains the result of the optimization process. |\n\n\u003cbr /\u003e"]]