【问题标题】:Concatenating two RNN states in tensorflow在张量流中连接两个 RNN 状态
【发布时间】:2017-12-05 23:58:19
【问题描述】:

我正在尝试组合两个 RNN 状态并通过 tensorflow 中的另一个 RNN 运行它们。这是我正在尝试处理的代码 sn-p:

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 状态的正确方法吗?

【问题讨论】:

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


    【解决方案1】:

    问题出在这两行:

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

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

    【讨论】:

    • 谢谢,解决了这个问题。您还可以建议如何连接下一个 rnn 的张量吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2021-01-30
    • 2021-11-20
    相关资源
    最近更新 更多