Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created August 3, 2017 12:51
Show Gist options
  • Save dz1984/040edeefb4cbbbf413e60b24ab16bcf2 to your computer and use it in GitHub Desktop.
Save dz1984/040edeefb4cbbbf413e60b24ab16bcf2 to your computer and use it in GitHub Desktop.
Try to simulate the BPN in R.
options(digits=6)
active = function(x) 1/ (1+exp(1)^(-x))
mse = function(x) 0.5 * (1.0 - x)^2
p = 0.5
u1 =0
u2 = 1
w31 = 1
w41 = -1.0
w32 = 0.5
w42 = 2
w53 = 1.5
w54 = -1.0
w3b = 1
w4b = 1
w5b = 1
b3 = -1
b4 = 1
b5 = 1
# The Feed-Forward Pass
u3 = active(w31*u1+w32*u2+w3b*b3)
u3
u4 = active(w41*u1+w42*u2+w4b*b4)
u4
u5 = active(w53*u3+w54*u4+w5b*b5)
u5
mse_1 = mse(u5)
mse_1
# The Error Backward-Propagation Pass
delta_o = round((1-u5)*u5*(1-u5), 6)
delta_o
delta_u4 = (delta_o * w54)*u4*(1.0-u4)
delta_u4
delta_u3 = (delta_o*w53)*u3*(1-u3)
delta_u3
w54 = w54 + (p*delta_o*u4)
w54
w53 = w53 + (p*delta_o*u3)
w53
w5b = w5b + (p*delta_o*b5)
w5b
w42 = w42 + (p*delta_u4*u2)
w42
w41 = w41 + (p*delta_u4*u1)
w41
w4b = w4b + (p*delta_u4*b4)
w4b
w32 = w32 + (p*delta_u3*u2)
w32
w31 = w31 + (p*delta_u3*u1)
w31
w3b = w3b + (p*delta_u3*b3)
w3b
# The Feed-Forward Pass again
u3 = f(w31*u1+w32*u2+w3b*b3)
u3
u4 = f(w41*u1+w42*u2+w4b*b4)
u4
u5 = f(w53*u3+w54*u4+w5b*b5)
u5
mse_2 = mse(u5)
mse_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment