Tensorflow: Delay variable over training steps
<p>In Tensorflow, I want to use some of the variables of my network from the previous training step in the next training step. More specifically, I want to calculate a secondary cost function during training which utilizes some network tensors from the previous training step.</p>

<p>This question could be answered with fragments of RNN code, but I didn't figure out how yet. I was looking into <a href="http://stackoverflow.com/questions/35145645/how-can-i-feed-last-output-yt-1-as-input-for-generating-yt-in-tensorflow-rnn#">How can I feed last output y(t-1) as input for generating y(t) in tensorflow RNN?</a> and <a href="http://stackoverflow.com/questions/39681026/tensorflow-how-to-pass-output-from-previous-time-step-as-input-to-next-timestep">Tensorflow: How to pass output from previous time-step as input to next timestep</a> as well as <a href="http://stackoverflow.com/questions/38241410/tensorflow-remember-lstm-state-for-next-batch-stateful-lstm">TensorFlow: Remember LSTM state for next batch (stateful LSTM)</a>.</p>

<p>Assume h is the last layer of a neural network with several previous layers, e.g.:</p>

<pre><code>h = tf.nn.relu(tf.matmul(h_previous,W_previous))
</code></pre>

<p>How could I preserve the tensor h after processing a sample during training (e.g. save it to h_old), so that I can use it in the next training step for a computation like:</p>

<pre><code>d = tf.sub(h,h_old)
</code></pre>

<p>In this example h is updated with the current training sample and h_old is the tensor which was computed on the previous training sample. Some ideas for this issue would be great!</p>

<p>How about making <code>h_old</code> a variable?</p>

<pre><code>h_old = tf.Variable(tf.zeros(&lt;some-shape&gt;))

.
.

h = tf.nn.relu(tf.matmul(h_previous,W_previous))
d = tf.sub(h,h_old)
h_old.assign(h)
</code></pre>

python tensorflow recurrent-neural-network