编译 new_op 教程时出错 (Tensorflow)

Error compiling the new_op tutorial (Tensorflow)

我想学习如何install new op。因此,为此我正在按照给定的教程进行操作。我创建了一个名为 user_ops 的文件夹,创建一个 "zero_out.cc" 文件并复制教程中给出的代码。当我尝试将 Op 编译成动态库时出现 g++ 错误:

zero_out.cc: In lambda function: zero_out.cc:10:14: error: ‘Status’ has not been declared return Status::OK(); ^ zero_out.cc: At global scope: zero_out.cc:11:6: error: invalid user-defined conversion from ‘’ to ‘tensorflow::Status ()(tensorflow::shape_inference::InferenceContext)’ [-fpermissive] }); ^ zero_out.cc:8:70: note: candidate is: ::operator void ()(tensorflow::shape_inference::InferenceContext)() const .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) { ^ zero_out.cc:8:70: note: no known conversion from ‘void ()(tensorflow::shape_inference::InferenceContext)’ to ‘tensorflow::Status ()(tensorflow::shape_inference::InferenceContext)’ In file included from zero_out.cc:1:0: /usr/local/lib/python2.7/dist-packages/tensorflow/include/tensorflow/core/framework/op.h:252:30: note: initializing argument 1 of ‘tensorflow::register_op::OpDefBuilderWrapper& tensorflow::register_op::OpDefBuilderWrapper::SetShapeFn(tensorflow::Status ()(tensorflow::shape_inference::InferenceContext))’ OpDefBuilderWrapper& SetShapeFn(<

为什么会这样?我该如何解决?

在我看来,Tensorflow tutorial 没有正确的代码。 所以我遵循了这个 tutorial 的代码,它工作得很好! 我不知道它说的是什么!

假设您唯一的问题是未定义的 Status 类型——并且复制和粘贴教程代码工作正常,除此之外——您需要将 using namespace tensorflow 移动到第一个之前使用 Status,或完全限定它(如 return tensorflow::Status::OK()

例如,如果您使用模板版本,REGISTER_OP 部分可以如下所示:

REGISTER_OP("ZeroOut")
    .Attr("T: {float, int32}")
    .Input("to_zero: T")
    .Output("zeroed: T")
    .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
      c->set_output(0, c->input(0));
      return tensorflow::Status::OK();
    });