Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: Writing an Op in Python

Tags:

tensorflow

I would like to write an Op in Python. This tutorial only explains how to do it in c++ with a Python wrapper. https://www.tensorflow.org/versions/master/how_tos/adding_an_op/index.html#adding-a-new-op

How can I write it completely in Python?

like image 359
Ray.R.Chua Avatar asked Jun 20 '16 13:06

Ray.R.Chua


1 Answers

You can use tf.py_func(func, inp, Tout).

Wraps a python function and uses it as a tensorflow op.

Given a python function func, which takes numpy arrays as its inputs and returns numpy arrays as its outputs.


Your python function needs to have:

  • numpy arrays as inputs, fed from the graph with the argument inp
  • numpy arrays as outputs, you need to specify their types to TensorFlow in the argument Tout

Inside the function, you can do whatever you like, if conditions of for loops, anything that is not possible in TensorFlow.


However, the operation will be executed on CPU so it may be slower than the equivalent TensorFlow op in GPU.

like image 102
Olivier Moindrot Avatar answered Sep 22 '22 20:09

Olivier Moindrot