图中的使用"/>
tfe 在静态图中的使用
原文链接: tfe 在静态图中的使用
上一篇: tfe mnist 使用dataset 分类 保存和载入
下一篇: tfe keras 任意大小的灰度转换
通过tf.contrib.eager.py_func可以在静态图运行中使用tfe
参数分别为:
1,用于调用的函数
2,输入的参数,是一个列表
3,输出类型
4,名称
tf.contrib.eager.py_func(func,inp,Tout,name=None
)
在自定义函数中获取数值,并将其求和后返回
完整代码
import tensorflow as tfdef my_py_func(x):# print(x) # but it's eager!# return 1for i in x:print(i)# tf.Tensor(1.0, shape=(), dtype=float32)# tf.Tensor(2.0, shape=(), dtype=float32)# tf.Tensor(3.0, shape=(), dtype=float32)sum = tf.reduce_sum(x)sum = tf.reduce_sum(x, axis=0) # sum tf.Tensor(6.0, shape=(), dtype=float32)return sumx = tf.Variable([1., 2., 3.])print(x) # <tf.Variable 'Variable:0' shape=(3,) dtype=float32_ref>
with tf.Session() as sess:sess.run(tf.global_variables_initializer())y = tf.contrib.eager.py_func(func=my_py_func, inp=[x], Tout=tf.float32)print(y, y.eval()) # Tensor("EagerPyFunc:0", dtype=float32) 6.0
更多推荐
tfe 在静态图中的使用
发布评论