转换张量流sess.运行到a@tf.功能

2024-03-29 07:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

如何编辑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})

我阅读了文档over here并了解到必须更改如下函数:

^{pr2}$

为此:

def myFunctionToReplaceSessionRun(resized,input_mean,input_std):
    return tf.divide(tf.subtract(resized, [input_mean]), [input_std])

normalized = myFunctionToReplaceSessionRun(resized,input_mean,input_std)

但我不知道如何改变第一个。在

这里有一点上下文,我正在尝试this代码实验室,在这发现{},这给我带来了麻烦。在

This is the command line output when running the ^{} file.

And this is the function that gave errors.


Tags: the函数runinputoutputtimetfmean
1条回答
网友
1楼 · 发布于 2024-03-29 07:28:51

在TensorFlow 1.x中,我们用来创建tf.placeholder张量,通过这些张量数据可以进入图形。我们使用了feed_dict=tf.Session()对象。在

在TensorFlow2.0中,我们可以直接将数据提供给图形,因为默认情况下启用了急切执行。使用@tf.function注释,我们可以直接在图中包含函数。说official docs

At the centre of this merger is tf.function, which allows you to transform a subset of Python syntax into portable, high-performance TensorFlow graphs.

这是一个来自文档的简单例子

@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)

现在,看看你的问题,你可以把你的函数转换成

^{pr2}$

简而言之,占位符张量被转换成函数参数,sess.run( tensor )中的tensor由函数返回。所有这些都发生在@tf.function带注释的函数中。在

相关问题 更多 >