首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Tensorflow错误:变量gru_def/rnn/gru_cell/网关/内核不存在,或者不是用tf.get_variable()创建的

Tensorflow错误:变量gru_def/rnn/gru_cell/网关/内核不存在,或者不是用tf.get_variable()创建的
EN

Stack Overflow用户
提问于 2018-01-07 12:44:33
回答 1查看 665关注 0票数 2

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

代码语言:javascript
运行
复制
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/gate/内核已经存在,不允许。你是想把reuse=True或reuse=tf.AUTO_REUSE设置在VarScope中吗?最初的定义是:

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

然后,我将一个重用参数添加到GRUCell中,但我有以下错误:

ValueError:变量gru_def/rnn/gru_cell/ was /内核不存在,或者不是用tf.get_variable()创建的。你是想把reuse=tf.AUTO_REUSE设为VarScope吗?

代码语言:javascript
运行
复制
cell = tf.contrib.rnn.GRUCell(num_units = rnn_size,reuse=True)

你能帮帮我吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-07 12:53:49

在tensorflow中,1.3+:

代码语言:javascript
运行
复制
cell = tf.contrib.rnn.GRUCell(num_units=rnn_size, reuse=tf.AUTO_REUSE)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48137219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档