I want to integrate simple python code with in the tensorflow graph.

I'm not sure if it's feasible. If feasible, please suggest how to integrate it.

Use case is, I want feed the output of intermediate tensor as an input to my python code and then python code output to another tensor.

Does @tf.function can be use here?



Yes, theoretically that's what tf.function does. This uses a tensorflow module called AutoGraph to basically convert your python/numpy operations to tensorflow ops.

However not all python operations can be converted. Things like printing, appending to lists and mutating global variables won't work in graph mode!

I suggest you take a look at the official guide to see how it is used and if it is applicable in your situation.

  • $\begingroup$ I just want to perform some Opencv(python method) operations and my machine is GPU machine. Can we say that, along with other execution, that python code will also gets executed by GPU? $\endgroup$ Nov 30 '20 at 11:36
  • $\begingroup$ Yes, but opencv isn't exactly python code. From my understanding, OpenCV is a library written in C++ that has a python API, so it most certainly won't work the way you imagine it. However, there are some CUDA bindings available that might do the trick. However, you'll have to manually build OpenCV instead of just pip installing it. More info here. $\endgroup$
    – Djib2011
    Nov 30 '20 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.