Giter Site home page Giter Site logo

Optimization interface about pennylane HOT 6 CLOSED

pennylaneai avatar pennylaneai commented on August 20, 2024
Optimization interface

from pennylane.

Comments (6)

mariaschuld avatar mariaschuld commented on August 20, 2024

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.

co9olguy avatar co9olguy commented on August 20, 2024

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.

smite avatar smite commented on August 20, 2024

I separated the optimize_SGD method from the Optimizer class into a standalone function in 60c5756.

from pennylane.

josh146 avatar josh146 commented on August 20, 2024

@smite I have a couple of questions regarding optimize_SGD.

  1. 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 level openqml.Optimizer.

  2. 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.

smite avatar smite commented on August 20, 2024
  1. 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).

  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.

co9olguy avatar co9olguy commented on August 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.