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

Method toFunction

src/recurrent/rnn-time-step.js:149–324  ·  view source on GitHub ↗

* * @returns {Function}

()

Source from the content-addressed store, hash-verified

147 * @returns {Function}
148 */
149 toFunction() {
150 let model = this.model;
151 let equations = this.model.equations;
152 const inputSize = this.inputSize;
153 debugger;
154 let equation = equations[1];
155 let states = equation.states;
156 let jsonString = JSON.stringify(this.toJSON());
157
158 function matrixOrigin(m, stateIndex) {
159 for (let i = 0, max = states.length; i < max; i++) {
160 let state = states[i];
161
162 if (i === stateIndex) {
163 let j = previousConnectionIndex(m);
164 switch (m) {
165 case state.left:
166 if (j > -1) {
167 return `typeof prevStates[${ j }] === 'object' ? prevStates[${ j }].product : new Matrix(${ m.rows }, ${ m.columns })`;
168 }
169 case state.right:
170 if (j > -1) {
171 return `typeof prevStates[${ j }] === 'object' ? prevStates[${ j }].product : new Matrix(${ m.rows }, ${ m.columns })`;
172 }
173 case state.product:
174 return `new Matrix(${ m.rows }, ${ m.columns })`;
175 default:
176 throw Error('unknown state');
177 }
178 }
179
180 if (m === state.product) return `states[${ i }].product`;
181 if (m === state.right) return `states[${ i }].right`;
182 if (m === state.left) return `states[${ i }].left`;
183 }
184 }
185
186 function previousConnectionIndex(m) {
187 const connection = model.equationConnections[0];
188 const states = equations[0].states;
189 for (let i = 0, max = states.length; i < max; i++) {
190 if (states[i].product === m) {
191 return i;
192 }
193 }
194 return connection.indexOf(m);
195 }
196
197 function matrixToString(m, stateIndex) {
198 if (!m || !m.rows || !m.columns) return 'null';
199
200 if (m === model.input) return `json.input`;
201 if (m === model.outputConnector) return `json.outputConnector`;
202 if (m === model.output) return `json.output`;
203
204 for (let i = 0, max = model.hiddenLayers.length; i < max; i++) {
205 let hiddenLayer = model.hiddenLayers[i];
206 for (let p in hiddenLayer) {

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected