Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. const math = require("mathjs");
  2. /**
  3. * @typedef {[number]} inputVector
  4. *
  5. */
  6. /**
  7. * @typedef {number} expectedOutput
  8. */
  9.  
  10. /**
  11. * @param {number} learningRate - between 0 and 1 (larger values make the weight change more volatile)
  12. * @param trainingSet {[{inputVector,expectedOutput,song}]}
  13. */
  14. function perceptron(trainingSet) {
  15. const learningRate = 1;
  16. console.log(trainingSet);
  17. const inputsWithBias = trainingSet.map(item => [1, ...item.inputVector]);
  18. const weights = new Array(inputsWithBias[0].length).fill(0);
  19. const trainedNetwork = inputsWithBias.reduce(
  20. (updatedWeights, curInput, ind) => {
  21. const output = math.multiply(updatedWeights, curInput);
  22. const newWeights = weights.map(
  23. (weight, i) =>
  24. weight +
  25. learningRate *
  26. (trainingSet[ind].expectedOutput - output) *
  27. curInput[i]
  28. );
  29. return newWeights;
  30. },
  31. weights
  32. );
  33. console.log(trainedNetwork);
  34. return trainedNetwork;
  35. // console.log(wegihts)
  36. // math.multiply(weights,trainingSet)
  37. }
  38.  
  39. const trainedNetwork = perceptron([
  40. { inputVector: [6], expectedOutput: 1 },
  41. { inputVector: [-5], expectedOutput: -1 },
  42. { inputVector: [10], expectedOutput: 1 },
  43. { inputVector: [-2], expectedOutput: 1 }
  44. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement