Tensorflow 如何在tensforflow 2.0中替换OutputProjectionWrapper

Tensorflow 如何在tensforflow 2.0中替换OutputProjectionWrapper,tensorflow,keras,tensorflow2.0,Tensorflow,Keras,Tensorflow2.0,我有以下带有注意机制的seq2seq解码器的代码片段。它在tensorflow 1.13中工作。现在我需要用keras升级到tensorflow 2.0,但是tensorflow 2.0中已经删除了tf.contrib.rnn.OutputProjectionWrapper。如何实施 attention_mechanism = tf.contrib.seq2seq.BahdanauAttention( num_units, memory=memory,

我有以下带有注意机制的seq2seq解码器的代码片段。它在tensorflow 1.13中工作。现在我需要用keras升级到tensorflow 2.0,但是tensorflow 2.0中已经删除了tf.contrib.rnn.OutputProjectionWrapper。如何实施

attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(
                num_units, memory=memory,
         memory_sequence_length=self.encoder_inputs_actual_length)
cell = tf.contrib.rnn.LSTMCell(num_units)
attn_cell = tf.contrib.seq2seq.AttentionWrapper(
                cell, attention_mechanism, attention_layer_size)
out_cell = tf.contrib.rnn.OutputProjectionWrapper(
                attn_cell, self.output_size, reuse=reuse)
decoder = tf.contrib.seq2seq.BasicDecoder(
                cell=out_cell, helper=helper,
                initial_state=out_cell.zero_state(
                    dtype=tf.float32, batch_size=self.batch_size))
final_outputs, _, _ = tf.contrib.seq2seq.dynamic_decode(
                decoder=decoder, output_time_major=True,
                impute_finished=True, 
maximum_iterations=self.input_steps
            )
我读了这篇文章,但没有弄清楚如何在我的案例中添加完整的连接

我试着使用最新的seq2seq插件和如下所示的急切模式,没有语法错误,但我不确定它是否正确。以前的tf 1.13版本预测准确率快速达到90%,而新的tf2.0版本预测准确率始终在60%左右

attention_mechanism = tfa.seq2seq.BahdanauAttention(num_units,memory,memory_sequence_length)
lstm_cell = layers.LSTMCell(num_units)
attn_cell = tfa.seq2seq.AttentionWrapper(lstm_cell,attention_mechanism, attention_layer_size=num_units) 
output_layer = layers.Dense(self.output_size)
basic_decoder = tfa.seq2seq.BasicDecoder(cell=attn_cell, sampler=sampler,output_layer=output_layer,output_time_major=True,impute_finished=True,maximum_iterations=self.input_steps)
initial_state = attn_cell.get_initial_state(batch_size=self.batch_size,dtype=tf.float32).clone(cell_state=encoder_final_state)
final_outputs, _, _ = basic_decoder(encoder_outputs_sequence,initial_state=initial_state)

谢谢。

我终于找到了准确率保持在60%左右的原因,因为AttentionWrapper默认情况下会输出注意分数,但在我的情况下,我需要实际输出,用于计算下一个注意分数。解决方案是在AttentionWrapper中设置output_attention=False:

attn_cell = tfa.seq2seq.AttentionWrapper(lstm_cell,attention_mechanism, 
attention_layer_size=num_units, output_attention=False) 
如果有人遇到同样的问题,请在此更新