Tensorflow:用 Python 编写操作 [英] Tensorflow: Writing an Op in Python

查看:39
本文介绍了Tensorflow:用 Python 编写操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Python 编写一个 Op.本教程仅说明如何使用 Python 包装器在 C++ 中执行此操作.https://www.tensorflow.org/versions/master/how_tos/adding_an_op/index.html#adding-a-new-op

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

如何完全用 Python 编写?

How can I write it completely in Python?

推荐答案

您可以使用 <代码>tf.py_func(func, inp, Tout).

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

包装一个 python 函数并将其用作张量流操作.

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

给定一个 python 函数 func,它以 numpy 数组作为输入并返回 numpy 数组作为输出.

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


你的python函数需要有:


Your python function needs to have:

  • numpy 数组作为输入,从带有参数 inp
  • 的图形中输入
  • numpy 数组作为输出,您需要在参数 Tout
  • 中将它们的类型指定给 TensorFlow
  • 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

在函数内部,你可以做任何你想做的事情,如果 for 循环的条件,任何在 TensorFlow 中是不可能的.

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

但是,该操作将在 CPU 上执行,因此它可能比 GPU 中的等效 TensorFlow op 慢.

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

这篇关于Tensorflow:用 Python 编写操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆