私はtensorflow C++ APIを初めて使用しており、ドキュメント をオンラインで見つけるのに苦労しています。このショートコードは、2つのベクトル Wとx1の内積を実行しますが、コンパイルは正常ですが、実行時にエラーが発生します。 コードをコピーしてエラー をログに記録します。おかげでたくさんの助けをtensorflow無効な引数:[0]は行列ではありません
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main() {
using namespace tensorflow;
using namespace tensorflow::ops;
Tensor W (DT_FLOAT,TensorShape({2}));
Tensor x1(DT_FLOAT,TensorShape({2}));
auto W_map = W .tensor<float,1>();
auto x1_map = x1.tensor<float,1>();
for(int i=0;i<L;++i) {
W_map(i) = -1;
x1_map(i) = 1;
}
std::cout<<"W \n"<<W .flat<float>()<<"\n debug "<<W .DebugString()<<std::endl;
std::cout<<"x1 \n"<<x1.flat<float>()<<"\n debug "<<x1.DebugString()<<std::endl;
Scope root = Scope::NewRootScope();
ClientSession session(root);
// either line of code gives similar run time error
// auto v1 = MatMul(root.WithOpName("v1"), W, x1, MatMul::TransposeA(true));
auto v1 = MatMul(root.WithOpName("v1"), W, x1, MatMul::TransposeB(true));
std::vector<Tensor> o1;
TF_CHECK_OK(session.Run({v1}, &o1));
}
===========================
hweekuans-MacBook-Pro:linear_model hweekuan$ ./linear
W
-1
-1
debug Tensor<type: float shape: [2] values: -1 -1>
x1
1
1
debug Tensor<type: float shape: [2] values: 1 1>
F tensorflow/cc/20170412/linear_model/linear.cc:37] Check failed: ::tensorflow::Status::OK() == (session.Run({v1}, &o1)) (OK vs. Invalid argument: In[0] is not a matrix
[[Node: v1 = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=true, _device="/job:localhost/replica:0/task:0/cpu:0"](Const/Const, Const_1/Const)]])
Abort trap: 6
ありがとうございます。テンソルフローの実装でベクトルが2x1の行列ではないことに気がつきました。 – user7867664