Skip to content

Optimizers in the C++ API #9837

Closed
Closed

Description

@theflofly

There is currently no Optimizer in the C++ API compared as the ones that we can find in the Python API.

It means, when using only the C++ API that we have to manually collect the gradients and apply them.

Don't you think it should be a convenient add? Retrieving the gradients and applying them is kind of hard and error prone. If yes, I could work on it and create an Optimizer + GradientDescentOptimizer in the C++ API.

I maybe missed something.

Activity

  1. drpngx commented on May 11, 2017

    @drpngx
    Contributor

    I would recommend working with us to define the interface and design that looks like gradients. That would be a nice contribution!

    Ideally, we would have a check that things don't go out of sync with python.

  2. asimshankar commented on May 11, 2017

    @asimshankar
    Contributor

    Yes, the C++ (and other language) APIs lack many of the higher level Python conveniences right now.
    (Side note: The gradient registry for all operations in C++ is not as comprehensive as Python, so there is work to be done there - for details see the link that @drpngx pointed to).

    The TensorFlow team doesn't have the cycles to pursue this ourselves right now, but if you'd like to give it a shot we'd be happy to discuss any interfaces/design that you propose.

    FYI @suharshs @skye @josh11b

  3. added
    stat:contribution welcome
    type:feature
    on May 11, 2017
  4. theflofly commented on May 11, 2017

    @theflofly
    ContributorAuthor

    Ok. I'll see how I can do things and I'll make proposals here. I'll surely contribute to the C++ gradients as you pointed it out.

  5. removed their assignment
    on May 12, 2017
  6. theflofly commented on May 24, 2017

    @theflofly
    ContributorAuthor

    My observations until now.

    In python the Optimizer class provides a minimize method that calls compute_gradient and apply_gradient. compute_gradient adds the gradient ops to the graph, apply_gradient adds the apply ops to the graph and return an operation that applies the gradients.

    The mains files are:

    • optimizer.py
    • gradient_descent.py
    • gradients_impl.py

    In C++ some gradient operations are implemented. There is also a SymbolicGradientBuilder that applies the gradient operations to the graph. The missing part is the applying of the gradients.

    The steps would be:

    • Create the Optimizer and GradientDescentOptimizer class
    • Use the SymbolicGradientBuilder in the ComputeGradient method
    • Add apply operations to the graph and return an operation in the ApplyGradient method
    • Combine both methods in the minimize method
    • Unit tests

    What do you think?

  7. AhmetCanSolak commented on Jun 22, 2017

    @AhmetCanSolak

    Hi @theflofly ! Your steps sounds reasonable in general. I would like to work on that but need guidance to start. Please contact if you are still interested in.

  8. theflofly commented on Jun 22, 2017

    @theflofly
    ContributorAuthor

    Hi @AhmetCanSolak, I am working on it and I made good progress even if it is not as easy as it sounds.

    I don't see a way to split the task unfortunately. Especially at this point. Once the optimizer part is added, there will be plenty of work. Namely adding all the Optimizers (adam, adagrad, momentum, etc.). If you want I can ping you at this time and we'll share the work.

    In the meantime, there is always the gradient operations that need to be ported.

  9. AhmetCanSolak commented on Jun 22, 2017

    @AhmetCanSolak

    Great. It is better idea to ping me when sharing tasks possible. Meanwhile I better check again the contribution guidelines since I have not contributed to TF yet.

  10. Moriadry-zz commented on Jun 25, 2017

    @Moriadry-zz

    @theflofly Do you have any ideas about bringing automatic differentiation (gradients) to C++ API?
    also, If any help needed, you can ping me:)

  11. theflofly commented on Jun 25, 2017

    @theflofly
    ContributorAuthor

    @moriadry AD is already here.

    std::vector<Output> grad_outputs;
    TF_ASSERT_OK( AddSymbolicGradients(scope, {z}, {x, y}, {dz}, &grad_outputs));
    

    Will add the gradient operations to the graph. You can find more example here. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/cc/framework/gradients_test.cc

    The goal of this issue is mainly about the update part. If you want to add some gradients you can follow these guidelines.

    Does this answer?

  12. Moriadry-zz commented on Jun 26, 2017

    @Moriadry-zz

    @theflofly ok, your answer looks like making sense, I will try to figure that out. thank you!

  13. theflofly commented on Jul 6, 2017

    @theflofly
    ContributorAuthor

    Hello, this is a message to keep you updated. I think that I have a working version, I'll soon share the code. I still have a TODO list of minor things to do.

  14. 12 remaining items

  15. theflofly commented on Dec 4, 2017

    @theflofly
    ContributorAuthor

    @GedisJ For now I am working on migrating the gradients from Python to C++ because one Operation without gradient in the graph means an untrainable network.
    Because the node ​ApplyAdam already exists I think that adding Adam is not that hard. I'll take a look and keep you updated.

  16. gedas256 commented on Dec 4, 2017

    @gedas256

    @theflofly this is true about gradients, I had to add some simple gradient operations myself, before they were introduced in 1.4 release.
    regarding Adam the main thing I guess is adding slots implementation to base Optimizer from what I've seen in Python code, because in C++ this seems to be missing too.

  17. epignatelli commented on Dec 2, 2018

    @epignatelli

    Reviving this old thread in connection with #11377.
    Is there any plan / road map publicly available to keep track and contribute to this?

  18. theflofly commented on Dec 8, 2018

    @theflofly
    ContributorAuthor

    I would also be interested.

    The work I began to do was in the end discarded and I never heard back from someone at Google about working on a design...

  19. asimshankar commented on Dec 11, 2018

    @asimshankar
    Contributor

    Thanks for reviving this thread.

    The sad news is that someone from Google hasn't worked on a design.
    The good news is that we've gotten better in terms of establishing a process so that design changes can be made without constantly waiting on someone at Google to drive it!

    If there is interest in pushing this, then it would be great for the person interested to drive the TensorFlow RFC process, which involves writing a design proposal as a first step. This will allow the community and maintainers to see the full picture and make sure it's headed in the right direction, instead of having the design evolve through a series of one-off PRs.

    I personally feel the ideal outcome would be that we'd have a path so that individual optimizer classes implemented in Python can be migrated to C++ so that the Python class becomes a trivial wrapper over the functionality exposed by the C API instead of having to duplicate logic in two languages. The work earlier this year to get the Python graph building libraries to be wrappers over the C API for graph construction should make this much easier than it would have been last year.

    So, long story short: No plan/roadmap available yet, but contributions are welcome to make them! :)

    FYI @ewilderj

  20. theflofly commented on Feb 20, 2019

    @theflofly
    ContributorAuthor

    Thanks for your answer @asimshankar, in the case of a RFC process do you accept being the RFC sponsor?

  21. ewilderj commented on Feb 20, 2019

    @ewilderj
    Contributor
  22. asimshankar commented on Feb 20, 2019

    @asimshankar
    Contributor

    @theflofly : I would have loved to have been, but I've since moved to other projects (and am no longer involved with TensorFlow on a daily basis) and am thus not a good person for this at the moment.

    @ewilderj / @karmel should be able to find an appropriate sponsor.

  23. karmel commented on Feb 21, 2019

    @karmel

    Thanks @theflofly for keeping on us about this. If you put together an RFC for review, I can help as the sponsor to find appropriate reviewers, etc. (CC @alextp , @skye who might be good reviewers here)

  24. theflofly commented on Feb 25, 2019

    @theflofly
    ContributorAuthor

    Thanks @asimshankar, @karmel.

    I will work on something then, but bear with me I am quite busy at the moment.

  25. cj741 commented on Dec 4, 2019

    @cj741

    Hi @theflofly , How is work doing? We also need the GradientDescentOptimizer in our project, but I didn't find it in the TF 2.0, hope your work merge into the TF.

  26. github-actions commented on Mar 28, 2023

    @github-actions

    This issue is stale because it has been open for 180 days with no activity. It will be closed if no further activity occurs. Thank you.

  27. added
    stale
    on Mar 28, 2023
  28. github-actions commented on Mar 29, 2024

    @github-actions

    This issue was closed because it has been inactive for 1 year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stalestat:contribution welcometype:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions