001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.linear.experiments.sinabill;
031
032import gnu.trove.set.hash.TIntHashSet;
033import gov.sandia.cognition.math.matrix.Matrix;
034import gov.sandia.cognition.math.matrix.MatrixFactory;
035
036import java.io.File;
037import java.io.IOException;
038import java.util.ArrayList;
039import java.util.List;
040
041import org.openimaj.io.IOUtils;
042import org.openimaj.math.matrix.CFMatrixUtils;
043import org.openimaj.ml.linear.data.BillMatlabFileDataGenerator;
044import org.openimaj.ml.linear.data.BillMatlabFileDataGenerator.Mode;
045import org.openimaj.ml.linear.evaluation.BilinearEvaluator;
046import org.openimaj.ml.linear.evaluation.RootMeanSumLossEvaluator;
047import org.openimaj.ml.linear.learner.BilinearLearnerParameters;
048import org.openimaj.ml.linear.learner.BilinearSparseOnlineLearner;
049import org.openimaj.ml.linear.learner.init.SingleValueInitStrat;
050import org.openimaj.ml.linear.learner.init.SparseZerosInitStrategy;
051import org.openimaj.ml.linear.learner.loss.MatSquareLossFunction;
052import org.openimaj.util.pair.Pair;
053
054public class BillAustrianExperiments extends BilinearExperiment {
055
056        public static void main(String[] args) throws IOException {
057                final BillAustrianExperiments exp = new BillAustrianExperiments();
058                exp.performExperiment();
059        }
060
061        @Override
062        public void performExperiment() throws IOException {
063                final BilinearLearnerParameters params = new BilinearLearnerParameters();
064                params.put(BilinearLearnerParameters.ETA0_U, 0.02);
065                params.put(BilinearLearnerParameters.ETA0_W, 0.02);
066                params.put(BilinearLearnerParameters.LAMBDA, 0.001);
067                params.put(BilinearLearnerParameters.BICONVEX_TOL, 0.01);
068                params.put(BilinearLearnerParameters.BICONVEX_MAXITER, 10);
069                params.put(BilinearLearnerParameters.BIAS, true);
070                params.put(BilinearLearnerParameters.ETA0_BIAS, 0.5);
071                params.put(BilinearLearnerParameters.WINITSTRAT, new SingleValueInitStrat(0.1));
072                params.put(BilinearLearnerParameters.UINITSTRAT, new SparseZerosInitStrategy());
073                params.put(BilinearLearnerParameters.LOSS, new MatSquareLossFunction());
074                final BillMatlabFileDataGenerator bmfdg = new BillMatlabFileDataGenerator(
075                                new File(MATLAB_DATA()),
076                                98,
077                                true
078                                );
079                prepareExperimentLog(params);
080                final BilinearSparseOnlineLearner learner = new BilinearSparseOnlineLearner(params);
081                learner.reinitParams();
082                TIntHashSet seenTraining = new TIntHashSet();
083                for (int i = 0; i < bmfdg.nFolds(); i++) {
084                        logger.debug("Fold: " + i);
085
086                        bmfdg.setFold(i, Mode.TEST);
087                        final List<Pair<Matrix>> testpairs = new ArrayList<Pair<Matrix>>();
088                        while (true) {
089                                final Pair<Matrix> next = bmfdg.generate();
090                                if (next == null)
091                                        break;
092                                testpairs.add(next);
093                        }
094                        int j = 0;
095                        logger.debug("...training");
096                        bmfdg.setFold(i, Mode.TRAINING);
097                        while (true) {
098                                final Pair<Matrix> next = bmfdg.generate();
099                                if(seenTraining.contains(j)) {
100                                        logger.debug("...skipping item " + j);
101                                        j++;
102                                        continue;
103                                }
104                                seenTraining.add(j);
105                                if (next == null)
106                                        break;
107                                logger.debug("...trying item " + j);
108                                learner.process(next.firstObject(), next.secondObject());
109                                logger.debug("...done processing item " + j);
110                                j++;
111                                
112                        }
113                        final Matrix u = learner.getU();
114                        final Matrix w = learner.getW();
115                        final Matrix bias = MatrixFactory.getDenseDefault().copyMatrix(learner.getBias());
116                        final BilinearEvaluator eval = new RootMeanSumLossEvaluator();
117                        eval.setLearner(learner);
118                        final double loss = eval.evaluate(testpairs);
119                        logger.debug(String.format("Saving learner, Fold %d, Item %d", i, j));
120                        final File learnerOut = new File(FOLD_ROOT(i), String.format("learner_%d", j));
121                        IOUtils.writeBinary(learnerOut, learner);
122                        logger.debug("W row sparcity: " + CFMatrixUtils.rowSparsity(w));
123                        logger.debug("U row sparcity: " + CFMatrixUtils.rowSparsity(u));
124                        final Boolean biasMode = learner.getParams().getTyped(BilinearLearnerParameters.BIAS);
125                        if (biasMode) {
126                                logger.debug("Bias: " + CFMatrixUtils.diag(bias));
127                        }
128                        logger.debug(String.format("... loss: %f", loss));
129                }
130        }
131
132}