Skip to content

Commit 71376c8

Browse files
committed
Update learn-math example to fix learning data and calculate error on test
1 parent b193942 commit 71376c8

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

examples-typescript/learn-math.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const net = new LSTM();
1818
const mathProblems = [
1919
'0+0=0',
2020
'0+1=1',
21-
'1+0=2',
21+
'1+0=1',
2222
'0+2=2',
23-
'2+0=3',
23+
'2+0=2',
2424
'0+3=3',
2525
'3+0=3',
2626
'0+4=4',
@@ -108,22 +108,25 @@ const mathProblems = [
108108
'6+9=15',
109109
'9+6=15',
110110
'7+7=14',
111-
'7+8=16',
112-
'8+7=16',
113-
'7+9=17',
114-
'9+7=17',
115-
'8+8=17',
116-
'8+9=18',
117-
'9+8=18',
111+
'7+8=15',
112+
'8+7=15',
113+
'7+9=16',
114+
'9+7=16',
115+
'8+8=16',
116+
'8+9=17',
117+
'9+8=17',
118118
'9+9=18'
119119
];
120120

121121
net.train(mathProblems, { log: true, errorThresh: 0.03 });
122122

123+
let errors = 0;
123124
for (let i = 0; i < mathProblems.length; i++) {
124125
const input = mathProblems[i].split('=')[0] + '=';
125126
const output = net.run(input);
126127
const predictedMathProblem = input + output;
127-
console.log(input + output);
128-
assert(mathProblems.indexOf(predictedMathProblem) > -1);
129-
}
128+
const error = mathProblems.indexOf(predictedMathProblem) <= -1 ? 1 : 0;
129+
errors += error;
130+
console.log(input + output + (error ? " - error" : ""));
131+
}
132+
console.log("Errors: " + errors / mathProblems.length);

examples/learn-math.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const net = new LSTM();
1818
const mathProblems = [
1919
'0+0=0',
2020
'0+1=1',
21-
'1+0=2',
21+
'1+0=1',
2222
'0+2=2',
23-
'2+0=3',
23+
'2+0=2',
2424
'0+3=3',
2525
'3+0=3',
2626
'0+4=4',
@@ -108,22 +108,25 @@ const mathProblems = [
108108
'6+9=15',
109109
'9+6=15',
110110
'7+7=14',
111-
'7+8=16',
112-
'8+7=16',
113-
'7+9=17',
114-
'9+7=17',
115-
'8+8=17',
116-
'8+9=18',
117-
'9+8=18',
111+
'7+8=15',
112+
'8+7=15',
113+
'7+9=16',
114+
'9+7=16',
115+
'8+8=16',
116+
'8+9=17',
117+
'9+8=17',
118118
'9+9=18'
119119
];
120120

121121
net.train(mathProblems, { log: true, errorThresh: 0.03 });
122122

123+
let errors = 0;
123124
for (let i = 0; i < mathProblems.length; i++) {
124125
const input = mathProblems[i].split('=')[0] + '=';
125126
const output = net.run(input);
126127
const predictedMathProblem = input + output;
127-
console.log(input + output);
128-
assert(mathProblems.indexOf(predictedMathProblem) > -1);
129-
}
128+
const error = mathProblems.indexOf(predictedMathProblem) <= -1 ? 1 : 0;
129+
errors += error;
130+
console.log(input + output + (error ? " - error" : ""));
131+
}
132+
console.log("Errors: " + errors / mathProblems.length);

0 commit comments

Comments
 (0)