by Fabio A. González, Universidad Nacional de Colombia
version 1.0, June 2018
This notebook is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
To run this notebook you need to download Pybrain (https://github.com/pybrain/pybrain) and copy the pybrain
folder to the same folder where this notebook is.
import pybrain
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
$X$ | $Y$ | $X$ and $Y$ |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
from pybrain.tools.shortcuts import buildNetwork
net = buildNetwork(2, 1, outclass=pybrain.SigmoidLayer)
print(net.params)
[ 0.453198 -1.24483399 -0.91415624]
def print_pred2(dataset, network):
df = pd.DataFrame(dataset.data['sample'][:dataset.getLength()],columns=['X', 'Y'])
prediction = np.round(network.activateOnDataset(dataset),3)
df['output'] = pd.DataFrame(prediction)
return df
from pybrain.datasets import UnsupervisedDataSet, SupervisedDataSet
D = UnsupervisedDataSet(2) # define a dataset in pybrain
D.addSample([0,0])
D.addSample([0,1])
D.addSample([1,0])
D.addSample([1,1])
print_pred2(D, net)
X | Y | output | |
---|---|---|---|
0 | 0.0 | 0.0 | 0.611 |
1 | 0.0 | 1.0 | 0.387 |
2 | 1.0 | 0.0 | 0.312 |
3 | 1.0 | 1.0 | 0.154 |
net.params[:] = [ -150, -100, 100]
print_pred2(D, net)
X | Y | output | |
---|---|---|---|
0 | 0.0 | 0.0 | 0.0 |
1 | 0.0 | 1.0 | 0.0 |
2 | 1.0 | 0.0 | 0.0 |
3 | 1.0 | 1.0 | 0.0 |
$X$ | $Y$ | $X$ xor $Y$ |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
def plot_nn_prediction(N):
# a function to plot the binary output of a network on the [0,1]x[0,1] space
x_list = np.arange(0.0,1.0,0.025)
y_list = np.arange(1.0,0.0,-0.025)
z = [0.0 if N.activate([x,y])[0] <0.5 else 1.0 for y in y_list for x in x_list]
z = np.array(z)
grid = z.reshape((len(x_list), len(y_list)))
plt.imshow(grid, extent=(x_list.min(), x_list.max(), y_list.min(), y_list.max()),cmap=plt.get_cmap('Greys_r'))
plt.show()
net.params[:] = [-20, -50, 50]
plot_nn_prediction(net)
Dtrain = SupervisedDataSet(2,1) # define a dataset in pybrain
Dtrain.addSample([0,0],[0])
Dtrain.addSample([0,1],[1])
Dtrain.addSample([1,0],[1])
Dtrain.addSample([1,1],[0])
from pybrain.supervised.trainers import BackpropTrainer
net = buildNetwork(2, 2, 1, hiddenclass=pybrain.SigmoidLayer, outclass=pybrain.SigmoidLayer)
T = BackpropTrainer(net, learningrate=0.1, momentum=0.9)
T.trainOnDataset(Dtrain, 1000)
print_pred2(D, net)
X | Y | output | |
---|---|---|---|
0 | 0.0 | 0.0 | 0.044 |
1 | 0.0 | 1.0 | 0.949 |
2 | 1.0 | 0.0 | 0.949 |
3 | 1.0 | 1.0 | 0.049 |
plot_nn_prediction(net)
from pybrain.tools.validation import Validator
validator = Validator()
Dlrrh = SupervisedDataSet(4,4)
Dlrrh.addSample([1,1,0,0],[1,0,0,0])
Dlrrh.addSample([0,1,1,0],[0,0,1,1])
Dlrrh.addSample([0,0,0,1],[0,1,1,0])
df = pd.DataFrame(Dlrrh['input'],columns=['Big Ears', 'Big Teeth', 'Handsome', 'Wrinkled'])
print (df.join(pd.DataFrame(Dlrrh['target'],columns=['Scream', 'Hug', 'Food', 'Kiss'])))
net = buildNetwork(4, 3, 4, hiddenclass=pybrain.SigmoidLayer, outclass=pybrain.SigmoidLayer)
Big Ears Big Teeth Handsome Wrinkled Scream Hug Food Kiss 0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 1 0.0 1.0 1.0 0.0 0.0 0.0 1.0 1.0 2 0.0 0.0 0.0 1.0 0.0 1.0 1.0 0.0
T = BackpropTrainer(net, learningrate=0.01, momentum=0.99)
scores = []
for i in range(1000):
T.trainOnDataset(Dlrrh, 1)
prediction = net.activateOnDataset(Dlrrh)
scores.append(validator.MSE(prediction, Dlrrh.getField('target')))
plt.ylabel('Mean Square Error')
plt.xlabel('Iteration')
plt.plot(scores)
[<matplotlib.lines.Line2D at 0x10dc8cc50>]
General optimization problem:
$$\min_{f\in H}L(f,D)$$with $H$: hypothesis space, $D$:training data, $L$:loss/error
$f(x)=w^{T}x$
$f(x)=P(C_{+}|x)=\sigma(w^{T}x)$
def lrrh_input(vals):
return pd.DataFrame(vals,index=['big ears', 'big teeth', 'handsome', 'wrinkled'], columns=['input'])
def lrrh_output(vals):
return pd.DataFrame(vals,index=['scream', 'hug', 'offer food', 'kiss cheek'], columns=['output'])
in_vals = [0, 0, 0, 0]
lrrh_input(in_vals)
input | |
---|---|
big ears | 0 |
big teeth | 0 |
handsome | 0 |
wrinkled | 0 |
lrrh_output(net.activate(in_vals))
output | |
---|---|
scream | 0.055038 |
hug | 0.786478 |
offer food | 0.947874 |
kiss cheek | 0.090418 |