test_script_XOR



In [1]:
import ANN
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

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

In [3]:
numLayers = 3
iterations = 5000
eta = 0.3

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

#output, error = nn1.train(iterations)


Class labels:set([0, 1])
Network constructed with 3 layers, learning rate is 0.3
Layers connected

In [4]:
target = nn1.__target__

error = []
output = []

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()



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