问题:tf.function 发出警告并失败 tf.stack

Issue: tf.function raising a warning and failing with tf.stack

我在 tensorflow 中遇到 tf.function 问题。似乎无法转换包含指令 tf.stack().

的函数

我写了一段简单的代码来强调这个问题:

import tensorflow as tf
c = tf.Variable([[1., 5.], [2., 4.]])
@tf.function
def toy_fct(x):
    y = tf.stack([x[0,:], x[1,:]], axis=0)
    return y
toy_fct(c)

我收到的消息如下:

WARNING:tensorflow:AutoGraph could not transform <function toy_fct at 0x000001DC9A13A670> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: module 'gast' has no attribute 'Index'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING: AutoGraph could not transform <function toy_fct at 0x000001DC9A13A670> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: module 'gast' has no attribute 'Index'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert

<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 5.],
       [2., 4.]], dtype=float32)>

有人知道吗?

我已经在 tf 2.0tf 2.4 中测试了您的代码,没有任何问题。但是当我使用 gast==0.4.0 时它出现了。试试这个

pip install --upgrade pip
pip install gast==0.2.2

仅供参考,它在 gast==0.4.0 和最新的 tf 2.5.0-rc0 上运行良好。