mirror of
https://github.com/skishore/makemeahanzi.git
synced 2025-11-01 20:27:44 +08:00
Drop neural net and trained classifier
This commit is contained in:
@ -1,21 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
this.combined_classifier = undefined;
|
||||
|
||||
Meteor.startup(function() {
|
||||
var weight = 0.8;
|
||||
var dimensions = 8;
|
||||
var input = new convnetjs.Vol(1, 1, dimensions);
|
||||
var net = new convnetjs.Net();
|
||||
net.fromJSON(TRAINED_NEURAL_NET);
|
||||
|
||||
function net_classifier(features) {
|
||||
input.w = features;
|
||||
var softmax = net.forward(input).w;
|
||||
return softmax[1] - softmax[0];
|
||||
}
|
||||
|
||||
combined_classifier = function(features) {
|
||||
return hand_tuned_classifier(features) + weight*net_classifier(features);
|
||||
}
|
||||
});
|
||||
22
lib/external/convnet/1.1.0/LICENSE
vendored
22
lib/external/convnet/1.1.0/LICENSE
vendored
@ -1,22 +0,0 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2014 Andrej Karpathy
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
2115
lib/external/convnet/1.1.0/convnet.js
vendored
2115
lib/external/convnet/1.1.0/convnet.js
vendored
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -540,7 +540,7 @@ this.get_glyph_render_data = function(glyph, manual_bridges, classifier) {
|
||||
}
|
||||
}
|
||||
var log = [];
|
||||
var bridges = get_bridges(endpoints, classifier || combined_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];
|
||||
@ -626,5 +626,3 @@ this.get_glyph_training_data = function(glyph) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
this.hand_tuned_classifier = hand_tuned_classifier;
|
||||
|
||||
@ -1,78 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function train_neural_net() {
|
||||
var glyphs = Glyphs.find({'manual.verified': true}).fetch();
|
||||
var sample = _.sample(glyphs, 400);
|
||||
console.log('Hand-tuned accuracy:', evaluate(sample, hand_tuned_classifier));
|
||||
|
||||
var training_data = [];
|
||||
for (var i = 0; i < glyphs.length; i++) {
|
||||
var glyph_data = get_glyph_training_data(glyphs[i]);
|
||||
var positive_data = glyph_data.filter(function(x) { return x[1] > 0; });
|
||||
var negative_data = glyph_data.filter(function(x) { return x[1] === 0; });
|
||||
if (positive_data.length > negative_data.length) {
|
||||
positive_data = _.sample(positive_data, negative_data.length);
|
||||
} else {
|
||||
negative_data = _.sample(negative_data, positive_data.length);
|
||||
}
|
||||
glyph_data = negative_data.concat(positive_data);
|
||||
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 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: 'softmax', num_classes: 2},
|
||||
]);
|
||||
var trainer = new convnetjs.Trainer(
|
||||
net, {method: 'adadelta', l2_decay: 0.001, batch_size: 10});
|
||||
var input = new convnetjs.Vol(1, 1, 8);
|
||||
for (var iteration = 0; iteration < 10; iteration++) {
|
||||
var loss = 0;
|
||||
var round_data = _.sample(training_data, 4000);
|
||||
for (var i = 0; i < round_data.length; i++) {
|
||||
assert(input.w.length === round_data[i][0].length);
|
||||
input.w = round_data[i][0];
|
||||
var stats = trainer.train(input, round_data[i][1]);
|
||||
assert(!isNaN(stats.loss))
|
||||
loss += stats.loss;
|
||||
}
|
||||
console.log('Iteration', iteration, 'mean loss:', loss/round_data.length);
|
||||
}
|
||||
console.log('Trained neural network:', JSON.stringify(net.toJSON()));
|
||||
|
||||
function net_classifier(features) {
|
||||
assert(input.w.length === features.length);
|
||||
input.w = features;
|
||||
var softmax = net.forward(input).w;
|
||||
assert(softmax.length === 2);
|
||||
return softmax[1] - softmax[0];
|
||||
}
|
||||
console.log('Neural-net accuracy:', evaluate(sample, net_classifier));
|
||||
|
||||
function combined_classifier(weight) {
|
||||
return function(features) {
|
||||
return hand_tuned_classifier(features) + weight*net_classifier(features);
|
||||
}
|
||||
}
|
||||
var weights = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
|
||||
for (var i = 0; i < weights.length; i++) {
|
||||
console.log('Weight', weights[i], 'combined accuracy:',
|
||||
evaluate(sample, combined_classifier(weights[i])));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user