Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试使用不同的变量范围重用RNNCell_Python_Tensorflow - Fatal编程技术网

Python 尝试使用不同的变量范围重用RNNCell

Python 尝试使用不同的变量范围重用RNNCell,python,tensorflow,Python,Tensorflow,我正在使用支持GPU的Tensorflow 1.1.0,我有以下功能: def get_init_cell(batch_size, rnn_size, keep_prob=0.75, layers=2): """ Create an RNN Cell and initialize it. :param batch_size: Size of batches :param rnn_size: Size of RNNs :return: Tuple (cell

我正在使用支持GPU的Tensorflow 1.1.0,我有以下功能:

def get_init_cell(batch_size, rnn_size, keep_prob=0.75, layers=2):
    """
    Create an RNN Cell and initialize it.
    :param batch_size: Size of batches
    :param rnn_size: Size of RNNs
    :return: Tuple (cell, initialize state)
    """
    # Basic LSTM cell
    # lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size, forget_bias=0.0, state_is_tuple=True, reuse=tf.get_variable_scope().reuse)

    # Add drop to the cell
    drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)

    # Stack multiple LSTM layers
    rnn_cell = tf.contrib.rnn.MultiRNNCell([drop for _ in range(layers)], state_is_tuple=True)

    # Getting an initial state of zeros
    initial_state = rnn_cell.zero_state(batch_size, tf.float32)

    # Set the name
    initial_state = tf.identity(initial_state, 'initial_state')

    return rnn_cell, initial_state
如果我使用
lstm=tf.contrib.rnn.BasicLSTMCell(rnn_size)
的话,前面的代码在Tensorflow 1.0.0上运行得非常好,但是现在我改变了获取lstm单元格的方式。我得到了这个错误:

ValueError:尝试重新使用RNNCell 变量范围不同于其 第一次使用。第一次使用cell是使用scope “rnn/multi_rnn_cell/cell_0/basic_lstm_cell”,此尝试与 范围“rnn/多rnn\U单元/单元1/基本单元”。请创建一个 如果希望单元格使用其他集合,请创建新的单元格实例 重量。如果在使用之前: MultirNcell([BasicLSTMCell(…)]*num_层),更改为: MultirNcell([BasicLSTMCell(…)用于范围内的(num_层)])。如果 在将同一个单元格实例用作forward和 反向双向RNN的单元,只需创建两个实例(一个 一个用于前进,一个用于后退)。2017年5月,我们将开始 如果需要,将此单元格的行为转换为使用现有存储的权重 any,当使用scope=None调用它时(这可能导致静默模型 降级,因此此错误将一直保留到那时。)

我已经用这个更改了
get_init_cell

def lstm_cell(rnn_size, keep_prob=0.75):
    # Basic LSTM cell
    lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size, reuse=tf.get_variable_scope().reuse)

    # Add drop to the cell
    return tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)

def get_init_cell(batch_size, rnn_size, keep_prob=0.75, layers=2):
    """
    Create an RNN Cell and initialize it.
    :param batch_size: Size of batches
    :param rnn_size: Size of RNNs
    :return: Tuple (cell, initialize state)
    """    
    # Stack multiple LSTM layers
    rnn_cell = tf.contrib.rnn.MultiRNNCell([lstm_cell(rnn_size, keep_prob) for _ in range(layers)])

    # Getting an initial state of zeros
    initial_state = rnn_cell.zero_state(batch_size, tf.float32)

    # Set the name
    initial_state = tf.identity(initial_state, 'initial_state')

    return rnn_cell, initial_state
但我还是犯了同样的错误

我也尝试过同样的错误:

def get_init_cell(batch_size, rnn_size, keep_prob=0.75, layers=2):
    """
    Create an RNN Cell and initialize it.
    :param batch_size: Size of batches
    :param rnn_size: Size of RNNs
    :return: Tuple (cell, initialize state)
    """    

    def lstm_cell():
        # Basic LSTM cell
        return tf.contrib.rnn.BasicLSTMCell(rnn_size, reuse=tf.get_variable_scope().reuse)        

    def attn_cell():
        return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=keep_prob)

    # Stack multiple LSTM layers
    rnn_cell = tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(layers)], state_is_tuple=True)

    # Getting an initial state of zeros
    initial_state = rnn_cell.zero_state(batch_size, tf.float32)

    # Set the name
    initial_state = tf.identity(initial_state, 'initial_state')

    return rnn_cell, initial_state

有什么想法吗?

您能给出一段小的、自包含的代码来重现问题(即,一个最小的脚本,它显示问题,没有外部依赖关系,我们可以运行和调试吗?)。根据你的描述,我自己无法重现这个问题。谢谢您能否给出一段复制问题的小型自包含代码(即,一个显示问题的最小脚本,没有外部依赖项,我们可以运行和调试它?)。根据你的描述,我自己无法重现这个问题。谢谢