【问题标题】:How to migrate from TensorFlow 1.x to TensorFlow 2.x如何从 TensorFlow 1.x 迁移到 TensorFlow 2.x
【发布时间】:2020-08-24 19:34:58
【问题描述】:
class Model:
    def __init__(
        self,
        learning_rate,
        num_layers,
        size,
        size_layer,
        output_size,
        forget_bias = 0.1,
    ):
        def lstm_cell(size_layer):
            return tf.compat.v1.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)

        rnn_cells = tf.compat.v1.nn.rnn_cell.MultiRNNCell(
            [lstm_cell(size_layer) for _ in range(num_layers)],
            state_is_tuple = False,
        )
        self.X = tf.compat.v1.placeholder(tf.float32, (None, None, size))
        self.Y = tf.compat.v1.placeholder(tf.float32, (None, output_size))
        drop = tf.compat.v1.nn.rnn_cell.DropoutWrapper(
            rnn_cells, output_keep_prob = forget_bias
        )
        self.hidden_layer = tf.compat.v1.placeholder(
            tf.float32, (None, num_layers * 2 * size_layer)
        )
        self.outputs, self.last_state = tf.compat.v1.nn.dynamic_rnn(
            drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32
        )
        self.logits = tf.compat.v1.layers.dense(self.outputs[-1], output_size)
        self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))
        self.optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(
            self.cost
        ) 

我想将上面的代码转换为相关的 TensorFlow 2.x 而无需急切执行,有人可以帮忙吗? 我一直在尝试改变一些事情,例如:将tf.compat.v1.nn.rnn_cell.LSTMCell 更改为tf.keras.layers.LSTMCelltf.compat.v1.nn.rnn_cell.MultiRNNCell 更改为tf.keras.layers.StackedRNNCells 也将tf.compat.v1.nn.dynamic_rnn 更改为tf.keras.layers.RNN 我该怎么做?

【问题讨论】:

标签: python tensorflow machine-learning tensorflow2.0 recurrent-neural-network


【解决方案1】:

TensorFlow 1.x 脚本不能直接与 TensorFlow 2.x 一起使用,但它们需要转换。

tf_upgrade_v2 --infile tensorflow_v1.py --outfile tensorflow_v2.py

如果文件夹中有多个文件,可以使用以下命令。

tf_upgrade_v2  v1-code-folder  code-upgraded-folder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-29
    • 2018-08-10
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多