【问题标题】:Eigen's AutoDiffJacobian, need some help getting a learning example to workEigen 的 AutoDiffJacobian,需要一些帮助才能获得学习示例
【发布时间】:2016-09-11 10:38:57
【问题描述】:

我一直在使用 Eigen 的 AutoDiffScalar 并取得了很大的成功,现在我想改用 AutoDiffJacobian 而不是我自己做这个。因此,我在学习了 AutoDiffJacobian.h 之后创建了一个学习示例,但出现了问题。

函子:

template <typename Scalar>
struct adFunctor
{
  typedef Eigen::Matrix<Scalar, 3, 1> InputType;
  typedef Eigen::Matrix<Scalar, 2, 1> ValueType;
  typedef Eigen::Matrix<Scalar,
                        ValueType::RowsAtCompileTime,
                        InputType::RowsAtCompileTime> JacobianType;

  enum {
    InputsAtCompileTime = InputType::RowsAtCompileTime,
    ValuesAtCompileTime = ValueType::RowsAtCompileTime
  };

  adFunctor() {}

  size_t inputs() const { return InputsAtCompileTime; }

  void operator() (const InputType &input,
                   ValueType *output) const
  {
    Scalar s1 = Scalar(0), s2 = Scalar(0);

    /* Some operations to test the AD. */
    for (int i = 0; i < 3; i++)
    {
      s1 += log(input(i));
      s2 += sqrt(input(i));
    }

    (*output)(0) = s1;
    (*output)(1) = s2;
  }
};

用法:

Eigen::Matrix<double, 3, 1> in;
in << 1,2,3;
Eigen::Matrix<double, 2, 1> out;
Eigen::AutoDiffJacobian< adFunctor<double> > adjac;
adjac(in, &out);

从这里收到的错误如下:

/usr/include/eigen3/unsupported/Eigen/src/AutoDiff/AutoDiffJacobian.h: In instantiation of ‘void Eigen::AutoDiffJacobian<Functor>::operator()(const InputType&, Eigen::AutoDiffJacobian<Functor>::ValueType*, Eigen::AutoDiffJacobian<Functor>::JacobianType*) const [with Functor = adFunctor<double>; Eigen::AutoDiffJacobian<Functor>::InputType = Eigen::Matrix<double, 3, 1>; Eigen::AutoDiffJacobian<Functor>::ValueType = Eigen::Matrix<double, 2, 1>; Eigen::AutoDiffJacobian<Functor>::JacobianType = Eigen::Matrix<double, 2, 3, 0, 2, 3>]’:
/home/emifre/Git/autodiff-test/src/autodiff_test.cpp:55:17:   required from here
/usr/include/eigen3/unsupported/Eigen/src/AutoDiff/AutoDiffJacobian.h:69:24: error: no matching function for call to ‘Eigen::AutoDiffJacobian<adFunctor<double> >::operator()(Eigen::AutoDiffJacobian<adFunctor<double> >::ActiveInput&, Eigen::AutoDiffJacobian<adFunctor<double> >::ActiveValue*) const’
     Functor::operator()(ax, &av);
     ~~~~~~~~~~~~~~~~~~~^~~~~~~~~
/home/emifre/Git/autodiff-test/src/autodiff_test.cpp:27:8: note: candidate: void adFunctor<Scalar>::operator()(const InputType&, adFunctor<Scalar>::ValueType*) const [with Scalar = double; adFunctor<Scalar>::InputType = Eigen::Matrix<double, 3, 1>; adFunctor<Scalar>::ValueType = Eigen::Matrix<double, 2, 1>]
   void operator() (const InputType &input,
        ^~~~~~~~
/home/emifre/Git/autodiff-test/src/autodiff_test.cpp:27:8: note:   no known conversion for argument 2 from ‘Eigen::AutoDiffJacobian<adFunctor<double> >::ActiveValue* {aka Eigen::Matrix<Eigen::AutoDiffScalar<Eigen::Matrix<double, 3, 1> >, 2, 1, 0, 2, 1>*}’ to ‘adFunctor<double>::ValueType* {aka Eigen::Matrix<double, 2, 1>*}’

从这个错误看来,我在第二次调用 AutoDiffJacobian.h 中的函子时不知何故没有正确的函子类型,但第一次调用它有效。 我希望这里有人知道为什么并可以提供帮助,也许我只是误解了用法。

编辑:显示问题的可编译示例:

#include <Eigen/Dense>
#include <unsupported/Eigen/AutoDiff>

/*
 * Testing differentiation that will produce a Jacobian, using functors and the
 * AutoDiffJacobian helper.
 */

template <typename Scalar>
struct adFunctor
{
  typedef Eigen::Matrix<Scalar, 3, 1> InputType;
  typedef Eigen::Matrix<Scalar, 2, 1> ValueType;
  typedef Eigen::Matrix<Scalar,
                        ValueType::RowsAtCompileTime,
                        InputType::RowsAtCompileTime> JacobianType;

  enum {
    InputsAtCompileTime = InputType::RowsAtCompileTime,
    ValuesAtCompileTime = ValueType::RowsAtCompileTime
  };

  adFunctor() {}

  size_t inputs() const { return InputsAtCompileTime; }

  void operator() (const InputType &input,
                   ValueType *output) const
  {
    Scalar s1 = Scalar(0), s2 = Scalar(0);

    /* Some operations to test the AD. */
    for (int i = 0; i < 3; i++)
    {
      s1 += log(input(i));
      s2 += sqrt(input(i));
    }

    (*output)(0) = s1;
    (*output)(1) = s2;
  }
};



int main(int argc, char *argv[])
{
  Eigen::Matrix<double, 3, 1> in;
  in << 1,2,3;
  Eigen::Matrix<double, 2, 1> out;
  Eigen::AutoDiffJacobian< adFunctor<double> > adjac;
  adjac(in, &out);

  return 0;
}

【问题讨论】:

  • 一一解决编译器错误,这些都很清楚,不要在这里要求为您这样做。请至少发布一个重现问题的minimal reproducible example
  • 我已经添加了一个完整的例子。
  • 我能够解决第一个问题,我的 Google-fu 不在上面。但是仍然有一个错误让我无法理解,AutoDiffJacobian 对函子的第二次调用说函子的类型不匹配,但第一次调用没有问题。一定有我遗漏的使用模式,但我不知道是什么。

标签: c++ eigen eigen3 autodiff


【解决方案1】:

好的,经过大量测试,我让它工作了。 只是我误解了编译器直接说的错误,我缺少运算符本身的模板。

只需要改成:

template <typename T1, typename T2>
void operator() (const T1 &input, T2 *output) const

现在它就像一种享受!我希望比我更多的人使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2022-09-24
    • 1970-01-01
    相关资源
    最近更新 更多