How to use Custom OP to build TensorFlow Graph in C++?











up vote
1
down vote

favorite












From TensorFlow documentation, the following can be done to build graph using inherent OP



#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;
Scope root = Scope::NewRootScope();
// Matrix A = [3 2; -1 0]
auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
// Vector b = [3 5]
auto b = Const(root, { {3.f, 5.f} });
// v = Ab^T
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
ClientSession session(root);
// Run and fetch v
TF_CHECK_OK(session.Run({v}, &outputs));
// Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix<float>();
return 0;
}


It seems that MatMul class is auto generated as there is no tensorflow/cc/ops/math_ops.h in the github source code.
How to do the same thing for custom op such as ZeroOut OP from here










share|improve this question


























    up vote
    1
    down vote

    favorite












    From TensorFlow documentation, the following can be done to build graph using inherent OP



    #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;
    Scope root = Scope::NewRootScope();
    // Matrix A = [3 2; -1 0]
    auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
    // Vector b = [3 5]
    auto b = Const(root, { {3.f, 5.f} });
    // v = Ab^T
    auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
    std::vector<Tensor> outputs;
    ClientSession session(root);
    // Run and fetch v
    TF_CHECK_OK(session.Run({v}, &outputs));
    // Expect outputs[0] == [19; -3]
    LOG(INFO) << outputs[0].matrix<float>();
    return 0;
    }


    It seems that MatMul class is auto generated as there is no tensorflow/cc/ops/math_ops.h in the github source code.
    How to do the same thing for custom op such as ZeroOut OP from here










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      From TensorFlow documentation, the following can be done to build graph using inherent OP



      #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;
      Scope root = Scope::NewRootScope();
      // Matrix A = [3 2; -1 0]
      auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
      // Vector b = [3 5]
      auto b = Const(root, { {3.f, 5.f} });
      // v = Ab^T
      auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
      std::vector<Tensor> outputs;
      ClientSession session(root);
      // Run and fetch v
      TF_CHECK_OK(session.Run({v}, &outputs));
      // Expect outputs[0] == [19; -3]
      LOG(INFO) << outputs[0].matrix<float>();
      return 0;
      }


      It seems that MatMul class is auto generated as there is no tensorflow/cc/ops/math_ops.h in the github source code.
      How to do the same thing for custom op such as ZeroOut OP from here










      share|improve this question













      From TensorFlow documentation, the following can be done to build graph using inherent OP



      #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;
      Scope root = Scope::NewRootScope();
      // Matrix A = [3 2; -1 0]
      auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
      // Vector b = [3 5]
      auto b = Const(root, { {3.f, 5.f} });
      // v = Ab^T
      auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
      std::vector<Tensor> outputs;
      ClientSession session(root);
      // Run and fetch v
      TF_CHECK_OK(session.Run({v}, &outputs));
      // Expect outputs[0] == [19; -3]
      LOG(INFO) << outputs[0].matrix<float>();
      return 0;
      }


      It seems that MatMul class is auto generated as there is no tensorflow/cc/ops/math_ops.h in the github source code.
      How to do the same thing for custom op such as ZeroOut OP from here







      c++ tensorflow machine-learning






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 0:14









      tianyapiaozi

      5481516




      5481516
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Take ZeroOut from here as example, you have to do the following



          class ZeroOut {
          public:
          ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x);
          operator ::tensorflow::Output() const { return y; }
          operator ::tensorflow::Input() const { return y; }
          ::tensorflow::Node* node() const { return y.node(); }

          ::tensorflow::Output y;
          };

          ZeroOut::ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x) {
          if (!scope.ok()) return;
          auto _x = ::tensorflow::ops::AsNodeOut(scope, x);
          if (!scope.ok()) return;
          ::tensorflow::Node* ret;
          const auto unique_name = scope.GetUniqueNameForOp("ZeroOut");
          auto builder = ::tensorflow::NodeBuilder(unique_name, "ZeroOut")
          .Input(_x)
          ;
          scope.UpdateBuilder(&builder);
          scope.UpdateStatus(builder.Finalize(scope.graph(), &ret));
          if (!scope.ok()) return;
          scope.UpdateStatus(scope.DoShapeInference(ret));
          this->y = Output(ret, 0);
          }


          Then you can use it to build graph



          Scope root = Scope::NewRootScope();
          // Matrix A = [3 2; -1 0]
          auto A = Const(root, { {3, 2}, {-1, 0} });
          auto v = ZeroOut(root.WithOpName("v"), A);
          std::vector<Tensor> outputs;
          ClientSession session(root);
          // Run and fetch v
          TF_CHECK_OK(session.Run({v}, &outputs));
          LOG(INFO) << outputs[0].matrix<int>();


          Note: For TensorFlow inherent OP, code like ZeroOut class is autogenerated by bazel rule. We can imitate those codes(e.g. tensorflow/cc/ops/math_ops.h) to hand write our own classes if we only have a few custom OPs.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384454%2fhow-to-use-custom-op-to-build-tensorflow-graph-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Take ZeroOut from here as example, you have to do the following



            class ZeroOut {
            public:
            ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x);
            operator ::tensorflow::Output() const { return y; }
            operator ::tensorflow::Input() const { return y; }
            ::tensorflow::Node* node() const { return y.node(); }

            ::tensorflow::Output y;
            };

            ZeroOut::ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x) {
            if (!scope.ok()) return;
            auto _x = ::tensorflow::ops::AsNodeOut(scope, x);
            if (!scope.ok()) return;
            ::tensorflow::Node* ret;
            const auto unique_name = scope.GetUniqueNameForOp("ZeroOut");
            auto builder = ::tensorflow::NodeBuilder(unique_name, "ZeroOut")
            .Input(_x)
            ;
            scope.UpdateBuilder(&builder);
            scope.UpdateStatus(builder.Finalize(scope.graph(), &ret));
            if (!scope.ok()) return;
            scope.UpdateStatus(scope.DoShapeInference(ret));
            this->y = Output(ret, 0);
            }


            Then you can use it to build graph



            Scope root = Scope::NewRootScope();
            // Matrix A = [3 2; -1 0]
            auto A = Const(root, { {3, 2}, {-1, 0} });
            auto v = ZeroOut(root.WithOpName("v"), A);
            std::vector<Tensor> outputs;
            ClientSession session(root);
            // Run and fetch v
            TF_CHECK_OK(session.Run({v}, &outputs));
            LOG(INFO) << outputs[0].matrix<int>();


            Note: For TensorFlow inherent OP, code like ZeroOut class is autogenerated by bazel rule. We can imitate those codes(e.g. tensorflow/cc/ops/math_ops.h) to hand write our own classes if we only have a few custom OPs.






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              Take ZeroOut from here as example, you have to do the following



              class ZeroOut {
              public:
              ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x);
              operator ::tensorflow::Output() const { return y; }
              operator ::tensorflow::Input() const { return y; }
              ::tensorflow::Node* node() const { return y.node(); }

              ::tensorflow::Output y;
              };

              ZeroOut::ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x) {
              if (!scope.ok()) return;
              auto _x = ::tensorflow::ops::AsNodeOut(scope, x);
              if (!scope.ok()) return;
              ::tensorflow::Node* ret;
              const auto unique_name = scope.GetUniqueNameForOp("ZeroOut");
              auto builder = ::tensorflow::NodeBuilder(unique_name, "ZeroOut")
              .Input(_x)
              ;
              scope.UpdateBuilder(&builder);
              scope.UpdateStatus(builder.Finalize(scope.graph(), &ret));
              if (!scope.ok()) return;
              scope.UpdateStatus(scope.DoShapeInference(ret));
              this->y = Output(ret, 0);
              }


              Then you can use it to build graph



              Scope root = Scope::NewRootScope();
              // Matrix A = [3 2; -1 0]
              auto A = Const(root, { {3, 2}, {-1, 0} });
              auto v = ZeroOut(root.WithOpName("v"), A);
              std::vector<Tensor> outputs;
              ClientSession session(root);
              // Run and fetch v
              TF_CHECK_OK(session.Run({v}, &outputs));
              LOG(INFO) << outputs[0].matrix<int>();


              Note: For TensorFlow inherent OP, code like ZeroOut class is autogenerated by bazel rule. We can imitate those codes(e.g. tensorflow/cc/ops/math_ops.h) to hand write our own classes if we only have a few custom OPs.






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Take ZeroOut from here as example, you have to do the following



                class ZeroOut {
                public:
                ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x);
                operator ::tensorflow::Output() const { return y; }
                operator ::tensorflow::Input() const { return y; }
                ::tensorflow::Node* node() const { return y.node(); }

                ::tensorflow::Output y;
                };

                ZeroOut::ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x) {
                if (!scope.ok()) return;
                auto _x = ::tensorflow::ops::AsNodeOut(scope, x);
                if (!scope.ok()) return;
                ::tensorflow::Node* ret;
                const auto unique_name = scope.GetUniqueNameForOp("ZeroOut");
                auto builder = ::tensorflow::NodeBuilder(unique_name, "ZeroOut")
                .Input(_x)
                ;
                scope.UpdateBuilder(&builder);
                scope.UpdateStatus(builder.Finalize(scope.graph(), &ret));
                if (!scope.ok()) return;
                scope.UpdateStatus(scope.DoShapeInference(ret));
                this->y = Output(ret, 0);
                }


                Then you can use it to build graph



                Scope root = Scope::NewRootScope();
                // Matrix A = [3 2; -1 0]
                auto A = Const(root, { {3, 2}, {-1, 0} });
                auto v = ZeroOut(root.WithOpName("v"), A);
                std::vector<Tensor> outputs;
                ClientSession session(root);
                // Run and fetch v
                TF_CHECK_OK(session.Run({v}, &outputs));
                LOG(INFO) << outputs[0].matrix<int>();


                Note: For TensorFlow inherent OP, code like ZeroOut class is autogenerated by bazel rule. We can imitate those codes(e.g. tensorflow/cc/ops/math_ops.h) to hand write our own classes if we only have a few custom OPs.






                share|improve this answer












                Take ZeroOut from here as example, you have to do the following



                class ZeroOut {
                public:
                ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x);
                operator ::tensorflow::Output() const { return y; }
                operator ::tensorflow::Input() const { return y; }
                ::tensorflow::Node* node() const { return y.node(); }

                ::tensorflow::Output y;
                };

                ZeroOut::ZeroOut(const ::tensorflow::Scope& scope, ::tensorflow::Input x) {
                if (!scope.ok()) return;
                auto _x = ::tensorflow::ops::AsNodeOut(scope, x);
                if (!scope.ok()) return;
                ::tensorflow::Node* ret;
                const auto unique_name = scope.GetUniqueNameForOp("ZeroOut");
                auto builder = ::tensorflow::NodeBuilder(unique_name, "ZeroOut")
                .Input(_x)
                ;
                scope.UpdateBuilder(&builder);
                scope.UpdateStatus(builder.Finalize(scope.graph(), &ret));
                if (!scope.ok()) return;
                scope.UpdateStatus(scope.DoShapeInference(ret));
                this->y = Output(ret, 0);
                }


                Then you can use it to build graph



                Scope root = Scope::NewRootScope();
                // Matrix A = [3 2; -1 0]
                auto A = Const(root, { {3, 2}, {-1, 0} });
                auto v = ZeroOut(root.WithOpName("v"), A);
                std::vector<Tensor> outputs;
                ClientSession session(root);
                // Run and fetch v
                TF_CHECK_OK(session.Run({v}, &outputs));
                LOG(INFO) << outputs[0].matrix<int>();


                Note: For TensorFlow inherent OP, code like ZeroOut class is autogenerated by bazel rule. We can imitate those codes(e.g. tensorflow/cc/ops/math_ops.h) to hand write our own classes if we only have a few custom OPs.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 at 10:40









                tianyapiaozi

                5481516




                5481516






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384454%2fhow-to-use-custom-op-to-build-tensorflow-graph-in-c%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen