I am implementing simple RNN. In it I want to return outputs of each timestep in list which later I can feed it to optimizer. I have built working rnn without @tf.function. But after adding @tf.function it gives problem
def basic_rnn_cell(self,x,s):#Note :These function are defined in class
s=self.U*x+self.W*s+self.b #U,W,b and all are tf.Variable
y=self.V*s+self.c
return y,s
@tf.function
def rnn(self,X):
outputs=[]
state=self.state
for x in X:
output,state=self.basic_rnn_cell(x,state)
outputs.append(output)
return outputs
This is how I call :
x=np.array([0.01,0.02,0.03],dtype=np.float32)
o.rnn(x)
The error I get :
raise errors.InaccessibleTensorError(
tensorflow.python.framework.errors_impl.InaccessibleTensorError: The tensor 'Tensor("while/add_2:0", shape=(), dtype=float32)' cannot be accessed here: it is defined in another function or code block.
Use return values, explicit Python locals or TensorFlow collections to access it. Defined in: FuncGraph(name=while_body_44, id=2538759416224); accessed from: FuncGraph(name=rnn, id=2538758824096).
This is because using python list for temporary saving tensor object.Memory recycle mechanism will delete what you saved after this function was traced so this can't be achieved.
If you want save those temporary tensors,you have to use tf.TensorArray as a replacement.You can refer to this:https://www.tensorflow.org/guide/function#loops
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With