Tensorflow,ValueError异常:这两个结构不具有相同的嵌套结构

问题描述 投票:1回答:1
import tensorflow as tf

vocab_num = 4000
word_dim = 300
question_encode = None
answer_num = 1000
common_dim = 256
memory_dim = 256

question_encode = tf.placeholder(
    tf.int64, [None, None], 'question_encode')
with tf.variable_scope('embedding'):
    embedding_matrix = tf.get_variable(
        'embedding_matrix',
        [vocab_num, word_dim], regularizer=tf.nn.l2_loss)
    question_embedding = tf.nn.embedding_lookup(
        embedding_matrix, question_encode, name='word_embedding')
    print('question_embedding', question_embedding)

shape = tf.shape(question_encode)
batch_size = shape[0]
question_length = tf.constant(15)
time = tf.constant(0, name='time')
max_length = tf.constant(20)
q_cell = tf.nn.rnn_cell.LSTMCell(word_dim)
q_state = q_cell.zero_state(batch_size, tf.float32)

word_embed_W = tf.get_variable('word_embed_W', [word_dim, common_dim], regularizer=tf.nn.l2_loss)
word_embed_b = tf.get_variable('word_embed_b', [common_dim])
word_embedding = question_embedding[:, time]
out_ = tf.ones((1, 256))

time = tf.constant(0)
out = tf.zeros((max_length - question_length, 256))

def _one_step(time, q_state, word_list):
    """One time step of model."""
    word_embedding = question_embedding[:, time]
    with tf.variable_scope('lstm_q'):
        q_output, q_state = q_cell(word_embedding, q_state)
    with tf.name_scope('transform_w'):
        word = tf.nn.xw_plus_b(
            word_embedding, word_embed_W, word_embed_b)
        word = tf.nn.tanh(word)
    word_list = tf.concat([word_list, word], axis=0)

    return time + 1, q_state, word_list

# main loop
time, q_state, out_ = tf.while_loop(
    cond=lambda time, *_: time < question_length,
    body=_one_step,
    loop_vars=[time, q_state, out_],
    shape_invariants=[time.get_shape(), tf.TensorShape([None, 256])]
)

word_list = tf.concat([out_, out], axis=0)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
res = sess.run(out)

当出现问题:


ValueError: The two structures don't have the same nested structure.

First structure: type=list str=[<tf.Tensor 'Const_2:0' shape=() dtype=int32>, LSTMStateTuple(c=<tf.Tensor 'LSTMCellZeroState/zeros:0' shape=(?, 300) dtype=float32>, h=<tf.Tensor 'LSTMCellZeroState/zeros_1:0' shape=(?, 300) dtype=float32>), <tf.Tensor 'ones:0' shape=(1, 256) dtype=float32>]

Second structure: type=list str=[TensorShape([]), TensorShape([Dimension(None), Dimension(256)])]

我试图实现与拼接在一起,每个字矩阵,但随着q_sate改变它原来是错的

但我试过很多方法都是错误的,所以我希望能得到您的帮助,但我试过很多方法都是错误的,所以我希望能得到您的帮助,但我试过很多方法都是错误的,所以我希望能得到您的帮助

tensorflow deep-learning nlp
1个回答
1
投票

变量loop_vars你输入三种,butshape_invariants你输入两种。因此,错误显示了两个结构不具有相同的嵌套结构。你只需要添加q_state的结构。

# main loop
time, q_state, out_ = tf.while_loop(
    cond=lambda time, *_: time < question_length,
    body=_one_step,
    loop_vars=[time, q_state, out_],
    shape_invariants=[time.get_shape()
        ,tf.nn.rnn_cell.LSTMStateTuple(tf.TensorShape([None, 300]),tf.TensorShape([None, 300]))
        ,tf.TensorShape([None, 256])]
)
© www.soinside.com 2019 - 2024. All rights reserved.