权重不存在,或者不是用tf.get_variable()创建的

时间:2017-10-19 10:39:19

标签: tensorflow lstm

我花了好几天试图弄清楚发生了什么,我仍然得到这个错误。这是我得到的错误

  

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)

2 个答案:

答案 0 :(得分:2)

如果您不需要重复使用该单元格,只需使用以下内容,

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

否则,如果您需要重复使用,可以按照此Reuse Reusing Variable of LSTM in Tensorflow帖子进行解释。

答案 1 :(得分:1)

如果您想重复使用权重,那么最简单的方法是创建一个单元格对象并将其多次传递给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输入的大小相同。