如何使用 TensorFlow v2 进行数据流编程?
How to use TensorFlow v2 for dataflow programming?
我找不到有关如何使用 TensorFlow v2 进行基本数据流编程的适当文档。我可以在网上找到很多关于 TensorFlow v1 的资源,但他们解释的大部分行为现在都已弃用。例如,以下 python 代码在 TensorFlow v1 中运行良好:
import tensorflow as tf
# Define some helper functions
def func1(a, b):
return a+b # Or any manipulation of the arguments
def func2(a, b):
return a*b
def func3(a, b):
return a-b
# Define a graph; (a,b)-->e, (c,d)-->f, (e,f)-->g
a = tf.placeholder(tf.float64)
b = tf.placeholder(tf.float64)
c = tf.placeholder(tf.float64)
d = tf.placeholder(tf.float64)
e = tf.py_func(func1, [a,b], tf.float64)
f = tf.py_func(func2, [c,d], tf.float64)
g = tf.py_func(func3, [e,f], tf.float64)
# Execute in a session
sess1 = tf.Session()
res = sess1.run([g], feed_dict={a:1, b:2, c:3, d:4})
print(res) # = [-9.0]
这里我把一些python函数转换成了tensorflow操作;我定义了一个图,其中 g
依赖于 e
和 f
而后者又依赖于 a,b,c,d
;最后,我执行了一个会话并为 a,b,c,d
.
提供了输入
我也可以避免提供所有输入,而是选择中间节点:
sess2 = tf.Session()
res = sess2.run([g], feed_dict={a:1, b:2, f:12})
print(res) # = [-9.0]
这里,我已经提供了f
,而不是它的依赖项c,d
。
像这样的东西在 TensorFlow v2 中如何工作(除了导入 tensorflow.compat.v1
)? TensorFlow v2 甚至想过做这样的事情还是只是机器学习?
注意:这与 有某种联系,后者处理更复杂的问题。
据我了解,TF 2.0 的 main motivation 摆脱了先定义所有数据流然后在最后调用它的旧样式。他们想切换到更 pythonic 的做事方式。
其中一个重要的部分是用pythonic方式定义函数,然后用@tf.function
装饰它们,让tensorflow生成一个图,提高性能。
"kitchen sink" 将所有内容放入一个大图中然后 运行 一个会话来满足你的需要的方法在 TF2 中是不鼓励的。
在你的例子中,这看起来像
import tensorflow as tf
# Define some helper functions
def func1(a, b):
return a+b # Or any manipulation of the arguments
def func2(a, b):
return a*b
def func3(a, b):
return a-b
如果你只需要一个大函数,这应该足够了:
# define combined tf function, the decorated ensures a graph is generated.
@tf.function
def tf2_function(a, b, c, d):
e = func1(a, b)
f = func2(c, d)
g = func3(e, f)
return g
e, f, g = tf2_function(1, 2, 3, 4) # -9
但如果您需要获取中间值或提供中间值的选项,那么最好的选择是将函数拆分为两个较小的函数,as is recommended by the TF-developers。
@tf.function
def get_f(a,b,c,d):
e = func1(a, b)
f = func2(c, d)
return f
@tf.function
def get_g_from_e_f(e,f):
g = func3(e,f)
return g
你也不需要用@tf.function
装饰每个函数,通常用它装饰更大、更昂贵的函数就足够了,所有调用它们的函数都会被转换成一个图作为嗯。
但是,在 TF2 中,没有真正的方法可以完成您在 TF1.x 中可以做的事情,即提供不同的值并从图表中获取不同的输出。
我找不到有关如何使用 TensorFlow v2 进行基本数据流编程的适当文档。我可以在网上找到很多关于 TensorFlow v1 的资源,但他们解释的大部分行为现在都已弃用。例如,以下 python 代码在 TensorFlow v1 中运行良好:
import tensorflow as tf
# Define some helper functions
def func1(a, b):
return a+b # Or any manipulation of the arguments
def func2(a, b):
return a*b
def func3(a, b):
return a-b
# Define a graph; (a,b)-->e, (c,d)-->f, (e,f)-->g
a = tf.placeholder(tf.float64)
b = tf.placeholder(tf.float64)
c = tf.placeholder(tf.float64)
d = tf.placeholder(tf.float64)
e = tf.py_func(func1, [a,b], tf.float64)
f = tf.py_func(func2, [c,d], tf.float64)
g = tf.py_func(func3, [e,f], tf.float64)
# Execute in a session
sess1 = tf.Session()
res = sess1.run([g], feed_dict={a:1, b:2, c:3, d:4})
print(res) # = [-9.0]
这里我把一些python函数转换成了tensorflow操作;我定义了一个图,其中 g
依赖于 e
和 f
而后者又依赖于 a,b,c,d
;最后,我执行了一个会话并为 a,b,c,d
.
我也可以避免提供所有输入,而是选择中间节点:
sess2 = tf.Session()
res = sess2.run([g], feed_dict={a:1, b:2, f:12})
print(res) # = [-9.0]
这里,我已经提供了f
,而不是它的依赖项c,d
。
像这样的东西在 TensorFlow v2 中如何工作(除了导入 tensorflow.compat.v1
)? TensorFlow v2 甚至想过做这样的事情还是只是机器学习?
注意:这与
据我了解,TF 2.0 的 main motivation 摆脱了先定义所有数据流然后在最后调用它的旧样式。他们想切换到更 pythonic 的做事方式。
其中一个重要的部分是用pythonic方式定义函数,然后用@tf.function
装饰它们,让tensorflow生成一个图,提高性能。
"kitchen sink" 将所有内容放入一个大图中然后 运行 一个会话来满足你的需要的方法在 TF2 中是不鼓励的。
在你的例子中,这看起来像
import tensorflow as tf
# Define some helper functions
def func1(a, b):
return a+b # Or any manipulation of the arguments
def func2(a, b):
return a*b
def func3(a, b):
return a-b
如果你只需要一个大函数,这应该足够了:
# define combined tf function, the decorated ensures a graph is generated.
@tf.function
def tf2_function(a, b, c, d):
e = func1(a, b)
f = func2(c, d)
g = func3(e, f)
return g
e, f, g = tf2_function(1, 2, 3, 4) # -9
但如果您需要获取中间值或提供中间值的选项,那么最好的选择是将函数拆分为两个较小的函数,as is recommended by the TF-developers。
@tf.function
def get_f(a,b,c,d):
e = func1(a, b)
f = func2(c, d)
return f
@tf.function
def get_g_from_e_f(e,f):
g = func3(e,f)
return g
你也不需要用@tf.function
装饰每个函数,通常用它装饰更大、更昂贵的函数就足够了,所有调用它们的函数都会被转换成一个图作为嗯。
但是,在 TF2 中,没有真正的方法可以完成您在 TF1.x 中可以做的事情,即提供不同的值并从图表中获取不同的输出。