【Tensorflow】报错:ValueError: At least two variables have the same name

案例1

问题描述:

import tensorflow as tf

with tf.Session() as sess:
    var = tf.Variable(42, name='var')
    sess.run(tf.global_variables_initializer())
    tf.train.export_meta_graph('file.meta')

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('file.meta')
    print sess.run(var)
以上代码在saver = tf.train.import_meta_graph('file.meta') 报错:ValueError: At least two variables have the same name: var.

更改代码如下:

import tensorflow as tf

file_name = "./file"

with tf.Session() as sess:
    var = tf.Variable(42, name='my_var')
    sess.run(tf.global_variables_initializer())

    saver = tf.train.Saver()
    saver.save(sess,file_name)
    saver.export_meta_graph(file_name + '.meta')

with tf.Session() as sess:
    saver = tf.train.import_meta_graph(file_name + '.meta')
    saver.restore(sess, file_name)
    print(sess.run(var))

    # new code that fails:
    saver = tf.train.Saver()
    saver.save(sess,file_name)
    saver.export_meta_graph(file_name + '.meta')

第一次运行得到正确的var值,但是2秒钟之后,再次报错:ValueError: At least two variables have the same name: var

问题解决:

第一部分代码的主要问题是,你在模型的图(TensorFlow graph)中定义了变量var,随后又尝试加载它。

因此,在重新导入tensorflow的图形之前,先清空它:

tf.reset_default_graph()
使用方法如下:

import tensorflow as tf

with tf.Session() as sess:
    var = tf.Variable(42, name='var')
    sess.run(tf.global_variables_initializer())
    tf.train.export_meta_graph('file.meta')
tf.reset_default_graph()
with tf.Session() as sess:
    saver = tf.train.import_meta_graph('file.meta')
    var = tf.global_variables()[0]
    sess.run(tf.initialize_all_variables())
    print sess.run(var)

The reason your intermediate code wasn't working was that tf.get_variable() was creating a new variable which was being randomly initialized. Make sure you first do tf.get_variable_scope().reuse_variables() first. Have a look at Understanding tf.get_variable().

第二部分代码没有工作是因为 tf.get_variable()创建了一个新变量,并随机初始化了。

确保你执行了tf.get_variable_scope().reuse_variables() ,可以在这里理解一下tf.get_variable().

此外不幸的是,你用tf.Variable()创建的变量不能直接被tf.get_variable()恢复。看一下案例1案例2

因此,如果将来想要恢复变量,就需要用tf.get_variable()来创建。


案例2

问题描述:

以下是我的代码

import tensorflow as tf
import numpy as np
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
sess.run(train)
这部分工作了:
saver = tf.train.Saver()
saver.save(sess, 'my-model')
sess = tf.Session()
new_saver = tf.train.import_meta_graph('my-model.meta')
但是这部分没有工作:

tf.train.export_meta_graph(filename='my-model.meta')
new_saver = tf.train.import_meta_graph('my-model.meta')

报错:ValueError: At least two variables have the same name: Variable_1

问题解决:

你应该调用 tf.train.import_meta_graph()来得到一个明确的图,例如:

    # Create a clean graph and import the MetaGraphDef nodes.
    new_graph = tf.Graph()
    with tf.Session(graph=new_graph) as sess:
      # Import the previously export meta graph.
      saver = tf.train.import_meta_graph(meta_graph_def)
如果还没解决问题,重启一下即可。


翻译来源:

https://stackoverflow.com/questions/41400391/tensorflow-saving-and-resoring-session-multiple-variables

https://github.com/tensorflow/tensorflow/issues/4603

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读