编辑:原始代码现在在下面。
当我对三元运算符进行某些修改时,我得到net.optimized的内存属性,在我使用此函数的web-worker中,因为optimized为null而无法调用。我知道这不是网络工作者的问题,因为只要删除我的添加内容,代码就可以正常工作(原始的三元组和工作者)。
我知道代码很乱(原始代码现在在下面),但是当我尝试使用if语句而不是三元组添加到美化版本时,此函数会出现各种错误。即使没有我的补充。
我已经尝试了尽可能多的调试,并且null错误是由于我的添加。
这些是我试图做的补充:
1。)j !== undefined && j instanceof jay ? j.recursive(function(){r(t[l].input)}) : r(t[l].input)
应该是
if(j!== undefined && j instanceof jay) j.recursive(function(){r(t[l].input)})
else r(t[l].input);
2。)j !== undefined && j instanceof jay ? j.recursive(function(){l++}, false) : l++
应该是
if(j !== undefined && j instanceof jay) j.recursive(function(){l++}, false)
else l++
3。)j !== undefined && j instanceof jay ? j.recursive(function(){r(t[l].input)}) : r(t[l].input);
应该是
if(j !== undefined && j instanceof jay) j.recursive(function(){r(t[l].input)})
else r(t[l].input);
这对我来说在逻辑上是正确的,但我做错了,导致工人的开始变得混乱。以下是工人的整个onmessage功能。
修改:现在完整代码
workerTrain: function(set, callback, options, z) {
var that = this;
var error = 1;
var iterations = bucketSize = 0;
var input, output, target, currentRate;
var length = set.length;
var start = Date.now();
if (options) {
if (options.shuffle) {
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o) { //v1.0
for (var j, x, i = o.length; i; j = Math.floor(Math.random() *
i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
}
if (options.iterations)
this.iterations = options.iterations;
if (options.error)
this.error = options.error;
if (options.rate)
this.rate = options.rate;
if (options.cost)
this.cost = options.cost;
if (options.schedule)
this.schedule = options.schedule;
if (options.customLog)
// for backward compatibility with code that used customLog
console.log('Deprecated: use schedule instead of customLog')
this.schedule = options.customLog;
}
// dynamic learning rate
currentRate = this.rate;
if(Array.isArray(this.rate)) {
bucketSize = Math.floor(this.iterations / this.rate.length);
}
// create a worker
var worker = this.network.worker();
// activate the network
function activateWorker(input)
{
worker.postMessage({
action: "activate",
input: input,
memoryBuffer: that.network.optimized.memory
}, [that.network.optimized.memory.buffer]);
}
// backpropagate the network
function propagateWorker(target){
if(bucketSize > 0) {
var currentBucket = Math.floor(iterations / bucketSize);
currentRate = this.rate[currentBucket];
}
worker.postMessage({
action: "propagate",
target: target,
rate: currentRate,
memoryBuffer: that.network.optimized.memory
}, [that.network.optimized.memory.buffer]);
}
// train the worker
worker.onmessage = function(e){
// give control of the memory back to the network
that.network.optimized.ownership(e.data.memoryBuffer);
if (e.data.action == "propagate")
{
if (index >= length)
{
index = 0;
iterations++;
error /= set.length;
// log
if (options) {
if (this.schedule && this.schedule.every && iterations % this.schedule.every == 0)
abort_training = this.schedule.do({
error: error,
iterations: iterations
});
else if (options.log && iterations % options.log == 0) {
console.log('iterations', iterations, 'error', error);
};
if (options.shuffle)
shuffle(set);
}
if (!abort_training && iterations < that.iterations && error > that.error)
{
activateWorker(set[index].input);
} else {
// callback
callback({
error: error,
iterations: iterations,
time: Date.now() - start
})
}
error = 0;
} else if(z!==undefined && z instanceof jay) z.recursive(function(){
activateWorker(set[index].input)
});
else activateWorker(set[index].input);
}
if (e.data.action == "activate")
{
error += that.cost(set[index].output, e.data.output);
propagateWorker(set[index].output);
if(z !== undefined && z instanceof jay) z.recursive(function(){index++}, false);
else index++;
}
}
// kick it
var index = 0;
var iterations = 0;
if(z!==undefined && z instanceof jay) z.recursive(function(){activateWorker(set[index].input)});
else activateWorker(set[index].input);
},