i am new to tensorflow C++ API and struggling to find documentation online. this short code performs inner product of two vectors W and x1, compile fine but there are run time errors, i copy the code and error logs here. thanks a lot for the help
我对tensorflow c++ API很陌生,很难在网上找到文档。这段简短的代码执行两个向量W和x1的内积,编译很好但是有运行时错误,我在这里复制代码和错误日志。非常感谢你的帮助
#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
1 个解决方案
#1
0
The error message gives a clue to the problem: neither W
nor x1
is a 2-D matrix—in fact, both are 1-D vectors—and the tensorflow::ops::MatMul()
op requires that both of its arguments are at least 2-dimensional. It does not automatically convert vectors to their matrix representation, and you must do this manually.
错误消息提供了问题的线索:W和x1都不是二维矩阵——事实上,它们都是一维向量——而tensorflow::ops:::MatMul() op要求其两个参数至少是二维的。它不会自动地将向量转换成它们的矩阵表示形式,而且您必须手动这样做。
To solve the problem, specify TensorShape({1, 2})
when you construct W
and TensorShape({2, 1})
when you construct x1
. With these shapes, you should not set MatMul::TransposeA(false)
and MatMul::TransposeB(false)
, or you can omit these options since they are the default values.
要解决这个问题,在构造W时指定TensorShape({1,2}),在构造x1时指定TensorShape({2,1})。对于这些形状,不应该设置MatMul: TransposeA(false)和MatMul:::TransposeB(false),或者可以省略这些选项,因为它们是默认值。
#1
0
The error message gives a clue to the problem: neither W
nor x1
is a 2-D matrix—in fact, both are 1-D vectors—and the tensorflow::ops::MatMul()
op requires that both of its arguments are at least 2-dimensional. It does not automatically convert vectors to their matrix representation, and you must do this manually.
错误消息提供了问题的线索:W和x1都不是二维矩阵——事实上,它们都是一维向量——而tensorflow::ops:::MatMul() op要求其两个参数至少是二维的。它不会自动地将向量转换成它们的矩阵表示形式,而且您必须手动这样做。
To solve the problem, specify TensorShape({1, 2})
when you construct W
and TensorShape({2, 1})
when you construct x1
. With these shapes, you should not set MatMul::TransposeA(false)
and MatMul::TransposeB(false)
, or you can omit these options since they are the default values.
要解决这个问题,在构造W时指定TensorShape({1,2}),在构造x1时指定TensorShape({2,1})。对于这些形状,不应该设置MatMul: TransposeA(false)和MatMul:::TransposeB(false),或者可以省略这些选项,因为它们是默认值。