MCPcopy
hub / github.com/BrainJS/brain.js / toFunction

Method toFunction

src/recurrent/rnn.js:517–717  ·  view source on GitHub ↗

* * @returns {Function}

()

Source from the content-addressed store, hash-verified

515 * @returns {Function}
516 */
517 toFunction() {
518 let model = this.model;
519 let equations = this.model.equations;
520 let equation = equations[1];
521 let states = equation.states;
522 let jsonString = JSON.stringify(this.toJSON());
523
524 function matrixOrigin(m, stateIndex) {
525 for (let i = 0, max = states.length; i < max; i++) {
526 let state = states[i];
527
528 if (i === stateIndex) {
529 let j = previousConnectionIndex(m);
530 switch (m) {
531 case state.left:
532 if (j > -1) {
533 return `typeof prevStates[${ j }] === 'object' ? prevStates[${ j }].product : new Matrix(${ m.rows }, ${ m.columns })`;
534 }
535 case state.right:
536 if (j > -1) {
537 return `typeof prevStates[${ j }] === 'object' ? prevStates[${ j }].product : new Matrix(${ m.rows }, ${ m.columns })`;
538 }
539 case state.product:
540 return `new Matrix(${ m.rows }, ${ m.columns })`;
541 default:
542 throw Error('unknown state');
543 }
544 }
545
546 if (m === state.product) return `states[${ i }].product`;
547 if (m === state.right) return `states[${ i }].right`;
548 if (m === state.left) return `states[${ i }].left`;
549 }
550 }
551
552 function previousConnectionIndex(m) {
553 const connection = model.equationConnections[0];
554 const states = equations[0].states;
555 for (let i = 0, max = states.length; i < max; i++) {
556 if (states[i].product === m) {
557 return i;
558 }
559 }
560 return connection.indexOf(m);
561 }
562
563 function matrixToString(m, stateIndex) {
564 if (!m || !m.rows || !m.columns) return 'null';
565
566 if (m === model.input) return `json.input`;
567 if (m === model.outputConnector) return `json.outputConnector`;
568 if (m === model.output) return `json.output`;
569
570 for (let i = 0, max = model.hiddenLayers.length; i < max; i++) {
571 let hiddenLayer = model.hiddenLayers[i];
572 for (let p in hiddenLayer) {
573 if (!hiddenLayer.hasOwnProperty(p)) continue;
574 if (hiddenLayer[p] !== m) continue;

Callers 5

lstm.jsFile · 0.45
gru.jsFile · 0.45
rnn-time-step.jsFile · 0.45
rnn.jsFile · 0.45
to-function.jsFile · 0.45

Calls 6

toJSONMethod · 0.95
matrixToStringFunction · 0.85
fileNameFunction · 0.85
toInnerFunction · 0.85
toStringMethod · 0.80
toFunctionStringMethod · 0.80

Tested by

no test coverage detected