0

C++の線形代数ライブラリーEigenで、"unsupported" に含まれるテンソル計算の部分を用いた際のエラーです。

https://qiita.com/suzuryo3893/items/1a79b4e9410f1803b4fa#geometrica...
にあるような

// サイズ1の新しい次元の導入によるテンソルのランクの増大
Tensor<float, 2> input(7, 11);
array<int, 3> three_dims{{7, 11, 1}};
Tensor<float, 3> result = input.reshape(three_dims);

という記述を真似しようとして、ソースコードを以下のように書きました。

# include "unsupported/Eigen/CXX11/Tensor"
using namespace Eigen;

int main(){
 Tensor<float, 2> input(7, 11);
 Eigen::array<int, 3> three_dims{{7, 11, 1}};
 Tensor<float, 3> result = input.reshape(three_dims);
 return 0;
}

使用しているPCはmac(Mojave)で、コンパイルは以下のように行いました。

g++ -std=c++14 -I (path to Eigen) eigen_practice.cpp -o eigen_practice

しかし、以下のようなエラーが出てしまいます。

error:
no matching member function for call to 'resize'
(中略)
candidate function not viable: requires 0 arguments, but 1 was provided
void resize()
^
1 error generated.

なにかしら、include するべきものが足りていないのでしょうか?

また、"reshape"という関数を呼んでいるはずなのに、なぜ"resize"という関数について問われるのでしょうか?

拙い説明ではありますが、適宜補足させていただきます。
よろしくお願いいたします。

以下がエラーの全文です。

In file included from eigen_practice.cpp:12:
In file included from /Users/yamamototatsuto/Dropbox/include_for_C++/eigen/unsupported/Eigen/CXX11/Tensor:145:
/Users/yamamototatsuto/Dropbox/include_for_C++/eigen/unsupported/Eigen/CXX11/src/Tensor/Tensor.h:397:7: error: 
      no matching member function for call to 'resize'
      resize(TensorEvaluator<const Assign, DefaultDevice>(assign, Defaul...
      ^~~~~~
eigen_practice.cpp:63:28: note: in instantiation of function template
      specialization 'Eigen::Tensor<float, 3, 0,
      long>::Tensor<Eigen::TensorReshapingOp<const std::__1::array<int, 3>,
      Eigen::Tensor<float, 2, 0, long> > >' requested here
        Tensor<float, 3> result = input.reshape(three_dims);
                                  ^
/Users/yamamototatsuto/Dropbox/include_for_C++/eigen/unsupported/Eigen/CXX11/src/Tensor/Tensor.h:423:10: note: 
      candidate function template not viable: no known conversion from 'const
      Eigen::TensorEvaluator<const Eigen::TensorAssignOp<Eigen::Tensor<float, 3,
      0, long>, const Eigen::TensorReshapingOp<const std::__1::array<int, 3>,
      Eigen::Tensor<float, 2, 0, long> > >, Eigen::DefaultDevice>::Dimensions'
      (aka 'const std::__1::array<int, 3>') to 'Eigen::Tensor<float, 3, 0,
      long>::Index' (aka 'long') for 1st argument
    void resize(Index firstDimension, IndexTypes... otherDimensions)
         ^
/Users/yamamototatsuto/Dropbox/include_for_C++/eigen/unsupported/Eigen/CXX11/src/Tensor/Tensor.h:432:28: note: 
      candidate function not viable: no known conversion from
      'array<int, [...]>' to 'const array<long, [...]>' for 1st argument
    EIGEN_DEVICE_FUNC void resize(const array<Index, NumIndices>& dimensions)
                           ^
/Users/yamamototatsuto/Dropbox/include_for_C++/eigen/unsupported/Eigen/CXX11/src/Tensor/Tensor.h:450:28: note: 
      candidate function not viable: no known conversion from 'const
      Eigen::TensorEvaluator<const Eigen::TensorAssignOp<Eigen::Tensor<float, 3,
      0, long>, const Eigen::TensorReshapingOp<const std::__1::array<int, 3>,
      Eigen::Tensor<float, 2, 0, long> > >, Eigen::DefaultDevice>::Dimensions'
      (aka 'const std::__1::array<int, 3>') to 'const
      DSizes<Eigen::Tensor<float, 3, 0, long>::Index, NumIndices>' (aka 'const
      DSizes<long, NumIndices>') for 1st argument
    EIGEN_DEVICE_FUNC void resize(const DSizes<Index, NumIndices>& dimensions) {
                           ^
/Users/yamamototatsuto/Dropbox/include_for_C++/eigen/unsupported/Eigen/CXX11/src/Tensor/Tensor.h:479:10: note: 
      candidate template ignored: could not match 'Sizes' against 'array'
    void resize(const Sizes<Indices...>& dimensions) {
         ^
/Users/yamamototatsuto/Dropbox/include_for_C++/eigen/unsupported/Eigen/CXX11/src/Tensor/Tensor.h:459:10: note: 
      candidate function not viable: requires 0 arguments, but 1 was provided
    void resize()
         ^
1 error generated.
0

1 件の回答 1

3

すいません、以下のURL先に答えがあることを教えて頂きました。
https://stackoverflow.com/questions/56985731/how-to-reshape-a-tensor-in-eigen3

テンソルの形を指定する配列は、整数型ではなくて「テンソルのインデックス」でなくてはいけないようです。
なので

Eigen::array<int, 3> three_dims{{7, 11, 1}};

ではなくて

Eigen::array<Eigen::Index, 3> three_dims{{7, 11, 1}};

と書く必要があるようでした。

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。