【问题标题】:Unable to interpret a line of python code that creates a LSTM cell using tensorflow无法解释使用 tensorflow 创建 LSTM 单元的 Python 代码行
【发布时间】:2019-06-28 06:23:25
【问题描述】:

我正在尝试弄清楚功能齐全的 python 代码是如何工作的。一个块使用 tensorflow 创建一个 LSTM 单元。我不知道如何解释下面评论指定的行

def get_lstm_weights(n_hidden, forget_bias, dim, scope="rnn_cell"):
    # Create LSTM cell
    cell = tf.contrib.rnn.LSTMCell(num_units = n_hidden, reuse=None, forget_bias = forget_bias)
    #--------------------------------------
    # I DO NOT UNDERSTAND THE NEXT LINE
    cell(tf.zeros([1, dim +1]), (tf.zeros([1, n_hidden]),tf.zeros([1, n_hidden])), scope=scope)
    # -------------------------------------
    cell = tf.contrib.rnn.LSTMCell(num_units = n_hidden, reuse=True, forget_bias = forget_bias)

    # Create output weights
    weights = {
        'W_1': tf.Variable(tf.truncated_normal([n_hidden, dim], stddev=0.05)),
        'b_1': tf.Variable(0.1*tf.ones([dim])),
    }

    return cell, weights

【问题讨论】:

    标签: python tensorflow lstm


    【解决方案1】:

    请注意,tf.contrib.rnn.LSTMCellcallable class 的一个示例。

    这是一个可以像函数一样调用的类。你正在努力的那条线正是这样做的。它调用 cell 并带有括号中的参数。

    如果你想看看这是什么,你可以检查 tf.contrib.rnn.LSTMCell 的类定义中的 __call__ 方法

    【讨论】:

    • 感谢您的回答,但为什么在调用“单元格”后重复同一行?我的意思是“cell = tf.contrib.rnn.LSTMCell(....”行。这是否意味着正在创建两个 LSTM 单元?
    • 恐怕我无法解释“为什么”,但是,是的,这似乎是正在发生的事情。 cell 在第 1 行创建,然后在第 2 行调用,然后在第 3 行创建新的cell
    • 您在哪里找到此代码?它在什么背景下?
    • 您肯定会创建两个 LSTM 单元并仅返回其中一个可能看起来很奇怪,但我猜它与 scope 变量有关(第一个 cell 是在此 scope 中创建的,不像第二个)但是,正如@Stewart_R 所问的那样,当我们不知道它的上下文时,很难再说什么..
    • @Stewart_R 源代码在 Github 上可用。这是整个代码的链接github.com/heytitle/blackbox-optimization-using-rnns/blob/…
    猜你喜欢
    • 1970-01-01
    • 2018-05-26
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多