我使用的是tensorflow 2.3
下面的代码
import tensorflow as tf
y_N= tf.Variable([1., 2., 3.],name="dd")
@tf.function
def loss():
return -tf.reduce_mean(input_tensor=tf.reduce_sum(input_tensor=tf.math.log(y_N), axis=0))
@tf.function
def run():
tf.keras.optimizers.Adam(0.5).minimize(loss, var_list=[y_N])
run()
例外
ValueError: tf.function-decorated function tried to create variables on non-first call.
问题看起来像tf.keras.optimizers.Adam(0.5).minimize(loss, var_list=[y_N])
在使用@tf.function
时在〉第一次调用时创建了新变量。如果必须将其 Package 在@tf.function
下,我应该如何修改它?(真实的情况下run()
是一个更大的函数)
3条答案
按热度按时间fwzugrvs1#
我认为这可能与优化器无关。因为使用了函数装饰器,我认为我们需要启用
tf.functions
的渴望执行,如下所示。Source更新
上面的代码运行在渴望模式下会降低性能,要在图形模式下运行,首先创建
optimizers.Adam
类的示例,然后使用类方法,如下所示:一个二个一个一个
您也可以查看this以获得类似的详细信息。
6g8kf2rb2#
恐怕我无法为您提供您可能正在寻找的所有解释,因为
@tf.function
的documentation有些复杂,但根据我使用Tensorflow构建神经网络的经验,只有"运行"过程的子部分必须使用@tf.function
装饰,并且这部分必须在with tf.GradientTape() as tape:
内。如果我这样实现,您的代码将正常工作:lb3vh1jj3#
我发现在调用
minimize
函数之后,它将通过调用self.iterations
一次来创建一个iter
变量。(这发生在tensorflow/python/keras/optimizer_v2/optimizer_v2.py
中)随后调用
self.iterations
将不会创建新变量-因为iter
已经创建。因此,通过调用使其工作,我们需要在外部创建一次
optimizer
,并将其传递给@tf.function.它仍然会在minimize
函数中创建变量,但它只会做一次,因为我们不会重复创建Adam模型。