python - 存储 session 时 tensorflow 中出现错误 "no Variable to save"

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

我试图将 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() 并查看列表是否为空。

此外,您可能需要在 savingPath = saver.save(sess, "/tmp/model.ckpt") 之前添加一个缩进,就像与 tf.Session as sess 一起使用一样,因此当您使用 tf.Session 时, session 实际上已关闭位于该 block 之外,那么您将面临“尝试使用封闭 session ”的问题。

关于python - 存储 session 时 tensorflow 中出现错误 "no Variable to save",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056650/

相关文章:

找不到主页的 PythonAnywhere Django 404 页面

Python Keras 代码无缘无故内存不足

python - 以有效的方式过滤2 numpy.ndarray中的相似图像

python - Tensorflow GPU 设置 : error with CUDA on PyCharm

machine-learning - Keras:是否有任何解决方法可以在不使用 Lamda 层的情况下分割中间层的输出?

machine-learning - 非离散(连续)数据的信息增益计算

python - 使用 apache 在子目录中运行一个 flask 网站

python - 使用Python检索Excel中的标题

python - 使用多处理在 Python 中制作并行版本的 map 函数时出现 Pickle 错误

python - 为什么要在 tensorflow 中构建用于训练和验证的分离图?