这是我第一次使用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/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吗?
cell = tf.contrib.rnn.GRUCell(num_units = rnn_size,reuse=True)你能帮帮我吗。
发布于 2018-01-07 12:53:49
在tensorflow中,1.3+:
cell = tf.contrib.rnn.GRUCell(num_units=rnn_size, reuse=tf.AUTO_REUSE)https://stackoverflow.com/questions/48137219
复制相似问题