python - Tensorflow 错误 : Variable gru_def/rnn/gru_cell/gates/kernel does not exist, 或不是使用 tf.get_variable() 创建的

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

这是我第一次使用 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()    

当我运行代码时出现此错误:

Variable gru_def/rnn/gru_cell/gates/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

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

然后,我向 GRUCell 添加一个 reuse 参数,但出现此错误:

ValueError: Variable gru_def/rnn/gru_cell/gates/kernel does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?

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

你能帮我一下吗?

最佳答案

在 tensorflow 1.3+ 中:

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

关于python - Tensorflow 错误 : Variable gru_def/rnn/gru_cell/gates/kernel does not exist, 或不是使用 tf.get_variable() 创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137219/

相关文章:

python - Http 错误 : 400 Missing Required Parameter while using jobs. query() BQ API

python 在 linux IPPROTO_ICMP 中具有非特权 ping

python - 两层神经网络 Tensorflow python

machine-learning - 如何在 TensorFlow 中重复未知维度

python - TensorFlow 和线程

neural-network - 如何按照全局步骤在 Keras 中实现指数衰减学习率

c# - C# 样本/用法的 Encog 正则化

python - 在python中使用rpy2将分位数输入ggplot geom_boxplot

用于基于文本的游戏的 Python GUI 工具包?

python - 安装旧版本的 Tensorflow GPU