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.evaluation; 031 032import gov.sandia.cognition.math.matrix.Matrix; 033import gov.sandia.cognition.math.matrix.mtj.SparseMatrix; 034 035import java.util.List; 036 037import org.apache.log4j.Logger; 038import org.openimaj.math.matrix.CFMatrixUtils; 039import org.openimaj.ml.linear.learner.BilinearLearnerParameters; 040import org.openimaj.ml.linear.learner.BilinearSparseOnlineLearner; 041import org.openimaj.ml.linear.learner.loss.LossFunction; 042import org.openimaj.ml.linear.learner.loss.MatLossFunction; 043import org.openimaj.util.pair.Pair; 044 045public class RootMeanSumLossEvaluator extends BilinearEvaluator { 046 Logger logger = Logger.getLogger(RootMeanSumLossEvaluator.class); 047 048 @Override 049 public double evaluate(List<Pair<Matrix>> data) { 050 final Matrix u = learner.getU(); 051 final Matrix w = learner.getW(); 052 final Matrix bias = learner.getBias(); 053 final double sumloss = sumLoss(data, u, w, bias, learner.getParams()); 054 return sumloss; 055 } 056 057 public double sumLoss(List<Pair<Matrix>> pairs, Matrix u, Matrix w, Matrix bias, BilinearLearnerParameters params) { 058 LossFunction loss = params.getTyped(BilinearLearnerParameters.LOSS); 059 if(!loss.isMatrixLoss()) loss = new MatLossFunction(loss); 060 double total = 0; 061 int i = 0; 062 int ntasks = 0; 063 boolean forceSparcity = learner.getParams().getTyped(BilinearLearnerParameters.FORCE_SPARCITY); 064 if(forceSparcity){ 065 u = CFMatrixUtils.asSparseColumn(u); 066 w = CFMatrixUtils.asSparseColumn(w); 067 068 } 069 for (final Pair<Matrix> pair : pairs) { 070 final Matrix X = pair.firstObject(); 071 final Matrix Y = pair.secondObject(); 072 final SparseMatrix Yexp = BilinearSparseOnlineLearner.expandY(Y); 073 Matrix xt = X.transpose(); 074 Matrix ut = u.transpose(); 075 final Matrix expectedAll = CFMatrixUtils.fastdot(CFMatrixUtils.fastdot(ut,xt),w); 076 loss.setY(Yexp); 077 loss.setX(expectedAll); 078 if (bias != null) 079 loss.setBias(bias); 080 081 logger.debug("Testing pair: " + i); 082 total += loss.eval(null); // Assums an identity w. 083 i++; 084 ntasks += Y.getNumColumns(); 085 } 086 total /= ntasks; 087 088 return Math.sqrt(total); 089 } 090}