I am converting my code to Tensorflow v2 and I keep getting the following error:
AssertionError: Called a function referencing variables which have been deleted. This likely means that function-local variables were created and not referenced elsewhere in the program. This is generally a mistake; consider storing variables in an object attribute on first call.
Here is a minimal example that reproduces the error
import tensorflow as tf
class TEST:
def __init__(self, a=1):
self.a = tf.Variable(a)
@tf.function
def increment(self):
self.a = self.a + 1
return self.a
tst = TEST()
tst.increment()
How should I fix this?