ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – TensorFlow FileWriter没有写入文件

2019-06-08 12:43:05  阅读:215  来源: 互联网

标签:python tensorflow logging filewriter


我正在训练一个简单的TensorFlow模型.训练方面工作正常,但没有日志写入/ tmp / tensorflow_logs,我不知道为什么.有人能提供一些见解吗?谢谢

# import MNIST
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf

# set parameters
learning_rate = 0.01
training_iteration = 30
batch_size = 100
display_step = 2

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

# create a model

# set model weights
# 784 is the dimension of a flattened MNIST image
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

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

# add summary ops to collect data
w_h = tf.summary.histogram("weights", W)
b_h = tf.summary.histogram("biases", b)

with tf.name_scope("cost_function") as scope:
    # minimize error using cross entropy
    cost_function = -tf.reduce_sum(y*tf.log(model))
    # create a summary to monitor the cost function
    tf.summary.scalar("cost_function", cost_function)

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

init = tf.global_variables_initializer()

# merge all summaries into a single operator
merged_summary_op = tf.summary.merge_all()

# launch the graph
with tf.Session() as sess:
    sess.run(init)

    # set the logs writer to the folder /tmp/tensorflow_logs
    summary_writer = tf.summary.FileWriter('/tmp/tensorflow_logs', graph=sess.graph)

    # training cycle
    for iteration in range(training_iteration):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # loop over all batches
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            # fit training using batch data
            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
            # compute the average loss
            avg_cost += sess.run(cost_function, feed_dict={x: batch_xs, y: batch_ys})/total_batch
            # write logs for each iteration
            summary_str = sess.run(merged_summary_op, feed_dict={x: batch_xs, y: batch_ys})
            summary_writer.add_summary(summary_str, iteration*total_batch + i)
        # display logs per iteration step
        if iteration % display_step == 0:
            print("Iteration:", '%04d' % (iteration + 1), "cost= ", "{:.9f}".format(avg_cost))

    print("Tuning completed!")

    # test the model
    predictions = tf.equal(tf.argmax(model, 1), tf.argmax(y, 1))
    # calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(predictions, "float"))
    print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))


print("Success!")

解决方法:

将文件路径从/ temp / …更改为temp / …并添加summary_writer.flush()和summary_writer.close()的组合使得日志成功写入.

标签:python,tensorflow,logging,filewriter
来源: https://codeday.me/bug/20190608/1197757.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有