我将以下代码用于标准GRU实施:
def BiRNN_deep_dynamic_FAST_FULL_autolength(x,batch_size,dropout,hidden_dim):
seq_len=length_rnn(x)
with tf.variable_scope('forward'):
lstm_cell_fwd =tf.contrib.rnn.GRUCell(hidden_dim,kernel_initializer=tf.contrib.layers.xavier_initializer(),bias_initializer=tf.contrib.layers.xavier_initializer())
lstm_cell_fwd = tf.contrib.rnn.DropoutWrapper(lstm_cell_fwd, output_keep_prob=dropout)
with tf.variable_scope('backward'):
lstm_cell_back =tf.contrib.rnn.GRUCell(hidden_dim,kernel_initializer=tf.contrib.layers.xavier_initializer(),bias_initializer=tf.contrib.layers.xavier_initializer())
lstm_cell_back = tf.contrib.rnn.DropoutWrapper(lstm_cell_back, output_keep_prob=dropout)
outputs,_= tf.nn.bidirectional_dynamic_rnn(cell_fw=lstm_cell_fwd,cell_bw= lstm_cell_back,inputs=x,sequence_length=seq_len,dtype=tf.float32,time_major=False)
outputs_fwd,outputs_bck=outputs
### fwd matrix is the matrix that keeps all the last [-1] vectors
fwd_matrix=tf.gather_nd(outputs_fwd, tf.stack([tf.range(batch_size), seq_len-1], axis=1)) ### 99,64
outputs_fwd=tf.transpose(outputs_fwd,[1,0,2])
outputs_bck=tf.transpose(outputs_bck,[1,0,2])
return outputs_fwd,outputs_bck,fwd_matrix
Run Code Online (Sandbox Code Playgroud)
谁能提供一个简单的示例来说明如何以类似方式使用tf.contrib.cudnn_rnn.CudnnGRU Cell?只是换出单元格是行不通的。
第一个问题是CuDnnGRU单元没有任何丢弃包装,这很好。其次,它似乎不适用于tf.nn.bidirectional_dynamic_rnn。任何帮助表示赞赏。
我已经在Keras的Github中将这个问题发布为问题,但认为它可能会在这里引起更多的读者。
系统信息
描述当前的行为,
我正在执行Seq2Seq 教程中的代码。我所做的唯一且唯一的更改是将LSTM层替换为CuDNNLSTM。发生的事情是该模型为我提供的任何输入预测了固定的输出。当我运行原始代码时,我得到了明智的结果。
描述预期的行为,
请参见上一节。
代码来重现问题
来自这里。只需用CuDNNLSTM替换LSTM。
任何见解都将不胜感激。