| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LinearUnit |
|
| 1.75;1.75 |
| 1 | /* | |
| 2 | * This program is free software: you can redistribute it and/or modify | |
| 3 | * it under the terms of the GNU General Public License as published by | |
| 4 | * the Free Software Foundation, either version 3 of the License, or | |
| 5 | * (at your option) any later version. | |
| 6 | * | |
| 7 | * This program is distributed in the hope that it will be useful, | |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 10 | * GNU General Public License for more details. | |
| 11 | * | |
| 12 | * You should have received a copy of the GNU General Public License | |
| 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 14 | */ | |
| 15 | ||
| 16 | /* | |
| 17 | * LinearUnit.java | |
| 18 | * Copyright (C) 2001-2012 University of Waikato, Hamilton, New Zealand | |
| 19 | */ | |
| 20 | ||
| 21 | package weka.classifiers.functions.neural; | |
| 22 | ||
| 23 | import weka.core.RevisionHandler; | |
| 24 | import weka.core.RevisionUtils; | |
| 25 | ||
| 26 | /** | |
| 27 | * This can be used by the | |
| 28 | * neuralnode to perform all it's computations (as a Linear unit). | |
| 29 | * | |
| 30 | * @author Malcolm Ware (mfw4@cs.waikato.ac.nz) | |
| 31 | * @version $Revision: 8034 $ | |
| 32 | */ | |
| 33 | 0 | public class LinearUnit |
| 34 | implements NeuralMethod, RevisionHandler { | |
| 35 | ||
| 36 | /** for serialization */ | |
| 37 | private static final long serialVersionUID = 8572152807755673630L; | |
| 38 | ||
| 39 | /** | |
| 40 | * This function calculates what the output value should be. | |
| 41 | * @param node The node to calculate the value for. | |
| 42 | * @return The value. | |
| 43 | */ | |
| 44 | public double outputValue(NeuralNode node) { | |
| 45 | 0 | double[] weights = node.getWeights(); |
| 46 | 0 | NeuralConnection[] inputs = node.getInputs(); |
| 47 | 0 | double value = weights[0]; |
| 48 | 0 | for (int noa = 0; noa < node.getNumInputs(); noa++) { |
| 49 | ||
| 50 | 0 | value += inputs[noa].outputValue(true) |
| 51 | * weights[noa+1]; | |
| 52 | } | |
| 53 | ||
| 54 | 0 | return value; |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * This function calculates what the error value should be. | |
| 59 | * @param node The node to calculate the error for. | |
| 60 | * @return The error. | |
| 61 | */ | |
| 62 | public double errorValue(NeuralNode node) { | |
| 63 | //then calculate the error. | |
| 64 | ||
| 65 | 0 | NeuralConnection[] outputs = node.getOutputs(); |
| 66 | 0 | int[] oNums = node.getOutputNums(); |
| 67 | 0 | double error = 0; |
| 68 | ||
| 69 | 0 | for (int noa = 0; noa < node.getNumOutputs(); noa++) { |
| 70 | 0 | error += outputs[noa].errorValue(true) |
| 71 | * outputs[noa].weightValue(oNums[noa]); | |
| 72 | } | |
| 73 | 0 | return error; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * This function will calculate what the change in weights should be | |
| 78 | * and also update them. | |
| 79 | * @param node The node to update the weights for. | |
| 80 | * @param learn The learning rate to use. | |
| 81 | * @param momentum The momentum to use. | |
| 82 | */ | |
| 83 | public void updateWeights(NeuralNode node, double learn, double momentum) { | |
| 84 | ||
| 85 | 0 | NeuralConnection[] inputs = node.getInputs(); |
| 86 | 0 | double[] cWeights = node.getChangeInWeights(); |
| 87 | 0 | double[] weights = node.getWeights(); |
| 88 | ||
| 89 | 0 | double learnTimesError = 0; |
| 90 | 0 | learnTimesError = learn * node.errorValue(false); |
| 91 | ||
| 92 | 0 | double c = learnTimesError + momentum * cWeights[0]; |
| 93 | 0 | weights[0] += c; |
| 94 | 0 | cWeights[0] = c; |
| 95 | ||
| 96 | 0 | int stopValue = node.getNumInputs() + 1; |
| 97 | 0 | for (int noa = 1; noa < stopValue; noa++) { |
| 98 | ||
| 99 | 0 | c = learnTimesError * inputs[noa-1].outputValue(false); |
| 100 | 0 | c += momentum * cWeights[noa]; |
| 101 | ||
| 102 | 0 | weights[noa] += c; |
| 103 | 0 | cWeights[noa] = c; |
| 104 | } | |
| 105 | 0 | } |
| 106 | ||
| 107 | /** | |
| 108 | * Returns the revision string. | |
| 109 | * | |
| 110 | * @return the revision | |
| 111 | */ | |
| 112 | public String getRevision() { | |
| 113 | 0 | return RevisionUtils.extract("$Revision: 8034 $"); |
| 114 | } | |
| 115 | } |