Source

ml-class / ex3.py

Full commit
#!/usr/bin/env python

from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from scipy.io import loadmat
from itertools import izip

data = loadmat('ex3/ex3data1.mat')
X, y = data['X'], data['y']
ds = SupervisedDataSet(X.shape[1], y.shape[1])
for inp, target in izip(X, y):
    ds.addSample(inp, target)

net = buildNetwork(X.shape[1], X.shape[1], y.shape[1])
t = BackpropTrainer(net, learningrate=0.01,momentum=0.5)
t.trainOnDataset(ds, 10)


from random import choice
indexes = range(len(X))
for _ in range(10):
    i = choice(indexes)
    print(net.activate(X[i]), y[i])