LMS 機器學習方法

# 程式全文

``````public abstract class LMS {
Object[] examples;
double[] values;
int[] successors;
double[] weights;

public LMS(Object[] E, double[] V, int[] S, double[] W) {
examples = E; values = V; successors = S; weights = W;
}

public void learn() {
// For each training example (e, V(s)) where  s = successor(e)
for (int ei=0; ei<examples.length; ei++) {
Object e  = examples[ei];
// use the current weight to calculate V(e)
double ve = value(e);
double vs = values[successors[ei]];
// For each weight wi, update it as
// wi = wi + c( V(s) - V(e) )
for (int wi=0; wi<weights.length; wi++)
weights[wi] = weights[wi]+0.001 * (vs - ve);
}
System.out.print("w=");
for (int wi=0; wi<weights.length; wi++)
System.out.print(weights[wi]+",");
System.out.println();
}

public abstract double value(Object e);
}

public class LMSTest extends LMS {
public static void main(String[] args) {
Object[]     E = {new Double(1), new Double(3), new Double(5)};
double[]       V = {2, 4, 6};
int[]         S = {0, 1, 2};
double[]    W = {0,0};

LMSTest learner = new LMSTest(E,V,S,W);
for (int i=0; i<1000; i++)
learner.learn();
}

public LMSTest(Object[] E, double[] V, int[] S, double[] W) {
super(E,V,S,W);
}

public double value(Object example) {
double x = ((Double) example).doubleValue();
return weights[0]+weights[1]*x;
}
}```
```