Skip to content

Instantly share code, notes, and snippets.

@yi-jiayu
Created October 7, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yi-jiayu/705e5514f8b3b6e779c8a47ef72b6fc4 to your computer and use it in GitHub Desktop.
Save yi-jiayu/705e5514f8b3b6e779c8a47ef72b6fc4 to your computer and use it in GitHub Desktop.
import random
import itertools
def E(x, y):
return (x - y) ** 2
def forward_pass(w, x):
x[2] = w[0] * x[0] + w[2] * x[1]
x[3] = w[1] * x[0] + w[3] * x[1]
x[4] = w[4] * x[2] + w[5] * x[3]
return x
def backward_pass(w, x, y, l=0.05):
dEdx_4 = -2 * (y - x[4])
dx_4dw_4 = x[2]
dx_4dw_5 = x[3]
dx_4dx_2 = w[4]
dx_4dx_3 = w[5]
dx_2dw_0 = x[0]
dx_2dw_2 = x[1]
dx_3dw_1 = x[0]
dx_3dw_3 = x[1]
dEdw = [0] * 6
dEdw[5] = dEdx_4 * dx_4dw_5
dEdw[4] = dEdx_4 * dx_4dw_4
dEdw[3] = dEdx_4 * dx_4dx_3 * dx_3dw_3
dEdw[2] = dEdx_4 * dx_4dx_2 * dx_2dw_2
dEdw[1] = dEdx_4 * dx_4dx_3 * dx_3dw_1
dEdw[0] = dEdx_4 * dx_4dx_2 * dx_2dw_0
dw = [-l * w_i for w_i in dEdw]
w = [w_i + dw_i for w_i, dw_i in zip(w, dw)]
return w
xs = [(1, 2), (1.5, 3), (2, 3), (2, 1), (3, 2), (3, 1)]
ys = [1, 1, 1, -1, -1, -1]
w = [round(random.random(), 2) for _ in range(6)]
print('initial weights:', w)
for x_i, y_i in zip(xs, ys):
x = [x_i[0], x_i[1], 0, 0, 0]
predicted = forward_pass(w, x)[4]
print('predicted:', predicted, 'actual:', y_i)
print('training...')
epoch = 0
for x_i, y_i in itertools.cycle(zip(xs, ys)):
x = [x_i[0], x_i[1], 0, 0, 0]
y = y_i
# print('input vector:', x)
x = forward_pass(w, x)
# print('x after forward pass:', x)
error = E(x[4], y)
# print('error', error)
w = backward_pass(w, x, y)
# print('w after backwards pass', w)
epoch += 1
if epoch > 100:
break
for x_i, y_i in zip(xs, ys):
x = [x_i[0], x_i[1], 0, 0, 0]
predicted = forward_pass(w, x)[4]
print('predicted:', predicted, 'actual:', y_i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment