python - tensorflow 错误 : 'FileWriter' method

标签 python machine-learning tensorflow runtime-error classification

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)

import tensorflow as tf

learning_rate = 0.01
training_iteration = 30
batch_size = 100
display_step = 2

x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10])

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

with tf.name_scope("Wx_b") as scope:
    model = tf.nn.softmax(tf.matmul(x, W) + b)

w_h = tf.summary.histogram("weights", W)
b_h = tf.summary.histogram("biases", b)

with tf.name_scope("cost_function") as scope:
    cost_function = -tf.reduce_sum(y*tf.log(model))
    tf.summary.scalar("cost_function", cost_function)

with tf.name_scope("train") as scope:
    optimizer =         tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)

init = tf.global_variables_initializer()
merged_summary_op = tf.summary.merge_all()


with tf.Session() as sess:
    sess.run(init)
    summary_writer = tf.summary.FileWriter('/home/sergo/work/logs',graph_def = sess.graph_def)   #I GET AN ERROR IN THIS FileWriter method
    for iteration in range(training_iteration):
            avg_cost = 0.
            total_batch = int(mnist.train.num_examples/batch_size)
            for i in range(total_batch):
                    batch_xs, batch_ys = mnist.train.next_batch(batch_size)
                    sess.run(optimizer, feed_dict = {x: batch_xs, y: batch_ys})
                    avg_cost += sess.run(cost_function, feed_dict={x:batch_xs, y: batch_ys})/total_batch

        # write logs for each iteration
                    sessummary_str = sess.run(merged_summary_op, feed_dict={x: batch_xs, y: batch_ys})
                    summary_writer.add_summary(summary_str, interation*total_batch + i)

    if iteration % display_step == 0:
            print("Iteration:", '%04d' % (iteration + 1), "cost=", "{:.9f}".format(avg_cost))





    #When finished:
    print("Turning completed!")

    predictions = tf.equal(tf.argmax(model, 1), tf.argmax(y,1))

    accuracy = tf.reduce_mean(tf.cast(predictions, "float"))
    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

您好,我正在使用 YouTube 教程中的 python3.6 示例代码和 Tensorflow 1.0。

我在第 36 行使用 FileWriter 方法收到错误,如下所示:

Traceback (most recent call last):
File "/Users/cliang/Desktop/tfclass.py", line 36, in <module>
summary_writer = tf.summary.FileWriter('/home/sergo/work/logs',graph_def = sess.graph_def)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/summary/writer/writer.py", line 308, in __init__
event_writer = EventFileWriter(logdir, max_queue, flush_secs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/summary/writer/event_file_writer.py", line 69, in __init__
gfile.MakeDirs(self._logdir)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py", line 301, in recursive_create_dir
pywrap_tensorflow.RecursivelyCreateDir(compat.as_bytes(dirname), status)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 89, in __exit__
next(self.gen)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 469, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.UnimplementedError: /home/sergo

有人知道如何修复这个错误以及“/home/sergo/work/logs”路径名参数的含义吗?

非常感谢任何帮助。

谢谢!

(我使用的是 Mac 0SX Yosemite 10.10.5)

最佳答案

在行中, summary_writer = tf.summary.FileWriter('/home/sergo/work/logs',graph_def = sess.graph_def) #我在这个 FileWriter 方法中遇到错误

它明确提到路径/home/sergo/work/logs

这个路径在你的本地机器上可能不存在 所以请提供一条存在的路径,应该可以解决您的问题

谢谢

关于python - tensorflow 错误 : 'FileWriter' method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42464109/

相关文章:

python - 将大型 NumPy 数组写入文件的有效方法

python - python中为什么字符串被分割成字符

python - Accuracy_score 出现错误

python - 逻辑回归得到 sm.Logit 值(python,statsmodels)

machine-learning - 使用 Sklearn 进行多标签分类

python - TF Keras 如何在加载模型时获得预期的输入形状?

python - pyplot TwoSlopeNorm LinearSegmentedColormap 中零值的唯一颜色

python - 打开工作区会导致 python.pythonPath 被忽略

apache-spark - 如何在 EC2 Spark 集群上训练深度神经网络(tensorflow)?

python - 在 TensorFlow 中从头开始训练 Inception