Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const math = require("mathjs");
- /**
- * @typedef {[number]} inputVector
- *
- */
- /**
- * @typedef {number} expectedOutput
- */
- /**
- * @param {number} learningRate - between 0 and 1 (larger values make the weight change more volatile)
- * @param trainingSet {[{inputVector,expectedOutput,song}]}
- */
- function perceptron(trainingSet) {
- const learningRate = 1;
- console.log(trainingSet);
- const inputsWithBias = trainingSet.map(item => [1, ...item.inputVector]);
- const weights = new Array(inputsWithBias[0].length).fill(0);
- const trainedNetwork = inputsWithBias.reduce(
- (updatedWeights, curInput, ind) => {
- const output = math.multiply(updatedWeights, curInput);
- const newWeights = weights.map(
- (weight, i) =>
- weight +
- learningRate *
- (trainingSet[ind].expectedOutput - output) *
- curInput[i]
- );
- return newWeights;
- },
- weights
- );
- console.log(trainedNetwork);
- return trainedNetwork;
- // console.log(wegihts)
- // math.multiply(weights,trainingSet)
- }
- const trainedNetwork = perceptron([
- { inputVector: [6], expectedOutput: 1 },
- { inputVector: [-5], expectedOutput: -1 },
- { inputVector: [10], expectedOutput: 1 },
- { inputVector: [-2], expectedOutput: 1 }
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement