Created
April 30, 2019 08:02
-
-
Save RodionGork/51d7dc76904286067427cadd39709607 to your computer and use it in GitHub Desktop.
Neural network in Py
This file contains 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
from random import random | |
import math | |
def dot(a, b): | |
return sum([k * v for k, v in zip(a, b)]) | |
def sig(x): | |
return 1 / (1 + math.exp(-x)) | |
class NNTest: | |
def __init__(self, inputs, outputs, layers): | |
self.w = [] | |
c = inputs | |
for n in layers + [outputs]: | |
wi = [[random() * 2 - 1 for _ in range(c)] for _ in range(n)] | |
self.w.append(wi) | |
def calc(self, x): | |
v = x | |
for wi in self.w: | |
v = [sig(dot(v, wij)) for wij in wi] | |
return v | |
if __name__ == '__main__': | |
nn = NNTest(2, 1, [4, 3]) | |
print(nn.calc([-0.5, 0.5])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment