代码之家  ›  专栏  ›  技术社区  ›  lampShadesDrifter

ValueError:尝试在tf.nn.dynamic\n中共享变量,但发现维度不匹配

  •  0
  • lampShadesDrifter  · 技术社区  · 7 年前

    尝试使用tensorflow创建展开的RNN模型,并看到我不理解的错误消息。最后,尝试使用多步前瞻进行简单的时间序列预测,其中输入向量是一组回望窗口样本(每个10步长,每个特征1个),响应集样本是每个10步回望窗口序列的下一(5)个步骤。

    在这里,您可以看到数据集的尺寸和预期的RNN细胞神经元

    n_samples = int( data_x.get_shape()[0] )
    n_input_steps = int( data__x.get_shape()[1] )
    n_inputs = int( tx.get_shape()[2] )
    n_neurons = 7 # small for demo purposes
    n_output_steps = int( data_y.get_shape()[1] )
    n_outputs = int( data_y.get_shape()[2] )
    
    print (n_samples, n_input_steps, n_inputs, n_neurons, n_output_steps, n_outputs)
    
    #output
    (97, 10, 1, 7, 5, 1)
    

    以及展开的模型代码

    # params section
    
    X = tf.placeholder(tf.float64, [None, n_input_steps, n_inputs])
    print X.get_shape()
    y = tf.placeholder(tf.float64, [None, n_output_steps, n_outputs])
    print y.get_shape()
    
    cell = tf.nn.rnn_cell.LSTMCell(name='basic_lstm_cell', num_units=n_neurons, activation=tf.nn.relu, use_peepholes=True)
    # wrap in fully connect single output projection
    fc_cell = tf.contrib.rnn.OutputProjectionWrapper(cell, output_size=n_outputs, reuse=tf.AUTO_REUSE)
    
    outputs, states = tf.nn.dynamic_rnn(cell=fc_cell, inputs=X, dtype=tf.float64)
    
    """
    expecting unrolled RNN:
    
    y[0]   .... y[n_input_steps]
     |           |
     * -> ... -> *
     |     ....  |
    x[0]        x[n_input_steps] 
    
    where each * is an RNN cell with a single output
    """
    

    尝试设置此RNN时引发的完整错误消息如下所示

    (?, 10, 1)
    (?, 5, 1)
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-204-d52a40d9a302> in <module>()
         10 fc_cell = tf.contrib.rnn.OutputProjectionWrapper(cell, output_size=n_outputs, reuse=tf.AUTO_REUSE)
         11 
    ---> 12 outputs, states = tf.nn.dynamic_rnn(cell=fc_cell, inputs=X, dtype=tf.float64)
         13 
    ....
    ....
    ....
    /usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.pyc in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource, constraint, synchronization, aggregation)
        864         raise ValueError("Trying to share variable %s, but specified shape %s"
        865                          " and found shape %s." % (name, shape,
    --> 866                                                    found_var.get_shape()))
        867       if not dtype.is_compatible_with(found_var.dtype):
        868         dtype_str = dtype.name
    
    ValueError: Trying to share variable rnn/output_projection_wrapper/basic_lstm_cell/kernel, but specified shape (8, 28) and found shape (4, 12).
    

    如有任何调试建议或修复,将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  0
  •   lampShadesDrifter    7 年前

    经过一些随机修补,发现将代码的单元格创建片段更改为

    cell = tf.nn.rnn_cell.LSTMCell(
        name='basic_lstm_cell', num_units=n_neurons, activation=tf.nn.relu, reuse=tf.AUTO_REUSE)
    # wrap cell in fully connect single output projection
    fc_cell = tf.contrib.rnn.OutputProjectionWrapper(cell, output_size=n_outputs, reuse=tf.AUTO_REUSE)
    

    (主要是删除 use_peepholes=True )似乎阻止了错误的出现(应该注意的是,我从来都不知道什么样的窥视孔在哪里,只是在一些代码片段出现时将它们保留在那里,但它们似乎影响了某些地方的尺寸)。需要做更多的研究来解释这到底是如何影响代码的。

    推荐文章