【问题标题】:Tensorflow error: Variable gru_def/rnn/gru_cell/gates/kernel does not exist, or was not created with tf.get_variable()Tensorflow 错误:变量 gru_def/rnn/gru_cell/gates/kernel 不存在,或者不是使用 tf.get_variable() 创建的
【发布时间】:2018-01-07 12:44:33
【问题描述】:

这是我第一次使用 python 和 tensorflow 进行编程。我想使用动态 RNN 来构建句子嵌入。 这是我用 jupyter 编写的代码的一部分。

graph = tf.Graph()
x_data = tf.placeholder(tf.int32, [None,None])
sequence_lengths = tf.placeholder(tf.int32, shape=[None])
embedding_mat = tf.Variable(tf.random_uniform([vocab_size,embedding_size],  0.0, 1.0),dtype=tf.float32)
embedding_output = tf.nn.embedding_lookup(embedding_mat,x_data)

with tf.variable_scope('cell_def'):
    cell =tf.contrib.rnn.GRUCell(num_units = rnn_size)
    hidden_state_in =cell.zero_state(batch_size,tf.float32) 
with tf.variable_scope('gru_def'):
    output, state = tf.nn.dynamic_rnn(cell,embedding_output,initial_state=hidden_state_in,dtype=tf.float32,sequence_length=sequence_lengths)

with tf.Session(graph=graph) as sess:
    sess.run(tf.global_variables_initializer())
    feed_dict = {x_data:embedding_output}
    sess.run(output,feed_dict=feed_dict)
    #tf.get_variable_scope().reuse_variables()    
sess.close()    

当我运行我的代码时,我得到了这个错误:

变量 gru_def/rnn/gru_cell/gates/kernel 已经存在,不允许。 您的意思是在 VarScope 中设置 reuse=True 或 reuse=tf.AUTO_REUSE 吗? 最初定义于:

我尝试通过调用 tf.get_variable_scope().reuse_variables() 将重用标志设置为 True 来解决此问题,但错误仍然存​​在。

然后,我在 GRUCell 中添加了一个 reuse 参数,但出现了这个错误:

ValueError: 变量 gru_def/rnn/gru_cell/gates/kernel 不存在, 或者不是用 tf.get_variable() 创建的。你的意思是设置 VarScope 中的重用=tf.AUTO_REUSE?

cell = tf.contrib.rnn.GRUCell(num_units = rnn_size,reuse=True)

你能帮帮我吗?

【问题讨论】:

    标签: python tensorflow neural-network deep-learning recurrent-neural-network


    【解决方案1】:

    在张量流 1.3+ 中:

    cell = tf.contrib.rnn.GRUCell(num_units=rnn_size, reuse=tf.AUTO_REUSE)
    

    【讨论】:

    • 感谢您的回复。它有效,但我收到另一个错误:“TypeError:提要的值不能是 tf.Tensor 对象。可接受的提要值包括 Python 标量、字符串、列表、numpy ndarray 或 TensorHandles。”我认为我对 feed_dict 有问题,特别是 tf.nn.embedding_lookup 返回一个张量。你能帮我解决这个问题吗?
    • 您最好写一个不同的问题,以便我或其他人可以回答。但简而言之,您需要使用索引一词。 embedding_output 是你提供给 RNN 的嵌入,这是正确的,但不是 feed_dict
    • 对不起。我现在点击了复选标记。感谢您的回复。如果您的建议后我无法解决问题,我会写一个不同的问题。
    猜你喜欢
    • 2017-08-28
    • 2018-05-26
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多