Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TensorBoard用プログラムの追加ver
# http://192.168.99.1:6006
import tensorflow as tf
import numpy as np
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
#MNISTデータの読み込み
mnist = read_data_sets("MNIST_data/", one_hot=True)
with tf.name_scope("input_x") as scope:
x = tf.placeholder("float", [None, 784])
with tf.name_scope("label_y_") as scope:
y_ = tf.placeholder("float",[None, 10])
W = tf.Variable(tf.zeros([784, 10]),name="Weights")
b = tf.Variable(tf.zeros([10]),name="bias")
with tf.name_scope("output_y") as scope:
y = tf.nn.softmax(tf.matmul(x, W) + b)
#histgramの追加
w_hist = tf.summary.histogram("weights", W)
b_hist = tf.summary.histogram("biases", b)
y_hist = tf.summary.histogram("y", y)
with tf.name_scope("cross_entropy") as scope:
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
#scalarの追加
cross_entropy_summary = tf.summary.scalar("cross_entropy", cross_entropy)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
with tf.name_scope("accuracy") as scope:
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
##scalarの追加
accuracy_summary = tf.summary.scalar("accuracy", accuracy)
sess = tf.Session()
#すべてのsummariesをmerge
merged = tf.summary.merge_all()
#logに書き出し
writer = tf.summary.FileWriter("log", sess.graph)
sess.run(tf.global_variables_initializer())
#1000回の学習開始
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
if i % 10 == 0:
#すべてのsummaryを記録
summary_str = sess.run(merged,feed_dict={x: batch_xs, y_: batch_ys})
writer.add_summary(summary_str, i)
else:
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#Testデータを用いた精度計算
print (sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment