-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtanh_test.coffee
32 lines (24 loc) · 970 Bytes
/
tanh_test.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Test by creating a function that approximates tanh
fs = require 'fs'
_ = require 'lodash'
NeuralNet = require './neural_net.coffee'
## Test
teacher = (data) ->
return (Math.tanh(_.sum(data)) + 1) / 2
nn = new NeuralNet.NeuralNet([5, 10, 1])
data = ((Math.random() * 2 - 1 for [0...5]) for [0...500])
diffs = []
for [0 .. 100]
for row in data
prediction = nn.predict(row)
diffs.push Math.abs (prediction - teacher(row))
nn.backpropagate(prediction, [teacher(row)])
# use the model to graph tanh
test_data = ((Math.random() * 2 - 1 for [0...5]) for [0...100])
myTanh = ([_.sum(row), nn.predict(row)] for row in test_data)
reportSuccess = (str="") -> (err) ->
console.log "Error writing file", err if err
console.log "Success writing to file #{str}"
fs.writeFile("./diffs.txt", diffs.join("\n"), reportSuccess "diffs")
fs.writeFile("./model.txt", nn.toString(), reportSuccess "model")
fs.writeFile("./test.txt", myTanh.join('\n'), reportSuccess "tanh")