适配Tensorflow RNN Seq2Seq模型代码到Tensorflow 2.0

5

我对Tensorflow非常陌生,最近从这个链接开始尝试一个简单的聊天机器人项目。

有很多警告提示说在Tensorflow 2.0中会弃用一些功能,建议升级。因此我进行了升级,并使用了自动Tensorflow代码升级工具更新所有必要的文件到2.0版本,但其中还是出现了一些错误。

在处理model.py文件时,它返回了以下警告:

133:20: WARNING: tf.nn.sampled_softmax_loss requires manual check. `partition_strategy` has been removed from tf.nn.sampled_softmax_loss.  The 'div' strategy will be used by default.
148:31: WARNING: Using member tf.contrib.rnn.DropoutWrapper in deprecated module tf.contrib.rnn. (Manual edit required) tf.contrib.rnn.* has been deprecated, and widely used cells/functions will be moved to tensorflow/addons repository. Please check it there and file Github issues if necessary.
148:31: ERROR: Using member tf.contrib.rnn.DropoutWrapper in deprecated module tf.contrib. tf.contrib.rnn.DropoutWrapper cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
171:33: ERROR: Using member tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq in deprecated module tf.contrib. tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
197:27: ERROR: Using member tf.contrib.legacy_seq2seq.sequence_loss in deprecated module tf.contrib. tf.contrib.legacy_seq2seq.sequence_loss cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.

我遇到的主要问题是使用现已不存在的contrib模块的代码。我该如何调整以下三个代码块,使它们在Tensorflow 2.0中工作?

# Define the network
        # Here we use an embedding model, it takes integer as input and convert them into word vector for
        # better word representation
        decoderOutputs, states = tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq(
            self.encoderInputs,  # List<[batch=?, inputDim=1]>, list of size args.maxLength
            self.decoderInputs,  # For training, we force the correct output (feed_previous=False)
            encoDecoCell,
            self.textData.getVocabularySize(),
            self.textData.getVocabularySize(),  # Both encoder and decoder have the same number of class
            embedding_size=self.args.embeddingSize,  # Dimension of each word
            output_projection=outputProjection.getWeights() if outputProjection else None,
            feed_previous=bool(self.args.test)  # When we test (self.args.test), we use previous output as next input (feed_previous)
        )

# Finally, we define the loss function
            self.lossFct = tf.contrib.legacy_seq2seq.sequence_loss(
                decoderOutputs,
                self.decoderTargets,
                self.decoderWeights,
                self.textData.getVocabularySize(),
                softmax_loss_function= sampledSoftmax if outputProjection else None  # If None, use default SoftMax
            )

encoDecoCell = tf.contrib.rnn.DropoutWrapper(
                    encoDecoCell,
                    input_keep_prob=1.0,
                    output_keep_prob=self.args.dropout
                )
1个回答

3

tf.contrib是TensorFlow社区的贡献,其工作方式如下:

  • 社区成员可以提交代码,该代码随后会分发到标准的TensorFlow软件包中。
  • 他们的代码会被TensorFlow团队审核,并作为TensorFlow测试的一部分进行测试。

现在,在tensorflow 2中,Tensorflow已经移除了contrib,现在contrib中的每个项目都有三种选择:转移到核心;转移到单独的存储库;或删除。

您可以从这个链接查看所有项目列表以及它们所属的类别。

至于替代解决方案,将代码从Tensorflow1迁移到Tensorflow2不会自动发生,您需要手动更改。以下是您可以使用的替代方法:

对于tf.contrib.rnn.DropoutWrapper,您可以将其更改为tf.compat.v1.nn.rnn_cell.DropoutWrapper

对于序列到序列,您可以使用TensorFlow Addons

TensorFlow Addons项目包含许多序列到序列工具,让您可以轻松地构建生产就绪的编码器-解码器。

例如,您可以使用以下内容:

import tensorflow_addons as tfa
encoder_inputs = keras.layers.Input(shape=[None], dtype=np.int32)
decoder_inputs = keras.layers.Input(shape=[None], dtype=np.int32)
sequence_lengths = keras.layers.Input(shape=[], dtype=np.int32)
embeddings = keras.layers.Embedding(vocab_size, embed_size)
encoder_embeddings = embeddings(encoder_inputs)
decoder_embeddings = embeddings(decoder_inputs)
encoder = keras.layers.LSTM(512, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_embeddings)encoder_state = [state_h, state_c]
sampler = tfa.seq2seq.sampler.TrainingSampler()
decoder_cell = keras.layers.LSTMCell(512)
output_layer = keras.layers.Dense(vocab_size)
decoder = tfa.seq2seq.basic_decoder.BasicDecoder(decoder_cell, sampler,
output_layer=output_layer)
final_outputs, final_state, final_sequence_lengths = decoder(
decoder_embeddings, initial_state=encoder_state,
sequence_length=sequence_lengths)
Y_proba = tf.nn.softmax(final_outputs.rnn_output)
model = keras.Model(inputs=[encoder_inputs, decoder_inputs,
sequence_lengths],
outputs=[Y_proba])

同样的,您需要将所有使用tf.contrib的方法更改为兼容的方法。

我希望这回答了您的问题。


@Adalex3 - 如果您认为我已经回答了您的问题,请接受答案并点赞。 - user11530462
请问您能解释一下这里的函数调用decoder(decoder_embeddings, initial_state=encoder_state, sequence_length=sequence_lengths)吗?我在源代码的第159行https://github.com/tensorflow/addons/blob/5f618fdb92d9737da059de2a33fa606e97505398/tensorflow_addons/seq2seq/decoder.py#L159中没有看到`sequence_length`作为函数参数。 - thinkdeep

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接