/
TFCircleTensorBoard.py
76 lines (66 loc) · 2.35 KB
/
TFCircleTensorBoard.py
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
"""
github : https://github.com/amingolnari/Deep-Learning-Course
Author : Amin Golnari
TF Version : 1.12.0
Date : 4/12/2018
TensorFlow Circle Classification and Visualize with TensorBoard
Code 204
"""
import numpy as np
import tensorflow as tf
tf.reset_default_graph() # Reset Graph
import matplotlib.pyplot as plt
from sklearn import datasets
# Make Moon Data
moon_X, moon_Y = datasets.make_circles(n_samples = 500,
factor = .2,
noise = .1,
random_state = 0)
# Initialization
InputShape = 2
NumClass = 1
Hidden = 20
lr = 0.5
Epochs = 70
NumShow = 10
# TF Input Graph and Target
Input = tf.placeholder(tf.float32, [None, InputShape])
Target = tf.placeholder(tf.float32, [None, NumClass])
# Hidden Layer #1
Hidden1 = tf.layers.dense(inputs = Input,
units = Hidden,
activation = tf.nn.tanh)
# TF Output Graph
Out = tf.layers.dense(inputs = Hidden1,
units = NumClass,
activation = tf.nn.sigmoid)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = Out, labels = Target))
Train = tf.train.AdamOptimizer(lr).minimize(loss)
# For Contour Prediction
x, y = np.meshgrid(np.linspace(-1.5, 1.5, 100),
np.linspace(-1.5, 1.5, 100))
xy = np.c_[x.ravel(), y.ravel()]
# Train Model
with tf.Session() as Sess:
Sess.run(tf.global_variables_initializer())
# Write Summary in File (TensorBoard)
tf.summary.scalar('Loss Function', loss)
tf.summary.histogram('Prediction', Out)
FWriter = tf.summary.FileWriter('./log', Sess.graph)
AllSummary = tf.summary.merge_all()
for i in range(Epochs):
_, L, All = Sess.run([Train, loss, AllSummary], {Input: moon_X, Target: np.expand_dims(moon_Y, axis = 1)})
FWriter.add_summary(All, i)
if np.mod(i+1, NumShow) == 0: # Plot Each NumShow Step
print('Epoch %d/%d | Loss : %.4f' % (i+1, Epochs, L))
Predict = Sess.run(Out, feed_dict = {Input: xy})
Predict = Predict.reshape(x.shape)
plt.cla()
plt.contourf(x, y, Predict, cmap = 'bwr', alpha = 0.2)
plt.plot(moon_X[moon_Y == 0, 0], moon_X[moon_Y == 0, 1], 'ob')
plt.plot(moon_X[moon_Y == 1, 0], moon_X[moon_Y == 1, 1], 'or')
plt.show()
"""
Type in your CMD : tensorboard --logdir log/
then click on link --> for axam http://AMIN-PC2:6006
"""