在张量流中,如何计算rnn输出到rnn输入的梯度(当输出和输入来自不同时间步长时)?
我正在使用双向rnn:
((fw_outputs, bw_outputs), (fw_state, bw_state)) = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, inputs, dtype=tf.float32)
# Concat foward and backward outputs
outputs = tf.concat((fw_outputs, bw_outputs), 2)
我想计算从时间步A的rnn输出到时间步B的rnn输入的梯度。例如,我的rnn输入的形状为(batch_size,time_steps,hidden_units),rnn输出的形状为(batch_size,time_steps,2 * hidden_units ),我尝试使用以下代码计算梯度:
### Set input time step to be 1, and output time step to be 2.
### this line return None
grad = tf.gradient(outputs[:,2,:], inputs[:,1,:])
### And I have tried grad_ys(in which only gradient of timestep 2 is 1), it returns zero grad the input of time step 1.
grad = sess.run(tf.gradient(outputs, inputs, grad_ys), feed_dict)
# grad[:,1,:] is zeros, only grad[:,2,:] has some non-zero values.
所以我的问题是如何实现我的目标?