GithubHelp home page GithubHelp logo

immarvin / tensorflow-cpp-op-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davidstutz/tensorflow-cpp-op-example

0.0 3.0 0.0 12 KB

Simple example of implementing a new Tensorflow operation and its gradient in C++.

CMake 10.26% Python 50.19% C++ 39.55%

tensorflow-cpp-op-example's Introduction

This is practice code base to add new operator to Tensorflow based on http://davidstutz.de/implementing-tensorflow-operations-in-c-including-gradients/

The Env:

mac OS: Mojave
version: 10.14.2

$ python --version
Python 2.7.15

$ pip show tensorflow
Name: tensorflow
Version: 1.12.0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python2.7/site-packages
Requires: astor, enum34, protobuf, keras-preprocessing, gast, six, wheel, absl-py, backports.weakref, termcolor, tensorboard, numpy, grpcio, mock, keras-applications
Required-by:

$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

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.

Building

The operation is built using CMake 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):

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.

tensorflow-cpp-op-example's People

Contributors

davidstutz avatar immarvin avatar siddhantjain avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.