aqibsaeed/Urban-Sound-Classification

ValueError: Trying to share variable rnn/multi_rnn_cell/cell_0/lstm_cell/kernel, but specified shape (600, 1200) and found shape (320, 1200).

Closed this issue · 8 comments

Hi,

I am trying out the RNN notebook and when executing the code, it gives this error:
ValueError: Trying to share variable rnn/multi_rnn_cell/cell_0/lstm_cell/kernel, but specified shape (600, 1200) and found shape (320, 1200).

when executing this line:
output, state = tf.nn.dynamic_rnn(cell, x, dtype = tf.float32)

I suspect the network is giving wrong n_input values, but I am not sure what should be the correct value?

the full stack trace is as below:

Traceback (most recent call last):
  File "RNNClassifier.py", line 79, in <module>
    prediction = RNN(x, weight, bias)
  File "RNNClassifier.py", line 74, in RNN
    output, state = tf.nn.dynamic_rnn(cell, x, dtype = tf.float32)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 574, in dynamic_rnn
    dtype=dtype)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 737, in _dynamic_rnn_loop
    swap_memory=swap_memory)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2770, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2599, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2549, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 722, in _time_step
    (output, new_state) = call_cell()
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn.py", line 708, in <lambda>
    call_cell = lambda: cell(input_t, state)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 180, in __call__
    return super(RNNCell, self).__call__(inputs, state)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 441, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 916, in call
    cur_inp, new_state = cell(cur_inp, cur_state)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 180, in __call__
    return super(RNNCell, self).__call__(inputs, state)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 441, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 542, in call
    lstm_matrix = _linear([inputs, m_prev], 4 * self._num_units, bias=True)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1017, in _linear
    initializer=kernel_initializer)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 360, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1405, in wrapped_custom_getter
    *args, **kwargs)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 183, in _rnn_get_variable
    variable = getter(*args, **kwargs)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/rnn_cell_impl.py", line 183, in _rnn_get_variable
    variable = getter(*args, **kwargs)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
  File "/datadrive/xiaoyzhu/python2env/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 669, in _get_single_variable
    found_var.get_shape()))
ValueError: Trying to share variable rnn/multi_rnn_cell/cell_0/lstm_cell/kernel, but specified shape (600, 1200) and found shape (320, 1200).

According to your earlier comment ("I figured it out - this issue is because i provided the wrong path to the wav files. I have changed to the absolute path (and referencing issue #6 ) and this issue is solved.") this issue has been solved.

It is pretty clear from the error message that your input dimensions don't the define input dimensions for RNN. Check this by printing out their values.

Can you please elaborate on how to fix this error?
I have been getting the same error.

I had the same experience with the same error and I lost a hole day to fix it.
There is an explanatory tutorial on RNN here which defines the correct form of multiple cells in RNN.
Also there is a problem with different modules of tf so I used this approach to solve it.
Thanks to @aqibsaeed for the interesting code of NN but please try to update your code in RNN_function with the correct ones in order to make the script runnable.

Thank you for your comment. The RNN function provided in the notebook can't be used to stack multiple RNNs in a straightforward way as I wrote in long time ago when TF's API was not that mature. It would be great, if you submit a pull request resolving this.

@Kyziridis could you please tell me how did you solve the problem exactly.. I am a beginner in deep learning and I’m facing the same problem I checked the input data I don't think there is a problem in it . I looked at the provided links but I did not know how should I exactly update the code

Thank you,

@fotoon1993 I saw again the code and my script. I am posting my function.

def RNN(x, weight, bias): stacked_rnn = [] for iiLyr in range(2): stacked_rnn.append(tf.nn.rnn_cell.LSTMCell(num_units=n_hidden, state_is_tuple=True)) MultiLyr_cell = tf.nn.rnn_cell.MultiRNNCell(cells=stacked_rnn, state_is_tuple=True) output, state = tf.nn.dynamic_rnn(MultiLyr_cell, x, dtype = tf.float32) output = tf.transpose(output, [1, 0, 2]) last = tf.gather(output, int(output.get_shape()[0]) - 1) return tf.nn.sigmoid(tf.matmul(last, weight) + bias)

I replace this function for RNN. Probably there are some TF commands which are depricated in python 3.6 . I think that the specific change is the tf.nn.dynamic_rnn(MultiLyr)

@Kyziridis it is working now thank you very much