In [1]:
import pybrain
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
pd.set_option('notebook_repr_html',True)
from notebook.services.config import ConfigManager
cm = ConfigManager()
cm.update('livereveal', {
'theme': 'league',
'transition': 'fade',
'center': 'false',
'overview' : 'true',
'start_slideshow_at': 'selected'
})
%matplotlib inline
Fabio A. González, Universidad Nacional de Colombia
$X$ | $Y$ | $X$ and $Y$ |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
In [2]:
from pybrain.tools.shortcuts import buildNetwork
net = buildNetwork(2, 1, outclass=pybrain.SigmoidLayer)
print net.params
In [3]:
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)
Out[3]:
In [7]:
net.params[:] = [0, 0, 0]
print_pred2(D, net)
Out[7]:
In [8]:
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()
In [9]:
net.params[:] = [-30, 20, 20]
plot_nn_prediction(net)
In [12]:
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)
Out[12]:
In [13]:
plot_nn_prediction(net)
In [14]:
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)
In [15]:
T = BackpropTrainer(net, learningrate=0.01, momentum=0.99)
scores = []
for i in xrange(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)
Out[15]:
In [16]:
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 [17]:
in_vals = [1, 1, 0, 0]
lrrh_input(in_vals)
Out[17]:
In [18]:
lrrh_output(net.activate(in_vals))
Out[18]: