繁体   English   中英

在存储会话时在张量流中出现错误“无变量可保存”

[英]Error “no Variable to save” in tensorflow while storing session

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

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

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

此外,您可能希望像在将tf.Session用作sess时所使用的那样,在savePath = saver.save(sess,“ /tmp/model.ckpt”)之前插入一个缩进,因此,当您不在该会话中时,该会话实际上是关闭的阻止,那么您将面临“尝试使用封闭会话”的问题。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM