console.clear() const Matrix = require('./Matrix') // Create the neural network class…
- User
console.clear() const Matrix = require('./Matrix')
// Create the neural network class NeuralNetwork { constructor(input_nodes, hidden_nodes, output_nodes) { this.input_nodes = input_nodes; this.hidden_nodes = hidden_nodes; this.output_nodes = output_nodes;
this.weights_ih = new Matrix(this.hidden_nodes, this.input_nodes); this.weights_ho = new Matrix(this.output_nodes, this.hidden_nodes); this.bias_h = new Matrix(this.hidden_nodes, 1); this.bias_o = new Matrix(this.output_nodes, 1); } train(input_array, target_array) { // Generating the Hidden Outputs let inputs = Matrix.fromArray(input_array); let hidden = this.weights_ih.multiply(inputs); hidden.add(this.bias_h); hidden.map(Matrix.sigmoid); // activation function // Generating the output"s output! let outputs = this.weights_ho.multiply(hidden); outputs.add(this.bias_o); outputs.map(Matrix.sigmoid); // activation function // // Convert array to matrix object let targets = Matrix.fromArray(target_array); // // Calculate the error // ERROR = TARGETS - OUTPUTS let output_errors = Matrix.subtract(targets, outputs); // // Calculate gradient let gradients = Matrix.map(outputs, Matrix.dsigmoid); gradients.multiply(output_errors); gradients.multiply(0.1); // // Calculate deltas let hidden_T = Matrix.transpose(hidden); let weights_ho_deltas = gradients.multiply(hidden_T); // // Adjust the weights by deltas this.weights_ho.add(weights_ho_deltas); // // Adjust the bias by its deltas (which is just the gradients) this.bias_o.add(gradients); // // Calculate the hidden layer errors let who_t = Matrix.transpose(this.weights_ho); let hidden_errors = who_t.multiply(output_errors); // // Calculate hidden gradient let hidden_gradient = Matrix.map(hidden, Matrix.dsigmoid); hidden_gradient.multiply(hidden_errors); hidden_gradient.multiply(0.1); console.log(`weights_ho`,this.weights_ho.data) console.log(`hidden`,hidden.data) console.log(`targets`,targets.data) console.log(`outputs`,outputs.data) console.log(`output_errors`,output_errors.data) console.log(`gradients`,gradients.data) console.log(`hidden_T`,hidden_T.data) console.log(`weights_ho_deltas`,weights_ho_deltas.data) console.log(`who_t`,who_t.data) console.log(`hidden_errors`,hidden_errors.data) console.log(`hidden_gradient`,hidden_gradient.data) // Calcuate input->hidden deltas let inputs_T = Matrix.transpose(inputs); let weights_ih_deltas = hidden_gradient.multiply(inputs_T); this.weights_ih.add(weights_ih_deltas); // Adjust the bias by its deltas (which is just the gradients) this.bias_h.add(hidden_gradient); } predict(input_array) { // Generating the Hidden Outputs let inputs = Matrix.fromArray(input_array); let hidden = this.weights_ih.multiply(inputs); hidden.add(this.bias_h); hidden.map(Matrix.sigmoid); // activation function // Generating the output"s output! let outputs = this.weights_ho.multiply(hidden); outputs.add(this.bias_o); outputs.map(Matrix.sigmoid); // activation function // Sending back to the caller! return outputs.toArray(); } - Assistant
The error you're referring to occurs because you're trying to multiply two matrices directly, likely with incompatible dimensions. The
multiplymethod inside theMatrixclass currently only supports multiplying a matrix by another matrix if it's doing matrix multiplication (with matching inner dimensions), or multiplying a matrix by a scalar (a single number).When you call:
hidden_gradient.multiply(hidden_errors);You're trying to perform an element-wise multiplication, but the
multiplyimplementation expects a matrix multiplication.To fix this error, you need to add an element-wise multiplication method in the
Matrixclass. This method should take another matrix of the same shape and multiply each corresponding element. Here's how you could implement it:hadamard(other) { if (this.rows !== other.rows || this.cols !== other.cols) { throw new Error("Sizes do not match."); } return this.map((value, i, j) => value * other.data[i][j]); } - User
- Assistant
- User
print code from "Here’s a suggestion"
- Assistant
Certainly! Here's how you could add logging to the
trainfunction of yourNeuralNetworkclass to monitor various parameters such as gradient magnitudes, weight adjustments, and layer outputs. The logs will help in understanding the training process and diagnosing any issues with the learning: