【问题标题】:weights does not exist, or was not created with tf.get_variable()权重不存在,或者不是用 tf.get_variable() 创建的
【发布时间】:2017-10-19 10:39:19
【问题描述】:

我花了几天时间试图弄清楚发生了什么,但我仍然收到此错误。这是我得到的错误

ValueError:变量 rnn/multi_rnn_cell/cell_1/basic_lstm_cell/weights 不存在,或者不是用 tf.get_variable() 创建的。你是否 意思是在 VarScope 中设置reuse=None?

这是我的示例代码,有谁知道我做错了什么?

x = tf.placeholder(tf.float32,[None,n_steps,n_input])
y = tf.placeholder(tf.float32,[None,n_classes])
weights = {
    'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
    'out': tf.Variable(tf.random_normal([n_classes]))
}

def RNN(x, weights, biases):

    x = tf.unstack(x, n_steps, 1)

    lstm_cell = rnn.MultiRNNCell([cell() for y in range(2)] , state_is_tuple=True)


    # Get lstm cell output
    outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

    # Linear activation, using rnn inner loop last output
    return tf.matmul(outputs[-1], weights['out']) + biases['out']

def cell():        
    return rnn.BasicLSTMCell(n_hidden,forget_bias=0.1, reuse=True)

pred = RNN(x, weights, biases)

【问题讨论】:

  • 你想用tf.variable_scope(...)做什么?您是否尝试重用两个 LSTM 单元中的权重?在这种情况下,您应该只重用相同的单元格对象。否则,只需删除 tf.variable_scope,因为它会混淆 TF(您在同一变量范围的两个实例下创建操作,这会导致命名不一致,例如参见 here)。
  • 谢谢,@jdehesa 我试过没有它,但它仍然在抱怨同样的问题。我已经编辑了我的问题。
  • 啊,等等,我没看到你在单元格中使用了reuse=True。这需要您事先创建必要的变量(或通过事先调用 static_rnn / dynamic_rnn 来创建它们)。如果您真的想重用权重,如果可能的话,通常更容易重用相同的单元格对象(即创建一个并将包含两个引用的列表传递给MultiRNNCell)。如果您不想重复使用权重,请不要传递reuse=True
  • 您能否按照您认为可行的方式修改代码来回答这个问题?
  • 我仍然不清楚你是否真的想重用 LSTM 单元中的权重,每种情况的答案都不一样。

标签: tensorflow lstm


【解决方案1】:

如果你不需要重复使用单元格,只需使用以下,

def cell():        
    return rnn.BasicLSTMCell(n_hidden,forget_bias=0.1)

另外,如果需要复用,可以关注Reuse Reusing Variable of LSTM in Tensorflow这个帖子,里面有很好的解释。

【讨论】:

    【解决方案2】:

    如果你想重用权重,那么最简单的方法是创建一个单元格对象并多次传递给MultiRNNCell

    import tensorflow as tf
    from tensorflow.contrib import rnn
    
    n_steps = 20
    n_input = 10
    n_classes = 5
    n_hidden = 15
    
    x = tf.placeholder(tf.float32,[None,n_steps,n_input])
    y = tf.placeholder(tf.float32,[None,n_classes])
    weights = {
        'in': tf.Variable(tf.random_normal([n_input, n_hidden])),
        'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
    }
    biases = {
        'in': tf.Variable(tf.random_normal([n_hidden])),
        'out': tf.Variable(tf.random_normal([n_classes]))
    }
    
    def RNN(x, weights, biases):
    
        # Initial input layer
        inp = (tf.matmul(x, weights['in'][tf.newaxis, ...]) +
               biases['in'][tf.newaxis, tf.newaxis, ...])
        inp = tf.nn.sigmoid(inp)
        inp = tf.unstack(inp, axis=-1)
    
        my_cell = cell()
        lstm_cell = rnn.MultiRNNCell([my_cell for y in range(2)], state_is_tuple=True)
    
        # Get lstm cell output
        outputs, states = rnn.static_rnn(lstm_cell, inp, dtype=tf.float32)
    
        # Linear activation, using rnn inner loop last output
        return tf.matmul(outputs[-1], weights['out']) + biases['out']
    
    def cell():        
        return rnn.BasicLSTMCell(n_hidden,forget_bias=0.1)
    
    pred = RNN(x, weights, biases)
    

    但是,您必须确保在维度方面共享变量有意义,否则它将失败。在这种情况下,我在 LSTM 单元之前添加了一个附加层,以确保每个 LSTM 输入的大小相同。

    【讨论】:

    • Woot...Woot ...太棒了,这很有效。您能否在 LSTM 之前使用密集层的场景扩展您的答案。
    • 非常感谢你是明星 :)
    猜你喜欢
    • 2017-08-28
    • 2018-05-26
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2019-10-30
    相关资源
    最近更新 更多