Created
March 10, 2014 08:02
-
-
Save Slater-Victoroff/9461115 to your computer and use it in GitHub Desktop.
Simple Neural Net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def normalize(vector, span=(0,1)): | |
minimum, maximum = (np.min(vector), np.max(vector)) | |
scaling = (span[1] - span[0]) / (maximum - minimum) | |
return ((vector - minimum) * scaling) + span[0] | |
class NeuralNet(object): | |
def __init__(self, layer_sizes=(10,50,20), threshold=0.5): | |
self.threshold = threshold | |
self.weight_matrices = [] | |
for i, element in enumerate(layer_sizes[:-1]): | |
# Just initializing random weight matrices | |
self.weight_matrices.append(np.random.rand(layer_sizes[i], layer_sizes[i+1])) | |
def transfer(self, input_vector, weight_matrix): | |
return normalize(np.dot(input_vector, weight_matrix)) > self.threshold | |
def predict(self, input_vector): | |
return reduce(self.transfer, [input_vector] + self.weight_matrices) | |
test = NeuralNet() | |
print test.predict(np.random.rand(1, 10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment