gpt4 book ai didi

python - TensorFlow 保存模型 - 自相矛盾的异常

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:10 30 4
gpt4 key购买 nike

我尝试保存一个基本的 MNIST 模型:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

saver = tf.train.Saver()

sess.run(tf.global_variables_initializer())

y = tf.matmul(x, W) + b
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

saver.save(sess, './mnist_to-save-saved')

for _ in range(1000):
batch = mnist.train.next_batch(100)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver.save(sess, '/mnist-to-save-saved', global_step=1000)

print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

它似乎部分起作用,因为在这个模型 .py 文件的同一目录中生成了一些文件:

mnist_to-save-daved.index
mnist_to-save_saved.meta
mnist_to-save.saved.data-00000-of-00001
checkpoint

我只能访问“检查点”文件的内容,即:

model_checkpoint_path: "mnist_to-save-saved"
all_model_checkpoint_paths: "mnist_to-save-saved"

我的问题是,在执行训练模型文件的时候,为什么会同时出现这个error trace,如何解决?

C:\Users\username\Anaconda3\envs\tensorflowenv\python.exe C:/Users/username/PycharmProjects/estimator/mnist-to-save.py
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
2018-04-15 18:22:39.959614: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1192] Permission denied: Failed to create a directory: /; Permission denied
Traceback (most recent call last):
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1323, in _do_call
return fn(*args)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1302, in _run_fn
status, run_metadata)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.PermissionDeniedError: Failed to create a directory: /; Permission denied
[[Node: save/SaveV2 = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, Variable, Variable_1)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/username/PycharmProjects/estimator/mnist-to-save.py", line 28, in <module>
saver.save(sess, '/mnist-to-save-saved', global_step=1000)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1573, in save
{self.saver_def.filename_tensor_name: checkpoint_file})
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
run_metadata_ptr)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1317, in _do_run
options, run_metadata)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\client\session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.PermissionDeniedError: Failed to create a directory: /; Permission denied
[[Node: save/SaveV2 = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, Variable, Variable_1)]]

Caused by op 'save/SaveV2', defined at:
File "C:/Users/username/PycharmProjects/estimator/mnist-to-save.py", line 11, in <module>
saver = tf.train.Saver()
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1218, in __init__
self.build()
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1227, in build
self._build(self._filename, build_save=True, build_restore=True)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 1263, in _build
build_save=build_save, build_restore=build_restore)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 748, in _build_internal
save_tensor = self._AddSaveOps(filename_tensor, saveables)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 296, in _AddSaveOps
save = self.save_op(filename_tensor, saveables)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\training\saver.py", line 239, in save_op
tensors)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 1162, in save_v2
shape_and_slices=shape_and_slices, tensors=tensors, name=name)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "C:\Users\username\Anaconda3\envs\tensorflowenv\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

PermissionDeniedError (see above for traceback): Failed to create a directory: /; Permission denied
[[Node: save/SaveV2 = SaveV2[dtypes=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/SaveV2/tensor_names, save/SaveV2/shape_and_slices, Variable, Variable_1)]]


Process finished with exit code 1

它似乎保存了预训练的 mdel,但在训练中效果不佳。

最佳答案

这是你的代码的摘录

saver.save(sess, './mnist_to-save-saved')

for _ in range(1000):
batch = mnist.train.next_batch(100)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver.save(sess, '/mnist-to-save-saved', global_step=1000)

问题

最后一行有 saver.save(sess, '/mnist-to-save-saved' ,应该是 saver.save(sess, './mnist-to -保存-保存'

这很可能导致问题 W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\framework\op_kernel.cc:1192] Permission denied: Failed创建一个目录:/;权限被拒绝,因为您的路径使用根“/”而不是相对“./”

解决方案

/mnist-to-save-saved 替换为 ./mnist-to-save-saved 或更好地在代码顶部定义一个变量,例如

TF_SAVE_FILE = './mnist-to-save-saved'

然后在整个代码中使用该变量而不是复制它。

关于python - TensorFlow 保存模型 - 自相矛盾的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49844290/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com