View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.demos.ml.linear.data;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import org.openimaj.ml.linear.data.FixedDataGenerator;
36  import org.openimaj.ml.linear.kernel.LinearVectorKernel;
37  import org.openimaj.ml.linear.learner.perceptron.DoubleArrayKernelPerceptron;
38  import org.openimaj.ml.linear.learner.perceptron.MeanCenteredKernelPerceptron;
39  import org.openimaj.ml.linear.learner.perceptron.PerceptronClass;
40  import org.openimaj.ml.linear.learner.perceptron.ThresholdDoubleArrayKernelPerceptron;
41  import org.openimaj.util.pair.IndependentPair;
42  
43  import cern.colt.Arrays;
44  
45  /**
46   *
47   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
48   */
49  public class WikipediaPerceptronExample {
50  
51  	/**
52  	 * @param args
53  	 */
54  	public static void main(String[] args) {
55  		thresholded(createData());
56  		centered(createData());
57  	}
58  
59  	private static void centered(FixedDataGenerator<double[], PerceptronClass> fdg) {
60  		System.out.println("CENTERED");
61  		final DoubleArrayKernelPerceptron mkp = new MeanCenteredKernelPerceptron(new LinearVectorKernel());
62  		for (int i = 0; i < 10; i++) {
63  			System.out.println("Iteration: " + i);
64  			for (int j = 0; j < 4; j++) {
65  				final IndependentPair<double[], PerceptronClass> v = fdg.generate();
66  				final double[] x = v.firstObject();
67  				final PerceptronClass y = v.secondObject();
68  				final PerceptronClass yestb = mkp.predict(x);
69  				mkp.process(x, y);
70  				final PerceptronClass yesta = mkp.predict(x);
71  
72  				System.out.println(String.format("x: %s, y: %s, ypred_b: %s, ypred_a: %s", Arrays.toString(x), y, yestb,
73  						yesta));
74  				// System.out.println(mkp.getWeights());
75  			}
76  		}
77  	}
78  
79  	private static FixedDataGenerator<double[], PerceptronClass> createData() {
80  
81  		final List<IndependentPair<double[], PerceptronClass>> data = new ArrayList<IndependentPair<double[], PerceptronClass>>();
82  		data.add(IndependentPair.pair(new double[] { 1, 0, 0 }, PerceptronClass.TRUE));
83  		data.add(IndependentPair.pair(new double[] { 1, 0, 1 }, PerceptronClass.TRUE));
84  		data.add(IndependentPair.pair(new double[] { 1, 1, 0 }, PerceptronClass.TRUE));
85  		data.add(IndependentPair.pair(new double[] { 1, 1, 1 }, PerceptronClass.FALSE));
86  		final FixedDataGenerator<double[], PerceptronClass> fdg = new FixedDataGenerator<double[], PerceptronClass>(data);
87  		return fdg;
88  	}
89  
90  	private static void thresholded(
91  			FixedDataGenerator<double[], PerceptronClass> fdg)
92  	{
93  		System.out.println("Thresholded");
94  		final DoubleArrayKernelPerceptron mkp = new ThresholdDoubleArrayKernelPerceptron(new LinearVectorKernel());
95  		for (int i = 0; i < 10; i++) {
96  			System.out.println("Iteration: " + i);
97  			for (int j = 0; j < 4; j++) {
98  				final IndependentPair<double[], PerceptronClass> v = fdg.generate();
99  				final double[] x = v.firstObject();
100 				final PerceptronClass y = v.secondObject();
101 				final PerceptronClass yestb = mkp.predict(x);
102 				mkp.process(x, y);
103 				final PerceptronClass yesta = mkp.predict(x);
104 
105 				System.out.println(String.format("x: %s, y: %s, ypred_b: %s, ypred_a: %s", Arrays.toString(x), y, yestb,
106 						yesta));
107 				// System.out.println(mkp.getWeights());
108 			}
109 		}
110 	}
111 
112 }