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 gov.sandia.cognition.math.matrix.Matrix;
033import gov.sandia.cognition.math.matrix.MatrixFactory;
034
035import java.io.File;
036import java.io.IOException;
037import java.util.ArrayList;
038import java.util.List;
039
040import org.openimaj.io.IOUtils;
041import org.openimaj.math.matrix.CFMatrixUtils;
042import org.openimaj.ml.linear.data.BillMatlabFileDataGenerator;
043import org.openimaj.ml.linear.evaluation.BilinearEvaluator;
044import org.openimaj.ml.linear.evaluation.RootMeanSumLossEvaluator;
045import org.openimaj.ml.linear.learner.BilinearLearnerParameters;
046import org.openimaj.ml.linear.learner.BilinearSparseOnlineLearner;
047import org.openimaj.ml.linear.learner.init.SparseZerosInitStrategy;
048import org.openimaj.ml.linear.learner.loss.MatSquareLossFunction;
049import org.openimaj.util.pair.Pair;
050
051public class BillAustrianExperimentsNormalised extends BilinearExperiment {
052
053        public static void main(String[] args) throws IOException {
054                final BillAustrianExperimentsNormalised exp = new BillAustrianExperimentsNormalised();
055                exp.performExperiment();
056        }
057
058        @Override
059        public void performExperiment() throws IOException {
060                final BilinearLearnerParameters params = new BilinearLearnerParameters();
061                int INITIAL_TRAIN_NUMBER = 48;
062                params.put(BilinearLearnerParameters.ETA0_U, 5.);
063                params.put(BilinearLearnerParameters.ETA0_W, 5.);
064//              params.put(BilinearLearnerParameters.LAMBDA, 0.00001);
065                params.put(BilinearLearnerParameters.LAMBDA_U, 0.000005);
066                params.put(BilinearLearnerParameters.LAMBDA_W, 0.0005);
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.1);
071                params.put(BilinearLearnerParameters.WINITSTRAT, new SparseZerosInitStrategy());
072                params.put(BilinearLearnerParameters.UINITSTRAT, new SparseZerosInitStrategy());
073                params.put(BilinearLearnerParameters.LOSS, new MatSquareLossFunction());
074//              params.put(BilinearLearnerParameters.Z_STANDARDISE, true);
075                final BillMatlabFileDataGenerator bmfdg = new BillMatlabFileDataGenerator(
076                                new File(MATLAB_DATA("%s/user_vsr_for_polls_SINA.mat")),
077                                "user_vsr_for_polls_SINA",
078                                new File(MATLAB_DATA()),
079                                98,
080                                false
081                                );
082                prepareExperimentLog(params);
083                final BilinearSparseOnlineLearner learner = new BilinearSparseOnlineLearner(params);
084                learner.reinitParams();
085                int j = 0;
086                bmfdg.setFold(-1, null); // Go over all of them
087                logger.debug("... training initial "+INITIAL_TRAIN_NUMBER+" items");
088                while (j < INITIAL_TRAIN_NUMBER) {
089                        final Pair<Matrix> next = bmfdg.generate();     
090                        if (next == null)
091                                break;
092                        logger.debug("...trying item " + j);
093                        learner.process(next.firstObject(), next.secondObject());
094                        logger.debug("...done processing item " + j);
095                        j++;
096                }
097                
098                logger.debug("... testing 5, training 5...");
099                int i = 0;
100                while (true) {
101                        final List<Pair<Matrix>> testpairs = new ArrayList<Pair<Matrix>>();
102                        for (int k = 0; k < 5; k++) {                                   
103                                final Pair<Matrix> next = bmfdg.generate();
104                                if (next == null) break;
105                                testpairs.add(next);
106                        }
107                        if(testpairs.size() == 0)break;
108                        final Matrix u = learner.getU();
109                        final Matrix w = learner.getW();
110                        final Matrix bias = MatrixFactory.getDenseDefault().copyMatrix(learner.getBias());
111                        final BilinearEvaluator eval = new RootMeanSumLossEvaluator();
112                        eval.setLearner(learner);
113                        final double loss = eval.evaluate(testpairs);
114                        logger.debug(String.format("Saving learner, Fold %d, Item %d", i, j));
115                        final File learnerOut = new File(FOLD_ROOT(i), String.format("learner_%d", j));
116                        IOUtils.writeBinary(learnerOut, learner);
117                        logger.debug("W row sparcity: " + CFMatrixUtils.rowSparsity(w));
118                        logger.debug("U row sparcity: " + CFMatrixUtils.rowSparsity(u));
119                        final Boolean biasMode = learner.getParams().getTyped(BilinearLearnerParameters.BIAS);
120                        if (biasMode) {
121                                logger.debug("Bias: " + CFMatrixUtils.diag(bias));
122                        }
123                        logger.debug(String.format("... loss: %f", loss));
124                        
125                        for (Pair<Matrix> next : testpairs) {
126                                logger.debug("...training with tests");
127                                logger.debug("...trying item " + j);
128                                learner.process(next.firstObject(), next.secondObject());
129                                logger.debug("...done processing item " + j);
130                                j++;
131                        }
132                        i++;
133                }
134        }
135
136}