Skip to content

Commit

Permalink
refactor: extract Neural Net function in a Seperate Module
Browse files Browse the repository at this point in the history
  • Loading branch information
Keshav-writes-code committed Sep 28, 2024
1 parent 2c343d8 commit c984809
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/components/NN_comps/NN_IOgraph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Chart } from "chart.js/auto";
import { Layer, af_enum, XYDataCollector, Neuron } from "./NN_classes.ts";
import { neuralNetwork } from "./NN_function.ts";
import {
hidOutLayers_store,
selActivaFn_store,
Expand Down Expand Up @@ -41,27 +42,7 @@
// Hidden & Output Layer Combined
function neuralNetwork(x: number | string, layers: Layer[], func: Function) {
x = parseFloat(x.toString());
let inputs = [x];
for (let i = 0; i < layers.length - 1; i++) {
const layer = layers[i];
const outputs = [];
for (const neuron of layer.neurons) {
const weightedSum =
neuron.weights.reduce(
(sum, weight, i) => sum + weight * inputs[i],
0
) + neuron.bias;
let neuronOut = func(weightedSum);
outputs.push(neuronOut);
}
inputs = outputs; // Set inputs for the next layer
}
return inputs.reduce((sum, output) => sum + output, 0); // Summing the final layer's outputs
}
// ---------------------------------------
// -------------- Plotting ---------------
Expand Down
20 changes: 20 additions & 0 deletions src/components/NN_comps/NN_function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Layer, Neuron } from "./NN_classes.ts";

export function neuralNetwork(x: number | string, layers: Layer[], func: Function) {
x = parseFloat(x.toString());
let inputs = [x];

for (let i = 0; i < layers.length - 1; i++) {
const layer = layers[i];
const outputs = [];
for (const neuron of layer.neurons) {
const weightedSum =
neuron.weights.reduce((sum, weight, i) => sum + weight * inputs[i], 0) +
neuron.bias;
let neuronOut = func(weightedSum);
outputs.push(neuronOut);
}
inputs = outputs; // Set inputs for the next layer
}
return inputs.reduce((sum, output) => sum + output, 0); // Summing the final layer's outputs
}

0 comments on commit c984809

Please sign in to comment.