【问题标题】:Trying to migrate model from Tensorflow 1.15.3 to Keras and Tensorflow 2.3.0尝试将模型从 Tensorflow 1.15.3 迁移到 Keras 和 Tensorflow 2.3.0
【发布时间】:2020-08-04 04:54:33
【问题描述】:

我有一个模型,我真的不明白如何转换为 Keras 模型,如果有人可以看一下并帮助我开始,我真的很感激!我试图找到一种使用“自我”的方法。 keras LSTM 模型中的值。通过查看其他论坛,我真的无法找到一种方法来做到这一点,而且 tensorflow 迁移教程对我的特定问题并没有真正的帮助。

如果有人也对类似的 keras 模型提出建议,那也将非常有帮助!

现在它正在使用 compat.v1 允许我在 Tensorflow 2.0 中运行。

代码:

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.nn.RNNCellDropoutWrapper(
            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(input_tensor=tf.square(self.Y - self.logits))
        self.optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(
            self.cost
        )

【问题讨论】:

    标签: python tensorflow keras migration lstm


    【解决方案1】:

    我发现在损失函数中实现的 tf 函数(如 tf.reduce_mean、K.mean、tf.square、tf.exp 等)会导致错误:

    急切执行函数的输入不能是 Keras 符号张量

    这是我在标题中提到的 tensorflow 版本之间的主要迁移问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 2021-09-25
      • 2018-12-08
      • 2019-06-04
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多