tensorflow: AttributeError: 'TensorArray' object has no attribute 'mark_used' with tf.function
Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): colab
- TensorFlow version (use command below): v2.3.0-54-gfcc4b966f1 2.3.1
You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with:
Describe the current behavior
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
class Test(tf.Module):
def __init__(self):
self.log_likes_list = None
self.i = tf.constant(0)
@tf.function
def __call__(self, samples):
@tf.function
def rnd():
return tfd.Normal(0,1).sample()+ tfd.Normal(3,1).sample()
if self.log_likes_list is None:
self.log_likes_list = tf.TensorArray(tf.float32, size=samples)
def cond(x,i):
return tf.less(i, samples)
def body(x,i):
#x=x.write(i,tfm.reduce_sum(tfd.Normal(rnd(), 1).log_prob(0.4)))
# AttributeError: 'TensorArray' object has no attribute 'mark_used'
x.write(i,tfm.reduce_sum(tfd.Normal(rnd(), 1).log_prob(0.4))).mark_used()
return x, i+1
self.log_likes_list, i = tf.while_loop(cond, body, [self.log_likes_list, self.i])
self.log_likes = self.log_likes_list.stack()
self.log_like = tfm.reduce_mean(self.log_likes)
loss = self.log_like
return loss
T= Test()
t= T(5)
T.log_likes, T.log_like,
# the code in below is not running
T.log_likes_list.stack()
Describe the expected behavior How can get values in t.log_likes, and t.log_like, and t.log_likes_list?
Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook. https://colab.research.google.com/drive/1ddlIXUBOHd5umTaCW-xghWk3bTOIKhVf#scrollTo=-yj19j0eC_cj
Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (7 by maintainers)
@cheshire FYI
Sorry for the delay. I think the warning about
mark_usedis quite misleading, we’ll need to fix it. You almost never want to use it. The warning itself was meant to caution that you should always assign the result ofx.writeback tox. It’s similar to Pandas, if you’re familiar with it.There is another limitation of tf.function here: when you want to pass values outside the
tf.function, you must either return them, or usetf.Variable. Assigning toself.propertywon’t work properly. This is something we intend to fix, and should have a warning message, but in the mean time, a workaround is to use Variables.I rewrote your code to get it to run without error, see below. A few more notes:
tf.Variablecannot change shape, I used padding forself.log_likestf.while_loopin a shorter form since the function is autographedtf.constant(5)instead of5) to avoid unwanted retracingjit_compileto False since it seems to reveal a bug; I think that will be fixed soon, and I do recommend turning it on when possible because it helps performancernddoesn’t need a separate tf.function - as long as it’s called inside one it will be compiled