如何编辑sessions.run函数,使其在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给我带来了麻烦。
发布于 2019-10-27 04:50:01
使用TensorFlow 1.x,我们使用创建tf.placeholder张量来输入图形。我们使用了feed_dict=和tf.Session()对象。
在TensorFlow 2.0中,我们可以直接将数据提供给图形,因为在默认情况下,急切的执行是启用的。使用@tf.function注释,我们可以将函数直接包含在图中。官方文件说,
这次合并的核心是
tf.function,它允许您将Python语法的子集转换为可移植的、高性能的TensorFlow图。
下面是一个来自文档的简单例子,
@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 )中的sess.run( tensor )。所有这些都发生在带有@tf.function注释的函数中。
https://stackoverflow.com/questions/58573710
复制相似问题