strange behavior of tensorflow decorator @tf.function

168 Views Asked by At

Can somebody explain the behavior of this code:

class Count:
    def __init__(self):
        self.count=0
    def increment(self):
        self.count+=1
        tf.print(self.count)

class SurCount:
    def __init__(self,count):
        self.count=count

    @tf.function 
    def step(self):
        self.count.increment()

    def start(self):
        for i in range(10):
            self.step()

count=Count()
surCount=SurCount(count)
surCount.start()

which returns 1,1,1,1,1... (the counter does not increase).

The next code works : it returns 1,2,3,...

class SurCount:
    def __init__(self,count):
        self.count=count

    @tf.function 
    def start(self):
        for i in range(10):
            self.count.increment()

count=Count()
surCount=SurCount(count)
surCount.start()
0

There are 0 best solutions below