classification with LSTM RNN in tensorflow, ValueError: Shape (1, 10, 5) must have rank 2
<p>I am trying to design a simple lstm in tensorflow. I want to classify a sequence of data into classes from 1 to 10.</p>

<p>I have <em>10 timestamps</em> and data X. I am only taking one sequence for now, so my batch size = 1.
At every epoch, a new sequence is generated. For example X is a numpy array like this-</p>

<pre><code>X [[ 2.52413028  2.49449348  2.46520466  2.43625973  2.40765466  2.37938545
     2.35144815  2.32383888  2.29655379  2.26958905]]
</code></pre>

<p>To make it suitable for lstm input, I first converted in to a tensor and then reshaped it (batch_size, sequence_lenght, input dimension) -</p>

<pre><code>X= np.array([amplitude * np.exp(-t / tau)])
print 'X', X

#Sorting out the input
train_input = X
train_input = tf.convert_to_tensor(train_input)
train_input = tf.reshape(train_input,[1,10,1])
print 'ti', train_input
</code></pre>

<p>For output I am generating a one hot encoded label within a class range of 1 to 10. </p>

<pre><code>#------------sorting out the output
train_output= [int(math.ceil(tau/resolution))]
train_output= one_hot(train_output, num_labels=10)
print 'label', train_output

train_output = tf.convert_to_tensor(train_output)

&gt;&gt;label [[ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]]
</code></pre>

<p>Then I created the placeholders for tensorflow graph, made the lstm cell and gave weights and bias-</p>

<pre><code>data = tf.placeholder(tf.float32, shape= [batch_size,len(t),1])
target = tf.placeholder(tf.float32, shape = [batch_size, num_classes])

cell = tf.nn.rnn_cell.LSTMCell(num_hidden)
output, state = rnn.dynamic_rnn(cell, data, dtype=tf.float32)

weight = tf.Variable(tf.random_normal([batch_size, num_classes, 1])),
bias = tf.Variable(tf.random_normal([num_classes]))

#training
prediction = tf.nn.softmax(tf.matmul(output,weight) + bias)
cross_entropy = -tf.reduce_sum(target * tf.log(prediction))
optimizer = tf.train.AdamOptimizer()
minimize = optimizer.minimize(cross_entropy)
</code></pre>

<p>I have written the code this far and got error at the training step. Is it to do with the input shapes? Here is the traceback---</p>

<p>Traceback (most recent call last):</p>

<pre><code>  File "/home/raisa/PycharmProjects/RNN_test1/test3.py", line 66, in &lt;module&gt;
prediction = tf.nn.softmax(tf.matmul(output,weight) + bias)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1036, in matmul
name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 911, in _mat_mul
transpose_b=transpose_b, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2156, in create_op
set_shapes_for_outputs(ret)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1612, in set_shapes_for_outputs
shapes = shape_func(op)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/common_shapes.py", line 81, in matmul_shape
a_shape = op.inputs[0].get_shape().with_rank(2)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 625, in with_rank
raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (1, 10, 5) must have rank 2
</code></pre>

<p>Looking at your code, your rnn output should have a dimension of <code>batch_size x 1 x num_hidden</code> while your w has dimension <code>batch_size x num_classes x 1</code> however you want multiplication of those two to be <code>batcH_size x num_classes</code>. </p>

<p>Can you try <code>output = tf.reshape(output, [batch_size, num_hidden])</code> and <code>weight = tf.Variable(tf.random_normal([num_hidden, num_classes]))</code> and let me know how that goes?</p>

python tensorflow deep-learning recurrent-neural-network lstm