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