Python 将TensorFlow sess.run转换为@tf.function
如何编辑Python 将TensorFlow sess.run转换为@tf.function,python,tensorflow,tensorflow2.0,Python,Tensorflow,Tensorflow2.0,如何编辑会话。运行函数,使其在Tensorflow 2.0上运行 with tf.compat.v1.Session(graph=graph) as sess: start = time.time() results = sess.run(output_operation.outputs[0], {input_operation.outputs[0]: t}) 我阅读了文档,了解到您必须更改如下函数: normalized =
会话。运行函数,使其在Tensorflow 2.0上运行
with tf.compat.v1.Session(graph=graph) as sess:
start = time.time()
results = sess.run(output_operation.outputs[0],
{input_operation.outputs[0]: t})
我阅读了文档,了解到您必须更改如下函数:
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.compat.v1.Session()
result = sess.run(normalized)
return result
为此:
def myFunctionToReplaceSessionRun(resized,input_mean,input_std):
return tf.divide(tf.subtract(resized, [input_mean]), [input_std])
normalized = myFunctionToReplaceSessionRun(resized,input_mean,input_std)
但我不知道如何改变第一个
这里有一点背景,我在尝试代码实验室,在这里发现sess.run
,这给我带来了麻烦
使用TensorFlow 1.x,我们用来创建tf.placeholder
张量,通过它数据可以进入图形。我们使用了feed\u dict=
和tf.Session()
对象
在TensorFlow 2.0中,我们可以直接将数据提供给图形,因为默认情况下启用了急切执行。通过@tf.function
注释,我们可以将函数直接包含在图形中。他说,
这次合并的核心是tf.function,它允许您
将Python语法的子集转换为可移植的高性能
张量流图
下面是一个来自文档的简单示例
@tf.function
def simple_nn_layer(x, y):
return tf.nn.relu(tf.matmul(x, y))
x = tf.random.uniform((3, 3))
y = tf.random.uniform((3, 3))
simple_nn_layer(x, y)
现在,看看你的问题,你可以转换你的函数
@tf.function
def get_output_operation( input_op ):
# The function goes here
# from here return `results`
results = get_output_operation( some_input_op )
简而言之,占位符张量转换为函数参数,sess.run(tensor)
中的tensor
由函数返回。所有这些都发生在@tf.function
注释函数中