Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. //initialize weights
  2. for (int i = 0; i < n; i++) {
  3. weights[i] = r.nextDouble();
  4. }
  5.  
  6.  
  7. for (int i = 0; i < maxIterations; i++) {
  8. int totalError = 0;
  9. for (int j = 0; j < outputs.length; j++) { // = ~5.000-10.000
  10. //classifiy object
  11. int output = classifyInt(inputs[j], false);
  12. //calculate error
  13. int error = output - outputs[j];
  14. //sum of errors
  15. totalError += error;
  16.  
  17. for (int k = 0; k < n; k++) { //n = 4
  18. //weight is unchanged if there is no error, else change it
  19. weights[k] += inputs[j][k] * lrate * error;
  20. }
  21.  
  22. }
  23. i++;
  24.  
  25. //done if there are no errors
  26. if (totalError == 0) {
  27. Log.i(LOG_TAG, String.valueOf(i));
  28. Log.i(LOG_TAG, "classifying completed with no errors");
  29. break;
  30. }
  31. }
  32.  
  33. private int classifyInt(double[] input, boolean b) {
  34. double sum = 0.0;
  35. for (int i = 0; i < input.length; i++) {
  36. sum += weights[i] * input[i];
  37. }
  38. //(just for debugging)
  39. if (b) {
  40. System.out.println(sum);
  41. }
  42. if (sum > threshold)
  43. return 1;
  44. else
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement