Renamed entry point to startReverseAD to emphasize it is only called once

release/4.3a0
dellaert 2014-10-09 13:42:43 +02:00
parent 563c4d214c
commit 5e5457b390
2 changed files with 13 additions and 9 deletions

View File

@ -74,13 +74,17 @@ struct JacobianTrace {
type = Function; type = Function;
content.ptr = trace; content.ptr = trace;
} }
// Either insert identity into Jacobians (Leaf) or propagate (Function) // *** This is the main entry point for reverseAD, called from Expression::augmented ***
void reverseAD(JacobianMap& jacobians) const { // Called only once, either inserts identity into Jacobians (Leaf) or starts AD (Function)
void startReverseAD(JacobianMap& jacobians) const {
if (type == Leaf) { if (type == Leaf) {
// This branch will only be called on trivial Leaf expressions, i.e. Priors
size_t n = T::Dim(); size_t n = T::Dim();
jacobians[content.key] = Eigen::MatrixXd::Identity(n, n); jacobians[content.key] = Eigen::MatrixXd::Identity(n, n);
} else if (type == Function) } else if (type == Function)
content.ptr->reverseAD(jacobians); // This is the more typical entry point, starting the AD pipeline
// It is inside the startReverseAD that the correctly dimensioned pipeline is chosen.
content.ptr->startReverseAD(jacobians);
} }
// Either add to Jacobians (Leaf) or propagate (Function) // Either add to Jacobians (Leaf) or propagate (Function)
void reverseAD(const Matrix& dTdA, JacobianMap& jacobians) const { void reverseAD(const Matrix& dTdA, JacobianMap& jacobians) const {
@ -109,7 +113,7 @@ struct JacobianTrace {
/// Make sure destructor is virtual /// Make sure destructor is virtual
virtual ~JacobianTrace() { virtual ~JacobianTrace() {
} }
virtual void reverseAD(JacobianMap& jacobians) const = 0; virtual void startReverseAD(JacobianMap& jacobians) const = 0;
virtual void reverseAD(const Matrix& dFdT, JacobianMap& jacobians) const = 0; virtual void reverseAD(const Matrix& dFdT, JacobianMap& jacobians) const = 0;
virtual void reverseAD2(const Jacobian2T& dFdT, virtual void reverseAD2(const Jacobian2T& dFdT,
JacobianMap& jacobians) const = 0; JacobianMap& jacobians) const = 0;
@ -137,7 +141,7 @@ struct Select<2, A> {
//template <class Derived> //template <class Derived>
//struct TypedTrace { //struct TypedTrace {
// virtual void reverseAD(JacobianMap& jacobians) const = 0; // virtual void startReverseAD(JacobianMap& jacobians) const = 0;
// virtual void reverseAD(const Matrix& dFdT, JacobianMap& jacobians) const = 0; // virtual void reverseAD(const Matrix& dFdT, JacobianMap& jacobians) const = 0;
//// template<class JacobianFT> //// template<class JacobianFT>
//// void reverseAD(const JacobianFT& dFdT, JacobianMap& jacobians) const { //// void reverseAD(const JacobianFT& dFdT, JacobianMap& jacobians) const {
@ -424,7 +428,7 @@ public:
JacobianTA dTdA1; JacobianTA dTdA1;
/// Start the reverse AD process /// Start the reverse AD process
virtual void reverseAD(JacobianMap& jacobians) const { virtual void startReverseAD(JacobianMap& jacobians) const {
Select<T::dimension, A1>::reverseAD(trace1, dTdA1, jacobians); Select<T::dimension, A1>::reverseAD(trace1, dTdA1, jacobians);
} }
/// Given df/dT, multiply in dT/dA and continue reverse AD process /// Given df/dT, multiply in dT/dA and continue reverse AD process
@ -515,7 +519,7 @@ public:
JacobianTA2 dTdA2; JacobianTA2 dTdA2;
/// Start the reverse AD process /// Start the reverse AD process
virtual void reverseAD(JacobianMap& jacobians) const { virtual void startReverseAD(JacobianMap& jacobians) const {
Select<T::dimension, A1>::reverseAD(trace1, dTdA1, jacobians); Select<T::dimension, A1>::reverseAD(trace1, dTdA1, jacobians);
Select<T::dimension, A2>::reverseAD(trace2, dTdA2, jacobians); Select<T::dimension, A2>::reverseAD(trace2, dTdA2, jacobians);
} }
@ -625,7 +629,7 @@ public:
JacobianTA3 dTdA3; JacobianTA3 dTdA3;
/// Start the reverse AD process /// Start the reverse AD process
virtual void reverseAD(JacobianMap& jacobians) const { virtual void startReverseAD(JacobianMap& jacobians) const {
Select<T::dimension, A1>::reverseAD(trace1, dTdA1, jacobians); Select<T::dimension, A1>::reverseAD(trace1, dTdA1, jacobians);
Select<T::dimension, A2>::reverseAD(trace2, dTdA2, jacobians); Select<T::dimension, A2>::reverseAD(trace2, dTdA2, jacobians);
Select<T::dimension, A3>::reverseAD(trace3, dTdA3, jacobians); Select<T::dimension, A3>::reverseAD(trace3, dTdA3, jacobians);

View File

@ -120,7 +120,7 @@ public:
typename JacobianTrace<T>::Pointer pointer; typename JacobianTrace<T>::Pointer pointer;
T value = root_->traceExecution(values,pointer); T value = root_->traceExecution(values,pointer);
Augmented<T> augmented(value); Augmented<T> augmented(value);
pointer.reverseAD(augmented.jacobians()); pointer.startReverseAD(augmented.jacobians());
return augmented; return augmented;
#else #else
return root_->forward(values); return root_->forward(values);