简体   繁体   English

Tensorflow中的可变范围问题

[英]variable scope issue in Tensorflow

def biLSTM(data, n_steps):


    n_hidden= 24
    data = tf.transpose(data, [1, 0, 2])
    # Reshape to (n_steps*batch_size, n_input)
    data = tf.reshape(data, [-1, 300])
    # Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
    data = tf.split(0, n_steps, data)    

    lstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)
    # Backward direction cell
    lstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)

    outputs, _, _ = tf.nn.bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, data, dtype=tf.float32)


    return outputs, n_hidden

In my code I am calling this function twice to create 2 bidirectional LSTMs. 在我的代码中,我调用此函数两次以创建2个双向LSTM。 Then I got the problem of reusing variables. 然后我遇到了重用变量的问题。

ValueError: Variable lstm/BiRNN_FW/BasicLSTMCell/Linear/Matrix already exists, disallowed. ValueError:变量lstm / BiRNN_FW / BasicLSTMCell / Linear / Matrix已存在,不允许。 Did you mean to set reuse=True in VarScope? 你的意思是在VarScope中设置reuse = True吗?

To resolve this I added the LSTM definition in the function within with tf.variable_scope('lstm', reuse=True) as scope: 为了解决这个问题,我在函数中添加了LSTM定义,并将with tf.variable_scope('lstm', reuse=True) as scope:

This led to a new issue 这导致了一个新问题

ValueError: Variable lstm/BiRNN_FW/BasicLSTMCell/Linear/Matrix does not exist, disallowed. ValueError:变量lstm / BiRNN_FW / BasicLSTMCell / Linear / Matrix不存在,不允许。 Did you mean to set reuse=None in VarScope? 你的意思是在VarScope中设置reuse = None吗?

Please help with a solution to this. 请帮助解决这个问题。

When you create BasicLSTMCell(), it creates all the required weights and biases to implement an LSTM cell under the hood. 当您创建BasicLSTMCell()时,它会创建所有必需的权重和偏差,以实现引擎盖下的LSTM单元。 All of these variables are assigned names automatically. 所有这些变量都自动分配名称。 If you call the function more than once within the same scope you get the error you get. 如果您在同一范围内多次调用该函数,则会得到错误。 Since your question seems to state that you want to create two separate LSTM cells, you do not want to reuse the variables, but you do want to create them in separate scopes. 由于您的问题似乎表明您要创建两个单独的LSTM单元格,因此您不希望重用这些变量,但您确实希望在不同的范围内创建它们。 You can do this in two different ways (I haven't actually tried to run this code, but it should work). 您可以通过两种不同的方式执行此操作(我实际上没有尝试运行此代码,但它应该可以工作)。 You can call your function from within a unique scope 您可以在一个独特的范围内调用您的函数

def biLSTM(data, n_steps):
    ... blah ...

with tf.variable_scope('LSTM1'):
    outputs, hidden = biLSTM(data, steps)

with tf.variable_scope('LSTM2'):
    outputs, hidden = biLSTM(data, steps)

or you can pass a unique scope name to the function and use the scope inside 或者您可以将唯一的范围名称传递给函数并使用范围内部

def biLSTM(data, n_steps, layer_name):
    ... blah...
    with tf.variable_scope(layer_name) as scope:
        lstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)
        lstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)
        outputs, _, _ = tf.nn.bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, data, dtype=tf.float32)
    return outputs, n_hidden

l1 = biLSTM(data, steps, 'layer1')
l2 = biLSTM(data, steps, 'layer2')

It is up to your coding sensibilities which approach to choose, they are functionally pretty much the same. 这取决于您的编码敏感度选择方法,它们在功能上几乎相同。

I also has the similar problem. 我也有类似的问题。 However I was using keras implementation with pretrained Resnet50 model. 但是我使用了预先训练的Resnet50模型的keras实现。

It worked for me when I updated the tensorflow version using following command: 当我使用以下命令更新tensorflow版本时,它对我有用:

conda update -f -c conda-forge tensorflow

and used 和使用

from keras import backend as K
K.clear_session

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM