XOR


In [1]:
# Import necessary libraries
import ANN   # Custom library. Install from libraries folder
import numpy as np
%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.figsize'] = (18,7)
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from IPython import display

Initialize the multilayer perceptron


In [2]:
Input = [[0,0],[0,1],[1,0],[1,1]]
target = [0,1,1,0]

numLayers = 3
iterations = 5000
eta = 0.3

nn1 = ANN.FNN(numLayers, Input, target, eta=eta)


Class labels:{0, 1}
Network constructed with 3 layers, learning rate is 0.3
Layers connected

In [3]:
target = nn1.__target__
error = []
output = []

Plot expected classification


In [4]:
plt.ion()

f, ax = plt.subplots(1,2)

im = ax[0].imshow(target, interpolation = 'none', cmap='viridis', origin='lower', aspect='auto', vmin= 0., vmax = 1.)
ax[0].set_xticks([0,1,2,3])
ax[0].set_xticklabels([str(inp) for inp in Input])
ax[0].set_yticks([])
ax[0].set_xlabel("Input")
ax[0].set_title('XOR Classification')

f.colorbar(im, ax=ax[0])

cost = ax[1].plot(error, c='k')
ax[1].set_title("Change in cost Function")
ax[1].set_xlabel("Iterations")
ax[1].set_ylabel("Cost Function")

f.canvas.draw()


Learn and classify XOR data


In [5]:
plt.ion()

f, ax = plt.subplots(1,2)

im = ax[0].imshow(target, interpolation = 'none', cmap='viridis', origin='lower', aspect='auto', vmin= 0., vmax = 1.)
ax[0].set_xticks([0,1,2,3])
ax[0].set_xticklabels([str(inp) for inp in Input])
ax[0].set_yticks([])
ax[0].set_xlabel("Input")
ax[0].set_title('XOR Classification')

f.colorbar(im, ax=ax[0])

cost = ax[1].plot(error, c='k')
ax[1].set_title("Change in cost Function")
ax[1].set_xlabel("Iterations")
ax[1].set_ylabel("Cost Function")

f.canvas.draw()


IRIS Dataset

Load dataset


In [6]:
data = load_iris()
Input = data.data
target = data.target

Connect up the multilayer perceptron


In [7]:
numLayers = 3
iterations = 2000
eta = 0.005

nn1 = ANN.FNN(numLayers, Input, target, eta=eta)
#output, error = nn1.train(iterations)


Class labels:{0, 1, 2}
Network constructed with 3 layers, learning rate is 0.005
Layers connected

In [8]:
target = nn1.__target__

error = []
output = []

Plot expected classification


In [9]:
plt.ion()

f, ax = plt.subplots(1,2)

im = ax[0].imshow(target, interpolation = 'none', cmap='viridis', origin='lower', aspect='auto', vmin= 0., vmax = 1.)
ax[0].set_yticks([0,1,2])
ax[0].set_yticklabels(['0', '1', '2'])
ax[0].set_ylabel("Classes")
ax[0].set_xlabel("Data Points")
ax[0].set_title('Target classes')

f.colorbar(im, ax=ax[0])
cost = ax[1].plot(error, c='k')
ax[1].set_title("Change in cost Function")
ax[1].set_xlabel("Iterations")
ax[1].set_ylabel("Cost Function")
f.canvas.draw()


Learn, classify and visualize classifications


In [10]:
for i in range(iterations):
    out, e = nn1.train()
    output.append(out)
    error.append(e)
    if i % 5 == 0: # Every 5th iteration
        try:
            im.set_data(out) 
            ax[1].plot(error, c='k')
            f.canvas.draw()
            display.display(f)
            ax[1].cla()
            display.clear_output(wait=True)
            #plt.pause(0.001)
    
        except KeyboardInterrupt:
            break
plt.ioff()
plt.close()