Implementing Multi-layer Perceptron Training for XOR

Inputs and Outputs

In [2]:
examples = np.array([[0,0,0], [0,1,1], [1,0,1], [1,1,0]])
inputs = np.hstack([ np.ones((4,1)), examples[:, [0,1]] ])
outputs = examples[:, [2]]
list(zip(inputs.tolist(), outputs.tolist()))
Out[2]:
[([1.0, 0.0, 0.0], [0]),
 ([1.0, 0.0, 1.0], [1]),
 ([1.0, 1.0, 0.0], [1]),
 ([1.0, 1.0, 1.0], [0])]

Activation function

In [3]:
sigmoid = lambda v: 1/(1 + np.exp(-v))

Batch Gradient Descent

In [5]:
def training_plot():
    a = plt.gca()
    p1 = a.plot(errors, label='squared error'); 
    plt.ylabel("error"); plt.xlabel("iterations"); 
#     plt.ylim((0, 1))
    plt.grid()
    a2 = plt.gca().twinx(); a2.set_ylabel("misclassified examples"); 
    a2.set_ylim(0, 4); a2.set_yticks(np.arange(5)); 
    p2 = a2.plot(misclass, c='r', ls='--', label='misclassified examples');
    lns = p1+p2
    a.legend(lns, [l.get_label() for l in lns], loc='best')
training_plot()