Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new autodiff type based on tape representation #89

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

supersega
Copy link
Contributor

Description

This PR introduce new automatic differentiable variable.

Type of change

  • Create tape based storage for fast lookup.
  • Add variable which store their index in tape storage.
  • Overload operators and math function.
  • Create example with new type.

expression - class for tape based representation which supports update.
@supersega supersega mentioned this pull request Nov 30, 2019
5 tasks
Copy link
Member

@allanleal allanleal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @supersega - very cool! I've left some comments and suggestions. I think what we need now is more work on:

  • tests (from simple to complex ones, considering also higher-order derivatives)
  • tutorials
  • a bit of documentation on how the type works.

// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <autodiff/mixed/algorithms/first_order.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For file names, so far we have adopted a no-underline, no-caps convention, meaning, we would use firstorder.hpp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that _ will help to reed file name, but if we prefer such naming as you mentioned I will change.

{
namespace detail
{
/// @brief Meta function to generate tuple which contains derivatives array.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about setting JAVADOC_AUTOBRIEF to YES, so @brief is not needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just arrange about documentation style, and I will do ;)

namespace autodiff::taperep
{
/// @brief Class which represent tape based storage for expression tree.
/// @tparam N order of tape.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say, if N == 2, does this means one can compute 2nd order derivatives, say, Hessian? How do you deal with all possible combinations of cross derivatives, and the symmetry among them (e.g., ∂²u/∂x∂y == ∂²u/∂y∂x)? This looks quite interesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can. I can send publication where algorithm is described.

/// @brief Construct variable from arithmetic types. We need it because Eigen requires it.
/// @details In Eigen code they have check in if statement, where compiler
/// looking for Scalar(0). Our constructor is empty.
template<typename U, std::enable_if_t<std::is_arithmetic_v<U>>...>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will most likely need to change to permit custom floating-point types to be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we must support custom floating point numbers, but we can do it in someone of future PRs.


/// @brief Get value for var.
/// @return Numerical value of variable.
auto value() const -> const double&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double also will need to be templated (either in this PR or in a future one).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it in next PR to avoid monsters =)

/// @brief Binary sum expression.
/// @tparam N Maximal derivative order of expression.
template<std::size_t N>
struct sum_expression final : binary_expression<N>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, sum means a summation of numbers (e.g., sum operator, sum symbol in LaTeX, etc.). I think this should be addition_expression.

/// @brief Binary minus expression.
/// @tparam N Maximal derivative order of expression.
template<std::size_t N>
struct minus_expression final : binary_expression<N>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we should use subtraction_expression if the intent is x - y. I think minus_expression makes sense for the unary operation -x.

/// @brief Update value of divide expression.
/// @details Only first order derivatives are need recalculation,
/// since other are always zero.
virtual void update()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering here if these update methods can deal with branches (e.g., if conditions)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can be introduced in new PR


namespace autodiff::taperep {

constexpr auto first_order = 1; ///< Constant to get first order partial derivatives.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need these two constants? Early on during this review, I found partial_derivatives<first_order>() and my impression was that first_order was a type. partial_derivatives<1>() would have been clearer. But this is a bit subjective. Maybe we could name order1st, order2nd, order3rd, order4th, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like first_order. I can read it like phrase from book.

@@ -1,2 +1,3 @@
add_subdirectory(forward)
add_subdirectory(reverse)
add_subdirectory(mixed)
Copy link
Member

@allanleal allanleal Dec 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning of mixed? What would you propose as alternative names so we can decide between them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can travel in both directions.

@supersega
Copy link
Contributor Author

Hi @allanleal, I want to replace only most serious moments like naming style etc.

What I suggest to move into future PRs?

  • Add examples.
  • Make enable to work with custom numbers. (Probably it will be common check for all autodiff types)
  • Introduce high order derivatives.
  • Documentation.

What I suggest to fix in this PR?

  • File naming.
  • Java style for Doxygen.
  • Add some tests.

@PaulXiCao
Copy link
Contributor

@allanleal There has not been any work on this mr for quite some time. Is this still a wanted feature? If so how can I help? Should I create a copy of this mr and improve on it? Is there a way to split it into smaller chunks? Maybe a milestone with multiple smaller steps in-between which could be iteratively merged?

@ianhbell
Copy link
Contributor

Could you comment on the potential advantages of this new type? I am in particular interested in obtaining all the second and third partial derivatives of a function of two variables in one shot, and wonder whether this would be superior to multiple calls with forward differentiation.

@allanleal
Copy link
Member

Ian, let's talk about this (please email me). I think I have a better solution for you (which we could test first before releasing). I assume your use case is for thermodynamics; and the two variables are perhaps temperature and density?!

@ianhbell
Copy link
Contributor

Correct - email headed your way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants