Reputation: 1194
I'm trying to code and learn RNN from scratch in tensorflow. I've understood that we need to create a basicRNNCell and call it as rnn(data, input) with the right dimensions. But I'm getting dimension error as shown below
Here's the code that I wrote.
x = tf.placeholder(dtype=tf.float32, shape=[2, 4]) # Batchsize: 2, stepsize: 4
rnn = tf.contrib.rnn.BasicRNNCell(10, reuse=True)
state = rnn.zero_state(2, dtype=tf.float32) # A matrix of shape [2, 10]
rnn(x, state) # ERROR OCCURS AT THIS LINE
with tf.Session() as sess:
sess.run(y, feed_dict={x: [[1, 2, 1, 1], [0, 0, 0, 1]]})
And here's the error
ValueError: Trying to share variable basic_rnn_cell/weights, but specified shape (14, 10) and found shape (6, 4).
What am I doing wrong?
Upvotes: 1
Views: 1753
Reputation: 1927
I think that you are doing something wrong with your parameters. I will report here a working code for you:
import tensorflow as tf
batch_size = 2
seq_len = 4
rnn_output_dim = 10
sess = tf.InteractiveSession()
x = tf.placeholder(dtype=tf.float32, shape=(batch_size, seq_len))
rnn_cell = tf.contrib.rnn.BasicRNNCell(rnn_output_dim)
state = rnn_cell.zero_state(batch_size, dtype=tf.float32)
output, state = rnn_cell(x, state)
sess.run(tf.global_variables_initializer())
res = sess.run(output, {x: [[1,2,1,1],[0,0,0,1]]})
print(res)
"""
array([[ 0.21117647, -0.66317081, 0.89524043, -0.54004282, -0.80074871,
0.86230665, -0.77558851, 0.46061009, 0.09429809, 0.17166322],
[ 0.42703518, -0.18116307, 0.32701704, 0.02224555, -0.39941645,
0.10977989, -0.15780419, 0.41289148, 0.35284221, -0.21626833]], dtype=float32)
"""
Upvotes: 2