In [1]:
%matplotlib inline
In [2]:
import numpy as np
import matplotlib.pyplot as plt
import time
from IPython import display
from mlp import MLP

Auxiliar functions

To visualize and store the results

In [3]:
def update_plot(X,Y,grid,prediction,plt):
    """
    Updates the image of the actual plot with the original target
    and the prediction of the model
    """
    plt.clf()
    plt.scatter(X,Y,color='red')
    plt.plot(grid,prediction)
    display.clear_output(wait=True)
    display.display(plt.gcf())

def export_plot(plt,filename,format='pdf'):
    """
    Exports the figure into the project folder
    suported formats : eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff.
    """
    formated_filename = "{0}_{1}.{2}".format(time.strftime("%Y%m%d_%H%M%S"), filename, format)
    plt.savefig(formated_filename, format=format)

Pattern to approximate

In [4]:
N = 50 # datapoints

X = np.random.uniform(-1,1,N)*np.pi
grid = np.linspace(-1,1,150)*(np.pi + 0.5)

PATTERN = 'sin_cos' # 'linear', 'abs', 'squared', 'sin', 'cos', 'sin_cos'
BIAS = 1
NOISE = 0.5

if PATTERN == 'sin':
    original = np.sin(grid)
    pattern = np.sin(X)
elif PATTERN == 'linear':
    original = grid
    pattern = X
elif PATTERN == 'abs':
    original = np.abs(grid)
    pattern = np.abs(X)
elif PATTERN == 'squared':
    original = grid**2
    pattern = X**2
elif PATTERN == 'sin':
    original = np.sin(grid)
    pattern = np.sin(X)
elif PATTERN == 'cos':
    original = np.cos(grid)
    pattern = np.cos(X)
elif PATTERN == 'sin_cos':
    original = np.sin(np.cos(grid)*np.pi)
    pattern = np.sin(np.cos(X)*np.pi)
else:
    original = grid
    pattern = X

original += BIAS
pattern += BIAS
if NOISE > 0:
    Y = pattern + np.random.normal(size=N, scale=NOISE)
else:
    Y = pattern

plt.plot(grid, original, color='red', label='original')
plt.scatter(X, Y, color='red', label='samples')
plt.ylabel('target')
plt.xlabel('x')
plt.legend()
export_plot(plt,filename="target_pattern")

Especification of the MLP

it shows the initial weights

In [5]:
n_input = 1
n_hidden = 7
n_output = 1
activation = 'tanh' # 'tanh', 'linear'

# Learning rate parameters
lr_policy = 'step' # 'step', 'rand', 'fixed'
lr = 0.1
gamma = 0.1
low = 0.0001
high = 0.1
stepsize = 100*N

EPOCHS = 200

mlp = MLP(n_input, n_hidden, n_output, activation=activation, learning_rate=lr, 
          lr_policy=lr_policy, stepsize=stepsize, gamma=gamma, low=low, high=high)
mlp.print_architecture()
ANN with next architecture
n_input = 1
n_hidden = 7
n_output = 1
lr_policy = step
lr = 0.1stepsize = 5000
gamma = 0.1
Input to hidden weights
[[ 1.74928086 -0.99770235]
 [-0.12808072  1.35196958]
 [ 1.68318194  1.76724545]
 [-0.70239021 -0.63632377]
 [-0.73942293 -0.02139752]
 [-0.86270875 -1.1380335 ]
 [ 1.7979958  -0.05223949]]
Hidden to output weights
[[ 0.27954061 -0.22166289 -0.32558404 -0.25020324 -0.39928252 -0.27151963
   0.05661958  0.16938963]]

Initial prediction

It contains the randomly initialized weights and bias

In [6]:
plt.clf()
prediction = mlp.test(grid)
plt.plot(grid, original, 'r-.', color='red', label='original')
plt.scatter(X, Y, color='red', label='samples')
plt.plot(grid, prediction, label='target')

# plot formatting
plt.legend()
plt.ylabel('y')
plt.xlabel('x')
export_plot(plt,"initial_prediction")

Training

In [7]:
plt.clf()
error = np.zeros(EPOCHS)
for epoch in range(EPOCHS):
    prediction = mlp.test(grid)
    plt.plot(grid,original, 'r-.', color='red', label='original')
    update_plot(X,Y,grid,prediction,plt)
    error[epoch] = mlp.mean_error(X,Y)
    
    mlp.train(X,Y)

plt.plot(grid,original, 'r-.', color='red', label='original')
plt.legend(['target', 'original', 'samples'])
plt.ylabel('target')
plt.xlabel('x')
export_plot(plt,"final_prediction")

Training error

In [8]:
plt.clf()
plt.plot(range(1,EPOCHS), error[1:])
plt.xlabel("Epoch")
plt.ylabel("Mean squared error")
export_plot(plt,"training_error")

Hidden representation

  • Red dots are the samples of the target pattern
  • Blue line is the model prediction in all the interval
  • The rest of the colors corresponds to the hidden activations
In [9]:
plt.clf()
prediction = mlp.test(grid)
plt.plot(grid, original, 'r-.', color='red', label='original')
plt.scatter(X,Y,color='red', label="samples")
plt.plot(grid,prediction, label="prediction", linewidth=2)
z_hidden = mlp.feature_extraction(grid)
for i, z in enumerate(np.transpose(z_hidden)):
    if i == n_hidden:
        plt.plot(grid, z, '-.', label="hbias")
    else:
        plt.plot(grid, z, '--', label="hz{0}".format(i))

plt.xlabel('input')
plt.ylabel('output')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),
          fancybox=True, shadow=True, ncol=5)
export_plot(plt,"hidden_representation")
In [10]:
mlp.print_architecture()
ANN with next architecture
n_input = 1
n_hidden = 7
n_output = 1
lr_policy = step
lr = 0.001stepsize = 5000
gamma = 0.1
Input to hidden weights
[[  2.53360798e+00  -3.73443867e+00]
 [ -1.07290597e+00   2.73851747e+00]
 [  3.51727433e+00   2.12111697e+00]
 [ -5.47304564e-05  -2.62228063e-03]
 [ -2.36843410e-08  -1.13469984e-06]
 [ -2.52879625e+00  -3.44087059e+00]
 [  1.48689433e+00   2.31874065e-02]]
Hidden to output weights
[[ -1.40212966e+00  -9.59110191e-01  -1.24780694e+00  -7.01063089e-04
   -3.03359733e-07  -1.14224878e+00   9.67336690e-01   8.39170271e-01]]

In []: