首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tensorflow RNN实现

tensorflow RNN实现
EN

Stack Overflow用户
提问于 2017-08-13 22:30:33
回答 1查看 222关注 0票数 1

我正在构建一个RNN模型来进行图像分类。我使用管道来输入数据。然而,它会返回

代码语言:javascript
复制
ValueError: Variable rnn/rnn/basic_rnn_cell/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

我想知道我能做些什么来解决这个问题,因为使用输入管道实现RNN的例子并不多。我知道如果我使用占位符就可以了,但是我的数据已经是以张量的形式存在了。除非我可以向占位符提供张量,否则我更喜欢使用管道。

代码语言:javascript
复制
def RNN(inputs):
with tf.variable_scope('cells', reuse=True):
    basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=batch_size)

with tf.variable_scope('rnn'):
    outputs, states = tf.nn.dynamic_rnn(basic_cell, inputs, dtype=tf.float32)

fc_drop = tf.nn.dropout(states, keep_prob)

logits = tf.contrib.layers.fully_connected(fc_drop, batch_size, activation_fn=None)

return logits

#Training
with tf.name_scope("cost_function") as scope:
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=train_label_batch, logits=RNN(train_batch)))
    train_step = tf.train.MomentumOptimizer(learning_rate, 0.9).minimize(cost)


#Accuracy
with tf.name_scope("accuracy") as scope:
    correct_prediction = tf.equal(tf.argmax(RNN(test_image), 1), tf.argmax(test_image_label, 0))
    accuracy = tf.cast(correct_prediction, tf.float32)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-13 22:41:27

您需要正确使用reuse选项。下面的更改将会解决这个问题。对于预测,您需要使用图中已经存在的变量。

代码语言:javascript
复制
def RNN(inputs, reuse):
    with tf.variable_scope('cells', reuse=reuse):
         basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=batch_size, reuse=reuse)

    ...

...
#Training
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=train_label_batch, logits=RNN(train_batch, reuse=None)))

#Accuracy
...
    correct_prediction = tf.equal(tf.argmax(RNN(test_image, reuse=True), 1), tf.argmax(test_image_label, 0))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45661424

复制
相关文章

相似问题

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