我试图保存一个基本的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
我只能访问" checkpoint"的内容。文件,即:
model_checkpoint_path: "mnist_to-save-saved"
all_model_checkpoint_paths: "mnist_to-save-saved"
我的问题是,在执行训练模型文件时,为什么我会同时得到此错误跟踪,以及如何解决?
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,但不适合训练。
答案 0 :(得分:1)
以下是代码的摘录
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-save-saved'
这很可能导致问题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
,因为您的路径正在使用root&#34; /&#34;而不是相对的&#34; ./"
将/mnist-to-save-saved
替换为./mnist-to-save-saved
或更好地在代码顶部定义变量,例如
TF_SAVE_FILE = './mnist-to-save-saved'
然后在整个代码中使用该变量,而不是复制它。