尝试共享变量 basic_rnn_cell/weights,但指定的形状错误

Trying to share variable basic_rnn_cell/weights, but specified shape error

我正在尝试在 tensorflow 中从头开始编写和学习 RNN。我知道我们需要创建一个 basicRNNCell 并将其称为具有正确尺寸的 rnn(data, input) 。但是我收到尺寸错误,如下所示

这是我写的代码。

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]]})

这是错误

ValueError: Trying to share variable basic_rnn_cell/weights, but specified shape (14, 10) and found shape (6, 4).

我做错了什么?

我认为您的参数有问题。我将在这里为您报告一个工作代码:

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)
"""