在tensorflow中连接两个RNN状态

时间:2017-07-02 17:14:50

标签: python numpy tensorflow neural-network recurrent-neural-network

我正在尝试组合两个RNN状态,并通过张量流中的另一个RNN运行它们。以下是我正在尝试处理的代码段:

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

但是,我遇到了这个错误:

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

我不明白,因为我在feed_dict喂养cn。另一个后续问题,这是连接RNN状态的正确方法吗?

1 个答案:

答案 0 :(得分:1)

问题在于以下两行:

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

由于c_state和u_state都依赖于占位符,因此您应该为它们提供值。