Tensorflow学习5-3:Tensorboard可视化

对 5-2 进行修改,加入放映器(projector)实现分类过程中图片可视化

例子

1
2
3
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.tensorboard.plugins import projector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#载入数据集
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

# ---------------------------NEW ADD 1
#运行次数
max_steps = 31
#图片数量
image_num = 3000
#文件路径
DIR = "D:/Tensorflow/"

#定义会话
sess = tf.Session()

#载入图片(将n个行向量堆叠起来)
embedding = tf.Variable(tf.stack(mnist.test.images[:image_num]), trainable=False, name = "embadding")
# ------------------------------------

#每个批次大小
batch_size = 100

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

1
2
3
4
5
6
7
8
9
10
11
# STEP 1 设计统计函数,用于计算传入的张量的各种统计量
def variable_summary(var):
with tf.name_scope("summary"):
mean = tf.reduce_mean(var)
tf.summary.scalar("mean", mean) #平均值
with tf.name_scope("stddev"):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar("stddev", stddev) #标准差
tf.summary.scalar("max", tf.reduce_max(var)) #最大值
tf.summary.scalar("min", tf.reduce_min(var)) #最小值
tf.summary.histogram("histogram", var) #直方图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
''' DEL  这次数据集很大,不计算批次数,而是自己指定 batch_num 即 max_steps
#计算有多少批次 = 总数 // 批次
batch_num = mnist.train.num_examples // batch_size

'''


with tf.name_scope("input"):
input_x = tf.placeholder(tf.float32, [None, 784], name="input_x")
input_y = tf.placeholder(tf.float32, [None, 10], name="input_y")

# --------------------------------NEW ADD 2
#显示图片
with tf.name_scope("input_reshape"):
image_shape_input = tf.reshape(input_x, [-1,28,28,1]) # -1表示未知
tf.summary.image("input", image_shape_input, 10) # 10张图片用来在 IMAGES 选项中预览
# ------------------------------------

#创建神经网络模型
with tf.name_scope("layers"):
W1 = tf.Variable(tf.truncated_normal([784,128], 0.,0.5), name="W1")
variable_summary(W1) # STEP 2.1
b1 = tf.Variable(tf.zeros([128]) + 0.1, name="b1")
variable_summary(b1) # STEP 2.1
L1 = tf.nn.relu(tf.matmul(input_x, W1) + b1, name="L1")

W2 = tf.Variable(tf.truncated_normal([128,10], 0.,0.5), name="W2")
b2 = tf.Variable(tf.zeros([10]) + 0.1, name="b2")
L2 = tf.add(tf.matmul(L1, W2), b2, name="L2")
with tf.name_scope("loss"):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=L2, labels=input_y))
tf.summary.scalar("loss", loss) # STEP 2.2
with tf.name_scope("train_and_optimizer"):
train = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#获取用于显示的精度——优化效果
with tf.name_scope("accuracy"):
correct_indices = tf.equal(tf.argmax(input_y, 1), tf.argmax(L2, 1))
accuracy = tf.reduce_mean(tf.cast(correct_indices, tf.float32))
tf.summary.scalar("accuracy", accuracy) # STEP 2.2


# -----------------------NEW ADD 3
#产生metadata文件,写入测试集的10进制labels
if tf.gfile.Exists(DIR + "projector/projector/meadata.tsv"):
tf.gfile.DeleteRecursively(DIR + "projector/projector/metadata.tsv")
with open(DIR + "projector/projector/metadata.tsv", "w") as f:
labels = sess.run(tf.argmax(mnist.test.labels[:], 1)) # 把one-hot转为10进制标签
for i in range(image_num):
f.write(str(labels[i]) + "\n")

projector_writer = tf.summary.FileWriter(DIR + "projector/projector/", sess.graph)
saver = tf.train.Saver() #保存模型
config = projector.ProjectorConfig()
embed = config.embeddings.add()
embed.tensor_name = embedding.name
embed.metadata_path = DIR + "projector/projector/metadata.tsv"
embed.sprite.image_path = DIR + "projector/data/mnist_10k_sprite.png"
embed.sprite.single_image_dim.extend([28,28])
projector.visualize_embeddings(projector_writer, config)
# ------------------------------------

# STEP 3:合并所有summary
merged = tf.summary.merge_all()

''' DEL
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
# STEP 2
writer = tf.summary.FileWriter("D:/Tensorflow/logs", sess.graph)
for epoch in range(5):
for batch in range(batch_num):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
# STEP 4:每次训练,计算一次merge summary
summary,_ = sess.run([merged, train], feed_dict={input_x:batch_xs, input_y:batch_ys})
# STEP 5:选择多久更新写入一次summary
writer.add_summary(summary, epoch)
_accuracy = sess.run(accuracy, feed_dict={input_x:mnist.test.images, input_y:mnist.test.labels})
print("epoch:"+ str(epoch) + ", accuracy:"+ str(_accuracy))
'''

# -----------------------NEW ADD 4:由于自己指定batch_num 数量很多,所以不需要来回迭代,修改如下
sess.run(tf.global_variables_initializer())

writer = tf.summary.FileWriter("D:/Tensorflow/logs", sess.graph)
for i in range(max_steps):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)

# 这里加入2个参数
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
summary,_ = sess.run([merged, train], feed_dict={input_x:batch_xs, input_y:batch_ys},
options = run_options, run_metadata = run_metadata)

projector_writer.add_run_metadata(run_metadata, "step%03d" % i)
projector_writer.add_summary(summary, i)

if i%3 == 0:
_accuracy = sess.run(accuracy, feed_dict={input_x:mnist.test.images, input_y:mnist.test.labels})
print("step:"+ str(i) + ", accuracy:"+ str(_accuracy))

# 模型保存到如下路径
saver.save(sess, DIR + "projector/projector/a_model.ckpt", global_step = max_steps)
projector_writer.close()
sess.close()

# -----------------------------

step:0, accuracy:0.1275
step:3, accuracy:0.3143
step:6, accuracy:0.5248
step:9, accuracy:0.5986
step:12, accuracy:0.6266
step:15, accuracy:0.6751
step:18, accuracy:0.6905
step:21, accuracy:0.7041
step:24, accuracy:0.7387
step:27, accuracy:0.747
step:30, accuracy:0.7538


总结

输入下面代码运行

tensorboard --logdir=D:/Tensorflow/projector/projector

可以在 Tensorboard 的 IMAGES 和 EMBEDDINGS 选项卡查看新效果

效果如图:

꧁༺The༒End༻꧂