Koustuv Sinha Koustuv Sinha - 1 month ago 11
Python Question

Concatenating two RNN states in tensorflow

I am trying to combine two RNN states and run them through another RNN in tensorflow. Here is the code snippet I am trying to work on :

import numpy as np
c = [1, 2, 3,4, 5, 6,2, 3,4]
u = [4,5,6,6,7,8,5,6,7]
tf.reset_default_graph()
with tf.Session() as sess:
cell = tf.contrib.rnn.BasicLSTMCell(1)
cn = tf.placeholder(tf.int32, shape=[None, 9],name="cn")
ut = tf.placeholder(tf.int32, shape=[None, 9],name="ut")
with tf.variable_scope("word_emb",reuse=None):
W = tf.get_variable("word_embed",shape=[10,1])
cn_e = tf.nn.embedding_lookup(W, cn)
ut_e = tf.nn.embedding_lookup(W, ut)
cn_e = tf.unstack(cn_e,9,1)
ut_e = tf.unstack(ut_e,9,1)
#print cn_e.get_shape().as_list()
with tf.variable_scope("encoding_1"):
c_out,c_state = tf.contrib.rnn.static_rnn(cell,cn_e,dtype=tf.float32)
with tf.variable_scope("encoding_2"):
u_out,u_state = tf.contrib.rnn.static_rnn(cell,ut_e,dtype=tf.float32)
print c_state[0].eval()
print u_state[0].eval()
comb_out,comb_state = tf.contrib.rnn.static_rnn(cell,tf.concat(c_state,u_state))
init_op = tf.global_variables_initializer()
sess.run(init_op)
sess.run(comb_out,feed_dict={
cn:np.random.randint(0, 25, size=[1, 9])
,ut:np.random.randint(0, 25, size=[1, 9])
})


However, I am facing this error:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'cn' with dtype int32


which I don't understand as I am feeding cn in the
feed_dict
. Another followup question, is this the right way to concatenate RNN states?

Answer Source

The problem is in these two lines:

print c_state[0].eval()
print u_state[0].eval()

As both c_state and u_state are dependent on placeholders, you should feed values for them.