Skip to content

Instantly share code, notes, and snippets.

@yusugomori
Created January 25, 2013 17:10
Show Gist options
  • Save yusugomori/4636147 to your computer and use it in GitHub Desktop.
Save yusugomori/4636147 to your computer and use it in GitHub Desktop.
Back-Propagation Neural Networks by CoffeeScript
###!
*
* Back-Propagation Neural Networks
*
* References:
* http://arctrix.com/nas/python/bpnn.py
*
###
class BPNN
constructor: (ni, nh, nout) ->
@ni = ni + 1
@nh = nh
@no = nout
@ai = []
@ah = []
@ao = []
for i in [0...@ni]
@ai.push 1.0
for i in [0...@nh]
@ah.push 1.0
for i in [0...@no]
@ao.push 1.0
@wi = @makeMatrix(@ni, @nh)
@wo = @makeMatrix(@nh, @no)
for i in [0...@ni]
for j in [0...@nh]
@wi[i][j] = @random(-0.2, 0.2)
for j in [0...@nh]
for k in [0...@no]
@wo[j][k] = @random(-2.0, 2.0)
@ci = @makeMatrix(@ni, @nh)
@co = @makeMatrix(@nh, @no)
return
update: (inputs) ->
unless inputs.length is @ni - 1
throw new Error('wrong number of inputs')
for i in [0...@ni-1]
@ai[i] = inputs[i]
for j in [0...@nh]
sum = 0.0
for i in [0...@ni]
sum = sum + @ai[i] * @wi[i][j]
@ah[j] = @sigmoid(sum)
for k in [0...@no]
sum = 0.0
for j in [0...@nh]
sum = sum + @ah[j] * @wo[j][k]
@ao[k] = @sigmoid(sum)
return @ao
backPropagate: (targets, n, m) ->
unless targets.length is @no
throw new Error('wrong number of target values')
output_deltas = []
for i in [0...@no]
output_deltas.push 0.0
for k in [0...@no]
error = targets[k] - @ao[k]
output_deltas[k] = @dsigmoid(@ao[k]) * error
hidden_deltas = []
for i in [0...@nh]
hidden_deltas.push 0.0
for j in [0...@nh]
error = 0.0
for k in [0...@no]
error = error + output_deltas[k] * @wo[j][k]
hidden_deltas[j] = @dsigmoid(@ah[j]) * error
for j in [0...@nh]
for k in [0...@no]
change = output_deltas[k] * @ah[j]
@wo[j][k] = @wo[j][k] + n * change + m * @co[j][k]
@co[j][k] = change
for i in [0...@ni]
for j in [0...@nh]
change = hidden_deltas[j] * @ai[i]
@wi[i][j] = @wi[i][j] + n * change + m * @ci[i][j]
@ci[i][j] = change
error = 0.0
for k in [0...targets.length]
error = error + 0.5 * Math.pow((targets[k] - @ao[k]), 2)
return error
test: (patterns) ->
for p in patterns
console.log "#{p[0].join(', ')} -> #{@update(p[0])}"
return
train: (patterns, iter=100, n=0.5, m=0.1) ->
for i in [0...iter]
error = 0.0
for p in patterns
inputs = p[0]
targets = p[1]
@update(inputs)
error += @backPropagate(targets, n, m)
return
random: (a, b) ->
r = (b - a) * Math.random() + a
return r
makeMatrix: (i, j, fill=0.0) ->
m = []
for k in [0...i]
n = []
for h in [0...j]
n.push fill
m.push n
return m
sigmoid: (x) ->
p = Math.exp(x)
n = Math.exp(-x)
tanh = (p - n) / (p + n)
return tanh
dsigmoid: (y) ->
d = 1.0 - Math.pow(y, 2)
return d
demo = () ->
patterns = [
[[-1,-1], [-1]],
[[-1,1], [1]],
[[1,-1], [1]],
[[1,1], [-1]]
]
bp = new BPNN(2, 3, 1)
bp.train(patterns, 100)
bp.test(patterns)
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment