-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathneural_net_learner.py
73 lines (56 loc) · 2.21 KB
/
neural_net_learner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from pybrain.datasets import ClassificationDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules import SoftmaxLayer, SigmoidLayer
"""
A simple neural net implementation using PyBrain.
See http://www.pybrain.org/docs/index.html?#installation for
details on installing PyBrain.
"""
def read_data(filename):
"""
See http://www.pybrain.org/docs/api/datasets/classificationdataset.html
Reads a (naive) csv file of data and converts it into
a ClassificationDataSet. 'Naive' in this case means
the data can be parsed by splitting on commas - i.e.,
no quotations or escapes. I picked this file format
because it should be trivial to convert all our data into it.
Raises an exception when an IO error occurs.
Parameters:
filename - The name of the file containing the data.
"""
data_file = open(filename, "r")
data_lines = [line.split(',') for line in data_file.readlines()]
data_file.close()
features = [[float(f) for f in line[0:-1]] for line in data_lines]
classes = [[int(line[-1])] for line in data_lines]
# Workaround to make classifications zero-based
class_min = min([c[0] for c in classes])
for i in range(len(classes)):
classes[i][0] -= class_min
data_set = ClassificationDataSet(len(features[0]))
for feature_vector, classification in zip(features, classes):
data_set.addSample(feature_vector, classification)
return data_set
def train(data):
"""
See http://www.pybrain.org/docs/tutorial/fnn.html
Returns a neural network trained on the test data.
Parameters:
data - A ClassificationDataSet for training.
Should not include the test data.
"""
network = buildNetwork(
# This is where we specify the architecture of
# the network. We can play around with different
# parameters here.
# http://www.pybrain.org/docs/api/tools.html
data.indim, 5, data.outdim,
hiddenclass=SigmoidLayer,
outclass=SoftmaxLayer
)
# We can fiddle around with this guy's options as well.
# http://www.pybrain.org/docs/api/supervised/trainers.html
trainer = BackpropTrainer(network, dataset=data)
trainer.trainUntilConvergence(maxEpochs=20)
return network