Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidstutz/tensorflow-cpp-op-example
Simple example of implementing a new Tensorflow operation and its gradient in C++.
https://github.com/davidstutz/tensorflow-cpp-op-example
tensorflow tensorflow-operation
Last synced: 4 days ago
JSON representation
Simple example of implementing a new Tensorflow operation and its gradient in C++.
- Host: GitHub
- URL: https://github.com/davidstutz/tensorflow-cpp-op-example
- Owner: davidstutz
- Created: 2017-02-04T16:25:07.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-28T15:06:13.000Z (over 5 years ago)
- Last Synced: 2023-03-27T16:44:45.529Z (over 1 year ago)
- Topics: tensorflow, tensorflow-operation
- Language: Python
- Size: 8.79 KB
- Stars: 57
- Watchers: 6
- Forks: 23
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Example of Tensorflow Operation in C++
This repository contains an example of a simple Tensorflow operation and its gradient both implemented in C++, as described in [this article](http://davidstutz.de/implementing-tensorflow-operations-in-c-including-gradients/).
## Building
The operation is built using [CMake](https://cmake.org/) and requires an appropriate version of Tensorflow to be installed. In order to get the necessary include directories containing the Tensorflow header files, the following trick is used (also see the [Tensorflow documentation](https://www.tensorflow.org/how_tos/adding_an_op/)):
import tensorflow
print(tensorflow.sysconfig.get_include())In the `CMakeLists.txt` this is used as follows:
execute_process(COMMAND python3 -c "import tensorflow; print(tensorflow.sysconfig.get_include())" OUTPUT_VARIABLE Tensorflow_INCLUDE_DIRS)
The remaining contents are pretty standard. Building is now done using:
$ mkdir build
$ cd build
$ cmake ..
$ make
Scanning dependencies of target inner_product
[ 50%] Building CXX object CMakeFiles/inner_product.dir/inner_product.cc.o
Linking CXX shared library libinner_product.so
[ 50%] Built target inner_product
Scanning dependencies of target inner_product_grad
[100%] Building CXX object CMakeFiles/inner_product_grad.dir/inner_product_grad.cc.o
Linking CXX shared library libinner_product_grad.so
[100%] Built target inner_product_grad`libinner_product.so` and `libinner_product_grad.so` can be found in `build` and need to be included in order to load the module in Python:
import tensorflow as tf
inner_product_module = tf.load_op_library('build/libinner_product.so')See `inner_product_tests.py` for usage examples.
## License
Copyright (c) 2016 David Stutz
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.