Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //initialize weights
- for (int i = 0; i < n; i++) {
- weights[i] = r.nextDouble();
- }
- for (int i = 0; i < maxIterations; i++) {
- int totalError = 0;
- for (int j = 0; j < outputs.length; j++) { // = ~5.000-10.000
- //classifiy object
- int output = classifyInt(inputs[j], false);
- //calculate error
- int error = output - outputs[j];
- //sum of errors
- totalError += error;
- for (int k = 0; k < n; k++) { //n = 4
- //weight is unchanged if there is no error, else change it
- weights[k] += inputs[j][k] * lrate * error;
- }
- }
- i++;
- //done if there are no errors
- if (totalError == 0) {
- Log.i(LOG_TAG, String.valueOf(i));
- Log.i(LOG_TAG, "classifying completed with no errors");
- break;
- }
- }
- private int classifyInt(double[] input, boolean b) {
- double sum = 0.0;
- for (int i = 0; i < input.length; i++) {
- sum += weights[i] * input[i];
- }
- //(just for debugging)
- if (b) {
- System.out.println(sum);
- }
- if (sum > threshold)
- return 1;
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement