简体   繁体   English

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

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

I was trYing to save session in model so that i can use it later on but i am getting an error everytime. 我试图将会话保存在模型中,以便以后可以使用它,但是每次都会出错。 My code is like: 我的代码是这样的:

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)

and the error is like: 和错误是这样的:

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

The code you provided does not give much information on the error. 您提供的代码没有提供有关该错误的太多信息。 You might need to check your previous code to see if you actually have an variables to be saved. 您可能需要检查以前的代码,以查看是否确实有要保存的变量。 You can check tf.global_variables() and see if the list is empty. 您可以检查tf.global_variables()并查看列表是否为空。

In addition, you might want to put an indent before the savedPath = saver.save(sess, "/tmp/model.ckpt") as you used with tf.Session as sess, so the session is actually closed when you are outside that block, then you'll face the problem of 'Attempting to use closed session'. 此外,您可能希望像在将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