-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuralnet.py
60 lines (48 loc) · 1.8 KB
/
neuralnet.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
# perceptron.py
# -----------------
# Main file for the Neural Net algorithm methods.
# Currently just a shell for further development.
# Thanks to the Berkeley AI projects for general code structure.
#
# Chet Aldrich
from loadFeatures import *
import numpy as np
import random
class NeuralNet:
"""
A Neural Net classifier with one hidden layer.
"""
def __init__(self, legalLabels):
self.legalLabels = legalLabels
self.layer_sizes, self.biases, self.weights = self.initWeights()
self.vector_sigmoid = np.vectorize(sigmoid)
self.vector_dsigmoid = np.vectorize(dsigmoid)
def initWeights(self):
layer_sizes = [10, 15, 10]
back_layers = layer_sizes[1:]
front_layers = layer_sizes[:-1]
biases = []
for layer_size in back_layers:
# Generates layer_size number of random values
# from Gaussian distribution with mean 0 and
# standard deviation of 1
biases.append(np.random.randn(layer_size, 1))
weights = []
# Builds the weights that create connections between
# the neurons in the network.
for layer_size_input, layer_size_output in zip(front_layers, back_layers):
weights.append(np.random.randn(layer_size_output, layer_size_input))
return layer_sizes, biases, weights
def classify(self, data):
guesses = []
progressBar = ProgressBar(100, len(data), "Classifying Data")
for index, entry in enumerate(data):
progressBar.update(index)
# temporary to get the network to return
guesses.append(1)
progressBar.clear()
return guesses
def sigmoid(self, x):
return 1.0 / (1.0 + np.exp(-x))
def dsigmoid(self, x):
return sigmoid(x) * (1 - sigmoid(x))