How to make custom Op in TensorFlow importable in Python?

ivan.kavalerov

I have implemented a kernel for my custom Op, and put it into /tensorflow/core/user_ops as custom_op.cc. Inside the Op I do all the registering stuff, like REGISTER_OP and REGISTER_KERNEL_BUILDER.

Then I implemented gradient for this Op in Python, and I put it in the same folder as custom_op_grad.py. I did all the registering here as well (@ops.RegisterGradient).

I have created the BUILD file, with the following content:

load("//tensorflow:tensorflow.bzl", "tf_custom_op_library")
tf_custom_op_library(
        name = "custom_op.so",
        srcs = ["custom_op.cc"],
)

py_library(
        name = "custom_op_grad",
        srcs = ["custom_op_grad.py"],
        srcs_version = "PY2",
        deps = [
        ":custom_op_grad",
        "//tensorflow:tensorflow_py",
        ],
)

After that, I rebuild Tensorflow:

pip uninstall tensorflow
bazel clean
bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
cp -r bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/__main__/* bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-0.8.0-py2-none-any.whl

When I try to use my Op after all this, by calling tf.user_ops.custom_op it tells me that module doesn't have it.

Maybe there are some additional steps I have to do? Or I am doing something wrong with the BUILD file?

ivan.kavalerov

Ok, I found the solution. I just removed the BUILD file, and my custom Op was successfully built and was importable in Python using tensorflow.user_ops.custom_op().

To use the gradient I had to put it's code directly inside the tensorflow/python/user_ops/user_ops.py. Not the most elegant solution, but working for now.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make a custom activation function with only Python in Tensorflow?

From Dev

Tensorflow: How to write op with gradient in python?

From Dev

How to make my library project on GitHub importable via Gradle?

From Dev

How to make your library in Github importable to Android studio

From Dev

How to make my library project on GitHub importable via Gradle?

From Dev

How do I Multiply two matrices in a TensorFlow custom op?

From Dev

Tensorflow: Writing an Op in Python

From Dev

trouble compiling with custom tensorflow gpu op

From Java

how to make custom messagebox in python tkinter?

From Dev

How to make type cast for python custom class

From Dev

How to use distutils or setuptools in setup.py to make a cython extention importable (without needing to append to sys.path before every import)?

From Dev

Python relative import of an importable module not working

From Dev

How to prefetch data using a custom python function in tensorflow

From Dev

How to make keras in R use the tensorflow installed by Python

From Java

How do I create an importable module in Swift?

From Dev

How to create an importable Swift library with swiftc

From Dev

What does setup.py install do to make code importable?

From Dev

Make my .py file importable from any directory

From Dev

How to make a custom container?

From Dev

How to make chocolatey install python2 into custom path?

From Dev

Virtualenv: How to make a custom Python include shared by multicomponents hosted by uWSGI

From Dev

How should I name importable .sqlite persistent store?

From Dev

How to make custom listSelector on custom listView

From Dev

How to make custom listSelector on custom listView

From Dev

how to make custom URL with custom routing in MVC

From Dev

Tensorflow: how to install roi_pooling user_op

From Dev

How to create an "exact match" eval_metric_op for TensorFlow?

From Dev

How to make this custom directive better?

From Dev

How to make custom size of FloatingActionButton

Related Related

  1. 1

    How to make a custom activation function with only Python in Tensorflow?

  2. 2

    Tensorflow: How to write op with gradient in python?

  3. 3

    How to make my library project on GitHub importable via Gradle?

  4. 4

    How to make your library in Github importable to Android studio

  5. 5

    How to make my library project on GitHub importable via Gradle?

  6. 6

    How do I Multiply two matrices in a TensorFlow custom op?

  7. 7

    Tensorflow: Writing an Op in Python

  8. 8

    trouble compiling with custom tensorflow gpu op

  9. 9

    how to make custom messagebox in python tkinter?

  10. 10

    How to make type cast for python custom class

  11. 11

    How to use distutils or setuptools in setup.py to make a cython extention importable (without needing to append to sys.path before every import)?

  12. 12

    Python relative import of an importable module not working

  13. 13

    How to prefetch data using a custom python function in tensorflow

  14. 14

    How to make keras in R use the tensorflow installed by Python

  15. 15

    How do I create an importable module in Swift?

  16. 16

    How to create an importable Swift library with swiftc

  17. 17

    What does setup.py install do to make code importable?

  18. 18

    Make my .py file importable from any directory

  19. 19

    How to make a custom container?

  20. 20

    How to make chocolatey install python2 into custom path?

  21. 21

    Virtualenv: How to make a custom Python include shared by multicomponents hosted by uWSGI

  22. 22

    How should I name importable .sqlite persistent store?

  23. 23

    How to make custom listSelector on custom listView

  24. 24

    How to make custom listSelector on custom listView

  25. 25

    how to make custom URL with custom routing in MVC

  26. 26

    Tensorflow: how to install roi_pooling user_op

  27. 27

    How to create an "exact match" eval_metric_op for TensorFlow?

  28. 28

    How to make this custom directive better?

  29. 29

    How to make custom size of FloatingActionButton

HotTag

Archive