我的X_seqs=tf.unstack(tf.transpose(X,[1,0,2]))
和X_seqs=tf.unstack(X,axis=1)
的RNN代码产生了不同的结果?然而,X_seqs
在这两种语法中都相同。有人遇到过类似的问题吗?
tf.reset_default_graph()
tf.random.set_random_seed(1234)
n_steps=2
n_inputs=3
n_neurons=5
X=tf.placeholder(tf.float32, shape=(None,n_steps,n_inputs))
#X_seqs=tf.unstack(tf.transpose(X,[1,0,2]))
X_seqs=tf.unstack(X,axis=1)
#basic_rnn=tf.contrib.rnn.BasicRNNCell(num_units=n_neurons)
basic_rnn=tf.nn.rnn_cell.BasicRNNCell(num_units=n_neurons)
#rnn_out,rnn_states=tf.contrib.rnn.static_rnn(basic_rnn,X_seqs,dtype=tf.float32)
rnn_out,rnn_states=tf.nn.static_rnn(basic_rnn,X_seqs,dtype=tf.float32)
outputs=tf.stack(rnn_out,axis=1)
X_batch = np.array([
# t = 0 t = 1
[[0, 1, 2], [9, 8, 7]], # instance 0
[[3, 4, 5], [0, 0, 0]], # instance 1
[[6, 7, 8], [6, 5, 4]], # instance 2
[[9, 0, 1], [3, 2, 1]], # instance 3
])
init= tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
X_s=sess.run(X_seqs,feed_dict={X:X_batch})
out_val_2,states_2=sess.run([outputs,rnn_states],feed_dict={X:X_batch})