-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictor.py
More file actions
37 lines (26 loc) · 1.17 KB
/
predictor.py
File metadata and controls
37 lines (26 loc) · 1.17 KB
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
import numpy as np
class NumberClassifier:
def __init__(self, weights_file='weights/weights.txt'):
self.weights = []
dims = None
with open(weights_file, 'r') as file:
for line in file:
if ';' in line:
dims = tuple([int(dim.strip()) for dim in line.split(';')])
else:
values = np.asarray([[float(val.strip()) for val in line.split(',')]])
self.weights.append(values.reshape(dims))
# self.weights = np.asarray(self.weights)
# returns the number corresponding to the largest output activation, but -1 if no output is greater than 0.5
def classify(self, input_vec):
def sigmoid(z):
return 1 / (1 + np.exp(-z))
activations = np.asarray([input_vec]).T
for wt in self.weights:
wt = np.asarray(wt)
# add on the bias unit to the vector
activations = np.concatenate(([[1]], activations))
# calculate the activations of the next layer
activations = sigmoid(np.dot(wt, activations))
outputs = activations.T[0]
return np.argmax(outputs)