-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract Neural Net function in a Seperate Module
- Loading branch information
1 parent
2c343d8
commit c984809
Showing
2 changed files
with
22 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |