creating variables on a non-first call to a function decorated with tf.function.
时间: 2023-04-22 09:05:05 浏览: 113
在使用tf.function装饰的函数中,如果在非首次调用时创建变量,会出现什么情况呢?这取决于变量的作用域和生命周期。如果变量是在函数内部定义的局部变量,那么每次调用函数时都会重新创建一个新的变量。如果变量是在函数外部定义的全局变量,那么它的生命周期会跨越多次函数调用,但是在非首次调用时创建全局变量可能会导致意外的结果。因此,建议在函数的第一次调用时创建所有变量,以确保代码的正确性和可重复性。
相关问题
Creating variables on a non-first call to a function decorated with tf.function.
这个问题通常出现在使用 TensorFlow 的 tf.function 时。它是由于在一个被装饰的函数中,会被调用多次且每次调用都会重新创建变量。为了避免这个问题,可以通过在函数内部创建变量的方式来解决它,或者使用 tf.VariableScope。另外,也可以通过将函数的参数作为变量传递来解决这个问题。
tf.function-decorated function tried to create variables on non-first call
这个错误通常是由于在 tf.function 装饰的函数中,尝试在非第一次调用时创建变量所导致的。这是因为 tf.function 会将函数编译成图形,而图形中的变量只能在第一次调用时创建。如果您需要在函数中创建变量,请将其放在函数的外部或使用 tf.Variable()。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![djvu](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)