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

Method nodeHandle

src/neural-network.js:940–975  ·  view source on GitHub ↗
(layers, layerNumber, nodeKey)

Source from the content-addressed store, hash-verified

938 const leakyReluAlpha = this.leakyReluAlpha;
939 let needsVar = false;
940 function nodeHandle(layers, layerNumber, nodeKey) {
941 if (layerNumber === 0) {
942 return (typeof nodeKey === 'string'
943 ? `input['${nodeKey}']`
944 : `input[${nodeKey}]`);
945 }
946
947 const layer = layers[layerNumber];
948 const node = layer[nodeKey];
949 let result = ['(' , node.bias];
950 for (let w in node.weights) {
951 if (node.weights[w] < 0) {
952 result.push(`${node.weights[w]}*${nodeHandle(layers, layerNumber - 1, w)}`);
953 } else {
954 result.push(`+${node.weights[w]}*${nodeHandle(layers, layerNumber - 1, w)}`);
955 }
956 }
957 result.push(')');
958
959 switch (activation) {
960 case 'sigmoid':
961 return `1/(1+1/Math.exp(${result.join('')}))`;
962 case 'relu': {
963 needsVar = true;
964 return `((v=${result.join('')})<0?0:v)`;
965 }
966 case 'leaky-relu': {
967 needsVar = true;
968 return `((v=${result.join('')})<0?0:${leakyReluAlpha}*v)`;
969 }
970 case 'tanh':
971 return `Math.tanh(${result.join('')})`;
972 default:
973 throw new Error('unknown activation type ' + activation);
974 }
975 }
976
977 const layers = this.toJSON().layers;
978 const layersAsMath = [];

Callers

nothing calls this directly

Calls 1

nodeHandleFunction · 0.85

Tested by

no test coverage detected