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.learner.loss;
031
032import org.apache.log4j.Logger;
033import org.openimaj.math.matrix.CFMatrixUtils;
034
035import gov.sandia.cognition.math.matrix.Matrix;
036import gov.sandia.cognition.math.matrix.Vector;
037import gov.sandia.cognition.math.matrix.mtj.SparseMatrix;
038import gov.sandia.cognition.math.matrix.mtj.SparseMatrixFactoryMTJ;
039
040public class MatSquareLossFunction extends LossFunction{
041        Logger logger = Logger.getLogger(MatSquareLossFunction.class);
042        private SparseMatrixFactoryMTJ spf;
043        public MatSquareLossFunction() {
044                spf = SparseMatrixFactoryMTJ.INSTANCE;
045        }
046        @Override
047        public Matrix gradient(Matrix W) {
048                Matrix ret = W.clone();
049                if(CFMatrixUtils.containsInfinity(X)){
050                        throw new RuntimeException();
051                }
052                if(CFMatrixUtils.containsInfinity(W)){
053                        throw new RuntimeException();
054                }
055                Matrix resid = CFMatrixUtils.fastdot(X,W);
056                if(CFMatrixUtils.containsInfinity(resid)){
057                        CFMatrixUtils.fastdot(X,W);
058                        throw new RuntimeException();
059                }
060                if(this.bias!=null)
061                {
062                        resid.plusEquals(this.bias);
063                }
064                CFMatrixUtils.fastminusEquals(resid, Y);
065                if(CFMatrixUtils.containsInfinity(resid)){
066                        throw new RuntimeException();
067                }
068                for (int t = 0; t < resid.getNumColumns(); t++) {
069                        Vector xcol = this.X.getRow(t).scale(resid.getElement(t, t)).clone();
070                        CFMatrixUtils.fastsetcol(ret,t, xcol);
071                }
072                return ret;
073        }
074        @Override
075        public double eval(Matrix W) {
076                Matrix resid = null;
077                if(W == null){
078                        resid = X.clone();
079                } else {
080                        resid = CFMatrixUtils.fastdot(X,W);
081                }
082                Matrix vnobias = resid.clone();
083                if(this.bias!=null)
084                {
085                        resid.plusEquals(this.bias);
086                }
087                Matrix v = resid.clone();
088                resid.minusEquals(Y);
089                double retval = 0;
090                
091                for (int t = 0; t < resid.getNumColumns(); t++) {
092                        double loss = resid.getElement(t, t);
093                        retval += loss * loss;
094                        logger.debug(
095                                        String.format(
096                                                        "yr=%d,y=%3.2f,v=%3.2f,v(no bias)=%2.5f,error=%2.5f,serror=%2.5f",
097                                                        t, Y.getElement(t, t), v.getElement(t, t), vnobias.getElement(t,t), loss, loss*loss
098                                                        )
099                                        );
100                }
101                return retval;
102        }
103        
104        @Override
105        public boolean test_backtrack(Matrix W, Matrix grad, Matrix prox, double eta) {
106                Matrix tmp = prox.minus(W);
107        double evalW = eval(W);
108                double evalProx = eval(prox);
109                Matrix fastdotGradTmp = CFMatrixUtils.fastdot(grad.transpose(),tmp);
110                double normGradProx = CFMatrixUtils.sum(fastdotGradTmp);
111                double normTmp = 0.5*eta*tmp.normFrobenius();
112                return (evalProx <= evalW + normGradProx + normTmp);
113        }
114        
115        @Override
116        public boolean isMatrixLoss() {
117                return true;
118        }
119}