Hi, I’m trying to manipulate a tensor as an object’s attribute, I’m only using tf.function’s return value here. All assignments are outside the function, I don’t think I’m violating any rules here. But the result is clearly not as expected.
class Holder: def __init__(self): self.v = tf.zeros([]) @tf.function def add(holder): return holder.v + 1 h = Holder() l = [] for _ in range(10): h.v = add(h) l.append(h.v) print(l)
Result:
[<tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>]
How can I achieve this correctly without explicitly passing the tensor itself as the args of the function which would be quite a long list? I’m just using an object to pack args.
Thank you.