mirror of
https://github.com/skishore/makemeahanzi.git
synced 2025-11-02 04:37:30 +08:00
Add complete failure of a trained classifier
This commit is contained in:
@ -494,7 +494,7 @@ function extract_strokes(paths, endpoints, bridges, log) {
|
||||
|
||||
// Exports go below this fold.
|
||||
|
||||
this.get_glyph_render_data = function(glyph, manual_bridges) {
|
||||
this.get_glyph_render_data = function(glyph, manual_bridges, classifier) {
|
||||
var paths = orient_paths(split_path(glyph.path));
|
||||
var endpoints = [];
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
@ -503,7 +503,7 @@ this.get_glyph_render_data = function(glyph, manual_bridges) {
|
||||
}
|
||||
}
|
||||
var log = [];
|
||||
var bridges = get_bridges(endpoints, hand_tuned_classifier);
|
||||
var bridges = get_bridges(endpoints, classifier || hand_tuned_classifier);
|
||||
var strokes = extract_strokes(
|
||||
paths, endpoints, manual_bridges || bridges, log);
|
||||
var expected = UNIHAN_STROKE_COUNTS[glyph.name];
|
||||
@ -524,6 +524,20 @@ this.get_glyph_render_data = function(glyph, manual_bridges) {
|
||||
};
|
||||
}
|
||||
|
||||
this.check_classifier_on_glyph = function(glyph, classifier) {
|
||||
assert(glyph.manual.verified);
|
||||
var render = get_glyph_render_data(glyph, undefined, classifier);
|
||||
function canonicalize(bridges) {
|
||||
var result = [];
|
||||
for (var i = 0; i < bridges.length; i++) {
|
||||
result.push(Point.key(bridges[i].map(Point.key).sort()));
|
||||
}
|
||||
return result.sort();
|
||||
}
|
||||
return _.isEqual(canonicalize(render.bridges),
|
||||
canonicalize(glyph.manual.bridges));
|
||||
}
|
||||
|
||||
this.get_glyph_training_data = function(glyph) {
|
||||
assert(glyph.manual.verified);
|
||||
var paths = orient_paths(split_path(glyph.path));
|
||||
@ -570,3 +584,5 @@ this.get_glyph_training_data = function(glyph) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
this.hand_tuned_classifier = hand_tuned_classifier;
|
||||
|
||||
52
server/training.js
Normal file
52
server/training.js
Normal file
@ -0,0 +1,52 @@
|
||||
function evaluate(glyphs, classifier) {
|
||||
var num_correct = 0;
|
||||
for (var i = 0; i < glyphs.length; i++) {
|
||||
if (check_classifier_on_glyph(glyphs[i], classifier)) {
|
||||
num_correct += 1;
|
||||
}
|
||||
}
|
||||
return num_correct/glyphs.length;
|
||||
}
|
||||
|
||||
Meteor.startup(function() {
|
||||
var glyphs = Glyphs.find({'manual.verified': true}).fetch();
|
||||
var glyphs = glyphs.slice(0, 100);
|
||||
console.log('Hand-tuned accuracy:', evaluate(glyphs, hand_tuned_classifier));
|
||||
|
||||
var training_data = [];
|
||||
var training_labels = [];
|
||||
for (var i = 0; i < glyphs.length; i++) {
|
||||
var glyph_data = get_glyph_training_data(glyphs[i]);
|
||||
for (var j = 0; j < glyph_data.length; j++) {
|
||||
training_data.push(glyph_data[j]);
|
||||
}
|
||||
}
|
||||
console.log('Got ' + training_data.length + ' rows of training data.');
|
||||
|
||||
var input = new convnetjs.Vol(1, 1, 8);
|
||||
var net = new convnetjs.Net();
|
||||
net.makeLayers([
|
||||
{type: 'input', out_sx: 1, out_sy: 1, out_depth: 8},
|
||||
{type: 'fc', num_neurons: 8, activation: 'tanh'},
|
||||
{type: 'fc', num_neurons: 8, activation: 'tanh'},
|
||||
{type: 'regression', num_neurons: 1},
|
||||
]);
|
||||
var trainer = new convnetjs.Trainer(
|
||||
net, {method: 'adadelta', l2_decay: 0.001, batch_size: 10});
|
||||
for (var iteration = 0; iteration < 1; iteration++) {
|
||||
for (var i = 0; i < training_data.length; i++) {
|
||||
assert(input.w.length === training_data[i][0].length);
|
||||
input.w = training_data[i][0];
|
||||
trainer.train(input, training_data[i][1]);
|
||||
}
|
||||
console.log('Completed iteration:', iteration);
|
||||
}
|
||||
console.log('Trained neural network.');
|
||||
|
||||
function net_classifier(features) {
|
||||
assert(input.w.length === features.length);
|
||||
input.w = features;
|
||||
return net.forward(input).w[0];
|
||||
}
|
||||
console.log('Neural-net accuracy:', evaluate(glyphs, net_classifier));
|
||||
});
|
||||
Reference in New Issue
Block a user