Create a gist now

Instantly share code, notes, and snippets.

# coding:utf-8
import numpy as np
import math
import random
import matplotlib.pyplot as plt
l = [1,8,8,8,1] #regression
weights = []
def sinpi(x):
return math.sin(2*math.pi * x)
def makeData(N):
l = []
for i in range(N):
x = random.random()
error = np.random.normal(0,0.1)
l.append((x,sinpi(x)+error))
return l
def activate(x):
return 1/(1+math.exp(-x))
def difActivate(x):
return activate(x)*(1-activate(x))
vectorizedActivate = np.vectorize(activate)
vectorizedDifActivate = np.vectorize(difActivate)
def calculate(x):
nexti = x
for i, x in enumerate(l):
if i+1 == len(l):
break
med = weights[i].dot(nexti)
ipt = med
if i+2 == len(l):
nexti = med
else:
nexti = vectorizedActivate(med)
return nexti[0,0]
def main():
global weights
N = 100
data = makeData(N)
eta = 0.5
for i, x in enumerate(l):
if i+1 == len(l):
break
weightMatrix = []
for j in range(x):
w = [random.random() for k in range(l[i+1])]
weightMatrix.append(w)
weights.append(np.matrix(weightMatrix).T)
inputs = [x[0] for x in data]
outputs = [x[1] for x in data]
xvec = np.zeros((N,1))
for idx in range(300):
for proc, ipt in enumerate(inputs):
# ipt: avec
# nexti: activateFunc(ipt)
ipts = []
ipt = np.matrix(ipt)
nexti = ipt
for i, x in enumerate(l):
if i+1 == len(l):
break
ipts.append(ipt)
ipt = weights[i].dot(nexti)
if i+2 == len(l):
nexti = ipt
else:
nexti = vectorizedActivate(ipt)
# Backpropagation
deltak = [nexti[0,0]-outputs[proc]]
delta = [deltak]
for i in range(len(l) - 1, 1, -1):
c = vectorizedDifActivate(ipts[i-1])
deltal = []
weight = weights[i-1].T
for k in range(l[i-1]):
d = 0
for k2 in range(l[i]):
d += weight[k, k2] * delta[-1][k2]
deltal.append(d*c[k,0])
delta.append(deltal)
deltaE = []
for i, x in enumerate(l):
if i==0:
va = ipts[0]
elif i+1 == len(l):
break
else:
va = vectorizedActivate(ipts[i])
deltaE1 = []
for k in range(l[i+1]):
m = []
for k2 in range(l[i]):
m.append(delta[-i-1][k]*va[k2, 0])
deltaE1.append(m)
deltaE.append(deltaE1)
for i in range(len(l)-1):
deltaEi = np.matrix(deltaE[i])
weights[i] = weights[i] - deltaEi * eta
x = np.arange(0,1,0.01)
y = [calculate(k) for k in x]
plt.plot(x, y)
plt.scatter(inputs, outputs)
plt.show()
print calculate(0.0)
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment