【问题标题】:Error "no Variable to save" in tensorflow while storing session存储会话时在张量流中出现错误“没有要保存的变量”
【发布时间】:2018-06-27 07:13:28
【问题描述】:

我试图在模型中保存会话,以便以后可以使用它,但每次都会出错。我的代码是这样的:

with tf.Session() as sess:
    sess.run(init)
    for j in range(3):
        for i in range(xtest.shape[0]):

            _, indices = sess.run(pred, feed_dict={x_train: xtrain, x_test: xtest[i,:]})
            pred_label = getMajorityPredictions(ytrain, indices) 
            actual_val = get_char( int( (ytest[i]).argmax() ) )

            # print("test: ", i, "prediction:       ", get_char(pred_label), "          actual:            ",   actual_val)
            # print(pred_label, actual_val, type(pred_label), type(actual_val), sep=" --> ")
            if get_char(pred_label) == actual_val:
                accuracy += 1/len(xtest)

            # print((i / (xtest.shape[0])) * 100)
            # os.system("cls")
                print("accuracy: ",accuracy)

    savedPath = saver.save(sess, "/tmp/model.ckpt")
    print("Model saved at: " ,savedPath)

错误是这样的:

Traceback (most recent call last):
File "prac3.py", line 74, in <module>
    saver = tf.train.Saver()
File "C:\Python36\lib\site-packages\tensorflow\python\training\saver.py", line 1239, in __init__
    self.build()
File "C:\Python36\lib\site-packages\tensorflow\python\training\saver.py", line 1248, in build
    self._build(self._filename, build_save=True, build_restore=True)
File "C:\Python36\lib\site-packages\tensorflow\python\training\saver.py", line 1272, in _build
    raise ValueError("No variables to save")
ValueError: No variables to save

【问题讨论】:

标签: python tensorflow machine-learning deep-learning artificial-intelligence


【解决方案1】:

您提供的代码没有提供有关错误的太多信息。您可能需要检查以前的代码,看看您是否真的有要保存的变量。您可以检查 tf.global_variables() 并查看列表是否为空。

此外,您可能希望在 savedPath = saver.save(sess, "/tmp/model.ckpt") 之前放一个缩进,就像您将 tf.Session 用作 sess 一样,因此当您使用 sess 时会话实际上已关闭位于该块之外,那么您将面临“尝试使用封闭会话”的问题。

【讨论】:

  • 嗨mukul,问题是你实际上没有任何变量。你只用了 tf.abs, tf.subtract, tf.nn.top_k... 等等,这些都是没有参数的函数。您的模型是恒定的,因此您无法保存它,没有什么可以保存的。
【解决方案2】:
x_train = tf.placeholder(tf.float32, shape=[None, 4096])           
y_train = tf.placeholder(tf.float32, shape=[None, 62])
x_test = tf.placeholder(tf.float32, shape=[4096])           
y_test = tf.placeholder(tf.float32, shape=[None, 62])

l1_distance = tf.abs(tf.subtract(x_train, x_test))
dis_l1 = tf.reduce_sum(l1_distance, axis=1)
pred = tf.nn.top_k(tf.negative(dis_l1), k=5)

xtrain, ytrain = TRAIN_SIZE(2852)
xtest, ytest = TEST_SIZE(557)

init = tf.global_variables_initializer()
accuracy = 0
saver = tf.train.Saver()
# --------------------- to create model 
with tf.Session() as sess:
    sess.run(init)
    for j in range(3):
        for i in range(xtest.shape[0]):

            _, indices = sess.run(pred, feed_dict={x_train: xtrain, x_test: xtest[i,:]})
            pred_label = getMajorityPredictions(ytrain, indices) 
            actual_val = get_char( int( (ytest[i]).argmax() ) )

            # print("test: ", i, "prediction:       ", get_char(pred_label), "          actual:            ",   actual_val)
            # print(pred_label, actual_val, type(pred_label), type(actual_val), sep=" --> ")
            if get_char(pred_label) == actual_val:
                accuracy += 1/len(xtest)

            # print((i / (xtest.shape[0])) * 100)
            # os.system("cls")
                print("accuracy: ",accuracy)

    savedPath = saver.save(sess, "/tmp/model.ckpt")
    print("Model saved at: " ,savedPath)

【讨论】:

    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多