~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/classifiers/functions/neural/LinearUnit.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2 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, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 *    LinearUnit.java
 
19
 *    Copyright (C) 2001 University of Waikato, Hamilton, New Zealand
 
20
 */
 
21
 
 
22
package weka.classifiers.functions.neural;
 
23
 
 
24
/**
 
25
 * This can be used by the 
 
26
 * neuralnode to perform all it's computations (as a Linear unit).
 
27
 *
 
28
 * @author Malcolm Ware (mfw4@cs.waikato.ac.nz)
 
29
 * @version $Revision: 1.6 $
 
30
 */
 
31
public class LinearUnit implements NeuralMethod {
 
32
 
 
33
  /** for serialization */
 
34
  private static final long serialVersionUID = 8572152807755673630L;
 
35
  
 
36
  /**
 
37
   * This function calculates what the output value should be.
 
38
   * @param node The node to calculate the value for.
 
39
   * @return The value.
 
40
   */
 
41
  public double outputValue(NeuralNode node) {
 
42
    double[] weights = node.getWeights();
 
43
    NeuralConnection[] inputs = node.getInputs();
 
44
    double value = weights[0];
 
45
    for (int noa = 0; noa < node.getNumInputs(); noa++) {
 
46
      
 
47
      value += inputs[noa].outputValue(true) 
 
48
        * weights[noa+1];
 
49
    }
 
50
     
 
51
    return value;
 
52
  }
 
53
  
 
54
  /**
 
55
   * This function calculates what the error value should be.
 
56
   * @param node The node to calculate the error for.
 
57
   * @return The error.
 
58
   */
 
59
  public double errorValue(NeuralNode node) {
 
60
    //then calculate the error.
 
61
    
 
62
    NeuralConnection[] outputs = node.getOutputs();
 
63
    int[] oNums = node.getOutputNums();
 
64
    double error = 0;
 
65
 
 
66
    for (int noa = 0; noa < node.getNumOutputs(); noa++) {
 
67
      error += outputs[noa].errorValue(true) 
 
68
        * outputs[noa].weightValue(oNums[noa]);
 
69
    }
 
70
    return error;
 
71
  }
 
72
 
 
73
  /**
 
74
   * This function will calculate what the change in weights should be
 
75
   * and also update them.
 
76
   * @param node The node to update the weights for.
 
77
   * @param learn The learning rate to use.
 
78
   * @param momentum The momentum to use.
 
79
   */
 
80
  public void updateWeights(NeuralNode node, double learn, double momentum) {
 
81
 
 
82
    NeuralConnection[] inputs = node.getInputs();
 
83
    double[] cWeights = node.getChangeInWeights();
 
84
    double[] weights = node.getWeights();
 
85
    
 
86
    double learnTimesError = 0;
 
87
    learnTimesError = learn * node.errorValue(false);
 
88
    
 
89
    double c = learnTimesError + momentum * cWeights[0];
 
90
    weights[0] += c;
 
91
    cWeights[0] = c;
 
92
      
 
93
    int stopValue = node.getNumInputs() + 1;
 
94
    for (int noa = 1; noa < stopValue; noa++) {
 
95
      
 
96
      c = learnTimesError * inputs[noa-1].outputValue(false);
 
97
      c += momentum * cWeights[noa];
 
98
      
 
99
      weights[noa] += c;
 
100
      cWeights[noa] = c; 
 
101
    }
 
102
  }
 
103
    
 
104
}