Skip to content

Instantly share code, notes, and snippets.

@vesche
Last active October 5, 2018 05:29
Show Gist options
  • Save vesche/72dafed33d614710f03f1b75cff1c807 to your computer and use it in GitHub Desktop.
Save vesche/72dafed33d614710f03f1b75cff1c807 to your computer and use it in GitHub Desktop.
import numpy as n
def s(x):return 1.0/(1+n.exp(-x))
def d(x):return x*(1.0-x)
class N:
def __init__(self,x,y):self.x,self.y=x,y;self.w1,self.w2,self.o=n.random.rand(self.x.shape[1],4),n.random.rand(4,1),n.zeros(self.y.shape)
def f(self):self.l1=s(n.dot(self.x,self.w1));self.o=s(n.dot(self.l1,self.w2))
def b(self):r=n.dot(self.l1.T,(2*(self.y-self.o)*d(self.o)));q=n.dot(self.x.T,(n.dot(2*(self.y-self.o)*d(self.o),self.w2.T)*d(self.l1)));self.w1+=q;self.w2+=r
X=n.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]]);y=n.array([[0],[1],[1],[0]]);a=N(X,y)
for i in range(1500):a.f();a.b()
print(a.o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment