-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multilayer-Perceptron-XOR.py
136 lines (94 loc) · 2.86 KB
/
Multilayer-Perceptron-XOR.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/pdthon
# -*- coding: utf-8 -*-
# Imports
from datetime import timedelta
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import time
# Dataset definition - all combinations of XOR
x = [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
]
# Desired output
d = [
[0],
[1],
[1],
[0]
]
# Set Parameters
# Learning rate - [0, 1]
learning_rate = 0.05
# Maximum number allowed of iterations
max_iterations = 100000
# Number of epochs
n_epochs = 10000
# Total amount of training items
n_training = len(x)
# Set Network Parameters
# Input layer
n_input_nodes = 2
# Hidden layer 1
n_hidden_nodes_1 = 1
# Output layer
n_output_nodes = 1
# Reserved spaces for storing input and output data
x_ = tf.placeholder(tf.float32, [None, n_input_nodes])
d_ = tf.placeholder(tf.float32, [n_training, n_output_nodes])
# Configure the parameters for the network
# Hidden layer 1
# Weights
theta1 = tf.Variable(tf.random_uniform([n_input_nodes,n_hidden_nodes_1], -1, 1))
# Bias
bias1 = tf.Variable(tf.zeros([n_hidden_nodes_1]))
# Net1 - Multiply the matrices
net1 = tf.matmul(x_, theta1)
# Activation function
O1 = tf.sigmoid(net1 + bias1)
# Output layer
# Hidden node weights of index 4 - [wi4]
theta2_x = tf.Variable(tf.random_uniform([n_input_nodes,n_output_nodes], -1, 1))
theta2_h = tf.Variable(tf.random_uniform([n_hidden_nodes_1,n_output_nodes], -1, 1))
# Hidden node bias of index 4
bias2 = tf.Variable(tf.zeros([n_output_nodes]))
# Net4 - Multiply the matrices
net4_x = tf.matmul(x_, theta2_x)
net4_h = tf.matmul(O1, theta2_h)
# Hidden node activation function of index 4
y = tf.sigmoid((net4_x + net4_h) + bias2)
# Cost (error) function and optimizer
# Mean Squared Estimate (MSE)
cost = tf.reduce_mean(tf.square(d_ - y))
# Training
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Create session
session = tf.Session()
# Initialize variables
init = tf.global_variables_initializer()
# Run the session
session.run(init)
# Training Step
# Start-time used for printing time-usage below
start_time = time.time()
for i in range(max_iterations):
session.run(train_step, feed_dict={x_: x, d_: d}) # Dictionary object with placeholders as keys and feed tensors as values
if i % n_epochs == 0:
accuracy = session.run(cost, feed_dict={x_: x, d_: d})
print("Optimization Iteration: %s, Training Accuracy: %s" % (i, accuracy))
# Ending time.
end_time = time.time()
# Difference between start and end-times.
time_dif = end_time - start_time
# Print the time-usage.
print("Time usage: " + str(timedelta(seconds=int(round(time_dif)))))
# Test Step
# Returns the absolute value, if the value is less than 0.5
correct_prediction = abs(d_ - y)
# Calculates the mean of the elements
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
final_accuracy = session.run(accuracy, feed_dict={x_: x, d_: d})
print'\nAccuracy on Test Step: %s',final_accuracy