質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

3298閲覧

Eigen で テンソル を扱おうとしていますが、reshape が使えません

ham_catu

総合スコア12

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2019/07/13 05:45

Eigen で テンソル を扱おうとしていますが、reshape が使えません

C++の線形代数ライブラリーEigenで、"unsupported" に含まれるテンソル計算の部分を用いた際のエラーです。Eigen のドキュメント(の日本語訳版)に記載されていた

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

というという記述を真似してみたのですが、同じようなコードを書いているのにコンパイルが通りません。

なにかしら、include するべきものが足りていないのでしょうか?
また、"reshape"という関数を呼んでいるはずなのに、エラーメッセージでは"resize"という関数について問われています。それはどうしてでしょうか?

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

発生しているエラーメッセージ

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.

該当のソースコード

C++

1# include "unsupported/Eigen/CXX11/Tensor" 2using namespace Eigen; 3 4int main(){ 5 Tensor<float, 2> input(7, 11); 6 Eigen::array<int, 3> three_dims{{7, 11, 1}}; 7 Tensor<float, 3> result = input.reshape(three_dims); 8 return 0; 9}

補足情報(FW/ツールのバージョンなど)

以下がエラーメッセージの全文です。使っているPCはMacです。

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.

参考にしたコードは
「Eigen Tensors ドキュメント日本語訳」というサイトのものです。
https://qiita.com/suzuryo3893/items/1a79b4e9410f1803b4fa#geometrica

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

すいません、以下の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}};

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

投稿2019/07/16 02:24

ham_catu

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問