yohan sebmoc
yohan sebmoc

Reputation: 33

Value error from tf.nn.dynamic_rnn: Dimensions must be equal

I want to use the tf.nn.dynamic_rnn function of tensorflow to create a RNN but it allows me to set a hidden size for only one of my layers.

Here is my code:

    self._Input=tf.placeholder(tf.float64,shape=(None,self._time_size,self._batch_dim),name='input')
    self._Expected_o=tf.placeholder(tf.float64,shape=(None,self._time_size,self._cell_output_size),name='Expected_o')
    #creation of the network

    initializer = tf.random_uniform_initializer(-1, 1)      
    cell = tf.nn.rnn_cell.GRUCell(self._hidden_size,kernel_initializer=initializer)  
    rnn_cells = tf.nn.rnn_cell.MultiRNNCell([cell] * self._num_layer) 

    # network
    self._output, out_state = tf.nn.dynamic_rnn(cell=rnn_cells,inputs= self._Input, dtype=tf.float64)

Everything works fine as long as I keep my hidden_size value as the same as the last dimension of my Input place holder, i.e., _batch_dim.

But when it's different, I always get this message of error:

ValueError: Dimensions must be equal, but are 8 and X for 'rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/gru_cell/MatMul_2' (op: 'MatMul') with input shapes: [?,Y], [X,Y].

where X is the value that I put for my hidden_size + 1 and Y the value of hidden_size*2. I've tried many value of hidden_size and this two numbers, X and Y appear each time. The message error indicate that the error occur during the calling of tf.rnn.dynamic_rnn.

Upvotes: 3

Views: 1989

Answers (1)

Maxim
Maxim

Reputation: 53758

Change your code from ...

cell = tf.nn.rnn_cell.GRUCell(self._hidden_size,kernel_initializer=initializer)  
rnn_cells = tf.nn.rnn_cell.MultiRNNCell([cell] * self._num_layer) 

to ...

layers = [tf.nn.rnn_cell.GRUCell(self._hidden_size,kernel_initializer=initializer) 
          for _ in self._num_layer]
rnn_cells = tf.nn.rnn_cell.MultiRNNCell(layers)

... and the deeper GRU layers will be able to adapt to the output from the earlier layers.

Upvotes: 4

Related Questions