Comments (6)
My idea is to do something like below, and I successfully tested autograd.grad to check if it can do this. This seems to solve part of the parameter issue by using weights and data as positional parameters and only deriving for weights. Or is there a reason we cannot do this with quantum nodes?
class MyGradbasedOptimizer:
def _init_(self, cost, initial_weights ...):
self.grad = autograd.grad(cost, 0) # Computes derivative for first argument, the weights
self.past_gradient = 0 # Keeps track of gradients at past points, i.e. for momentum optimizer
self.global_step = 0 # Keeps track of steps performed with this optimizer
self.weights = initial_weights
...
def step(self, current_weights, current_data):
self.weights = self.grad(current_weights, current_data)
....
def cost(weights, data):
''' weights and data can be any kind of (nested) np.array it seems...'''
# Depends on quantum nodes and classical nodes...
return scalar_cost
o = MyGradbasedOptimizer(cost, initial_weights, ...)
o.step(data)
new_weights = o.weights() ```
from pennylane.
Looks like it does all the basic things we want to do, while keeping things as simple as possible 👍 . I've asked @smite to take a crack at writing a couple slimmed-down optimizers based on the examples in autograd (which acts largely like these in terms of what they do)
from pennylane.
I separated the optimize_SGD method from the Optimizer class into a standalone function in 60c5756.
from pennylane.
@smite I have a couple of questions regarding optimize_SGD
.
-
Is it possible to slim it down even further? For instance, have a very basic function
openqml.SGDOptimizer
, that simply performs a single optimization step given a cost function and initial weights. The user could then use this to define their own optimizer class, or use our provided higher levelopenqml.Optimizer
. -
How come
optimize_SGD
now requires training data? Shouldn't this be able to be called without training data/classification data having to be provided?
from pennylane.
- All nontrivial optimizers need to keep track of internal state variables that typically change at each iteration. In the simple SGD implementation we have now the state consists of the learning rate (which is a function of the current step number and some fixed parameters). More complex optimization algorithms like BFGS keep track of e.g. an approximate Hessian that is updated based on the gradient information over several iterations. The single step function would need to get all this state information as input arguments, and ideally also return every intermediate variable it computes (like the gradient) to the caller so the caller can update the state variables without having to recompute the intermediates. This doesn't seem very helpful, but maybe there's an use case I'm not thinking of?
1b. The current function can also be forced to run just one iteration by using optimize_SGD(max_steps=1).
- As far as I understand SGD always takes a data set as input, otherwise it would be just normal gradient descent. The stochastic part is selecting a random subsample of the data set for each iteration, and passing that to the cost function.
from pennylane.
Yep, the optimizers need to be classes, since they need to keep track of internal state. We want something only slightly more complex than they have in autograd (https://github.com/HIPS/autograd/blob/master/autograd/misc/optimizers.py). Those ones force you to either predict the full number of iterations you want from the start, or risk losing state between multilple calls.
No ML library that I have used has a SGD optimizer. They all have GD optimizers. It is up to the user to decide what to pass this. In future versions, we can build more complex classes, but for the initial release, our goal is to have the basic mathematical functions built-in with minimal further assumptions about what the user might want to do
from pennylane.
Related Issues (20)
- [BUG] Shape mismatch occurs in gradient calculation when using an indexing operation with batched inputs and autograd HOT 1
- [BUG] qml.ThermalRelaxationError argument typo HOT 1
- [BUG] `qml.exp` does not work with `jax.jit` and workflows that require decomposition, since `Exp.decomposition` is not traceable HOT 5
- [BUG] `qml.equal` doesn't work with `PauliError` instances
- [BUG] `single_qubit_fusion` gets rid of the `GlobalPhase`
- [BUG] `BasisRotation` is not JAX compatible HOT 2
- [BUG] `BasisState` does not work with `jax.jit`
- [BUG] `BasisEmbedding` gives the wrong decomposition
- [BUG] `BasisEmbedding` does not work with `lightning.qubit` and `jax.jit` HOT 2
- [BUG] Mid-circuit measurements cannot be added to tapes in transforms HOT 9
- [BUG] jax interface does not support multicontrol gates HOT 1
- [BUG] `qml.qsvt` breaks with some angles
- [BUG] qml.expval returns unexpected values HOT 2
- LinAlgError: Singular matrix when trying to do qml.lie_closure on Heisenberg model
- [BUG] Discrepancy in output values of the example of `qml.pulse.drive` HOT 1
- [BUG] `SPSAOptimizer.step_and_cost` ignores keyword arguments HOT 1
- [BUG] Loading the MQT Bench dataset does not work as expected. HOT 1
- [BUG] Array indexing error when sampling after mid-circuit measurements are performed on other wires HOT 3
- NotImplementedError: VJP of eigvals wrt argnums (0,) not defined[BUG] HOT 2
- Implement `Hermitian.decomposition`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pennylane.