-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXOR_model.py
More file actions
92 lines (77 loc) · 3.18 KB
/
Copy pathXOR_model.py
File metadata and controls
92 lines (77 loc) · 3.18 KB
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
# Author: Diana Chajkovska
# Date: 20th May, 2019
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def analyze_classifier(sess, i, w1, b1, w2, XOR_X, XOR_T):
"""Visualize the classification."""
print('\nEpoch %i' % i)
print('Hypothesis %s' % sess.run(output,
feed_dict={n_input: XOR_X,
n_output: XOR_T}))
print('w1=%s' % sess.run(w1))
print('b1=%s' % sess.run(b1))
print('w2=%s' % sess.run(w2))
print('cost (ce)=%s' % sess.run(cross_entropy,
feed_dict={n_input: XOR_X,
n_output: XOR_T}))
# Visualize classification boundary
xs = np.linspace(-1, 2)
ys = np.linspace(-1, 2)
pred_classes = []
for x in xs:
for y in ys:
pred_class = sess.run(output,
feed_dict={n_input: [[x, y]]})
# print('x - ', x, 'y - ', y, 'c - ', pred_class)
pred_classes.append((x, y, pred_class.round()))
xs_p, ys_p = [], []
xs_n, ys_n = [], []
for x, y, c in pred_classes:
if c == 0:
xs_n.append(x)
ys_n.append(y)
else:
xs_p.append(x)
ys_p.append(y)
plt.plot(xs_p, ys_p, 'ro', xs_n, ys_n, 'bo')
plt.show()
input_data = np.array([[0., 0.], [0., 1.], [1., 0.], [1., 1.]], dtype='uint8')
output_data = np.array([[0.], [1.], [1.], [0.]], dtype='uint8')
# Number of input|output neurons
n_input = tf.placeholder(tf.float32, shape=[None, 2], name="n_input")
n_output = tf.placeholder(tf.float32, shape=[None, 1], name="n_output")
hidden_nodes = 2
b_hidden = tf.Variable(tf.zeros([hidden_nodes]), name="hidden_bias")
b2 = tf.Variable(tf.zeros([1]), name="Biases2")
W_hidden = tf.Variable(tf.random_normal([2, hidden_nodes]), name="hidden_weights")
hidden = tf.sigmoid(tf.matmul(n_input, W_hidden) + b_hidden)
# Output layer`s weight matrix
W_output = tf.Variable(tf.random_normal([hidden_nodes, 1]), name="output_weights")
# Calculate output layer`s activation
output = tf.sigmoid(tf.matmul(hidden, W_output) + b2)
# Calculate the mean of cross_entropy
cross_entropy = tf.square(n_output - output)
# Calculate the mean of cross_entropy
loss = tf.reduce_mean(cross_entropy)
# Optimize loss function
optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss)
init = tf.initialize_all_variables()
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Create session
sess = tf.Session()
# Initialize all variables
sess.run(init)
print("Training the model: ")
for epoch in range(0, 1001):
# run the training operation
sess.run(optimizer, feed_dict={n_input: input_data, n_output: output_data})
if epoch % 200 == 0:
analyze_classifier(sess, epoch, W_hidden, b_hidden, W_output, input_data, output_data)
print("\nCheck the model:")
for i in range(len(input_data)):
print("input: {} | output: {}".format(input_data[i], np.ndarray.round(sess.run(output, feed_dict={n_input: [input_data[i]]}))))
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print("\nModel saved in path: %s" % save_path)