-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuralNetwork.ai.js
103 lines (87 loc) · 3.58 KB
/
neuralNetwork.ai.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Matrix } from "./matrix.ai.js";
import { sigmoid, sigmoidDerivative } from "./activationFunctions.ai.js";
/**
* Represents a neural network.
*/
export class NeuralNetwork {
/**
* Creates a new instance of the NeuralNetwork class.
* @param {number} inputSize - The size of the input layer.
* @param {number} hiddenSize - The size of the hidden layer.
* @param {number} outputSize - The size of the output layer.
*/
constructor(inputSize, hiddenSize, outputSize) {
// Initialize weights and biases for input to hidden layer
this.weightsInputHidden = new Matrix(hiddenSize, inputSize);
this.weightsInputHidden.randomize();
this.biasHidden = new Matrix(hiddenSize, 1);
this.biasHidden.randomize();
// Initialize weights and biases for hidden to output layer
this.weightsHiddenOutput = new Matrix(outputSize, hiddenSize);
this.weightsHiddenOutput.randomize();
this.biasOutput = new Matrix(outputSize, 1);
this.biasOutput.randomize();
}
/**
* Performs a feedforward operation on the neural network.
* @param {number[]} inputArray - The input array.
* @returns {number[]} The output array.
*/
feedforward(inputArray) {
// Convert input array to a Matrix object
const inputs = Matrix.fromArray(inputArray);
// Calculate hidden layer output
const hidden = Matrix.dot(this.weightsInputHidden, inputs);
hidden.add(this.biasHidden);
hidden.map(sigmoid);
// Calculate output layer output
const output = Matrix.dot(this.weightsHiddenOutput, hidden);
output.add(this.biasOutput);
output.map(sigmoid);
return output.toArray();
}
/**
* Trains the neural network using backpropagation.
* @param {number[]} inputArray - The input array.
* @param {number[]} targetArray - The target array.
*/
train(inputArray, targetArray) {
// Convert input and target arrays to Matrix objects
const inputs = Matrix.fromArray(inputArray);
const targets = Matrix.fromArray(targetArray);
// === Feedforward ===
// Calculate hidden layer output
const hidden = Matrix.dot(this.weightsInputHidden, inputs);
hidden.add(this.biasHidden);
hidden.map(sigmoid);
// Calculate output layer output
const output = Matrix.dot(this.weightsHiddenOutput, hidden);
output.add(this.biasOutput);
output.map(sigmoid);
// === Backpropagation ===
// Calculate output layer errors
const outputErrors = Matrix.subtract(targets, output);
// Calculate output layer gradients
const outputGradients = Matrix.map(output, sigmoidDerivative);
outputGradients.multiply(outputErrors);
outputGradients.multiply(0.1); // Learning rate
// Calculate hidden layer errors
const hiddenErrors = Matrix.dot(
Matrix.transpose(this.weightsHiddenOutput),
outputErrors
);
// Calculate hidden layer gradients
const hiddenGradients = Matrix.map(hidden, sigmoidDerivative);
hiddenGradients.multiply(hiddenErrors);
hiddenGradients.multiply(0.1); // Learning rate
// Update weights and biases
this.weightsHiddenOutput.add(
Matrix.dot(outputGradients, Matrix.transpose(hidden))
);
this.biasOutput.add(outputGradients);
this.weightsInputHidden.add(
Matrix.dot(hiddenGradients, Matrix.transpose(inputs))
);
this.biasHidden.add(hiddenGradients);
}
}