簡體   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