001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.core.algorithm.classification.mlp;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    
029    import org.jdmp.core.algorithm.AlgorithmFiveSources;
030    import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.BiasType;
031    import org.jdmp.core.variable.Variable;
032    import org.jdmp.core.variable.VariableFactory;
033    import org.ujmp.core.Matrix;
034    import org.ujmp.core.MatrixFactory;
035    import org.ujmp.core.calculation.Calculation.Ret;
036    import org.ujmp.core.exceptions.MatrixException;
037    import org.ujmp.core.util.MathUtil;
038    
039    public class WeightUpdate extends AlgorithmFiveSources {
040            private static final long serialVersionUID = -6437801270586210637L;
041    
042            public static final String WEIGHT = SOURCE1;
043    
044            public static final String ETA = SOURCE2;
045    
046            public static final String CONTACTDEVIATION = SOURCE3;
047    
048            public static final String INPUT = SOURCE4;
049    
050            public static final String SAMPLEWEIGHT = SOURCE5;
051    
052            private BiasType biasType = null;
053    
054            public WeightUpdate(BiasType biasType) {
055                    this.biasType = biasType;
056                    setDescription("weight = weight + eta * sampleweight * inputdeviation * input");
057                    Variable eta = VariableFactory.singleValue("eta", 1);
058                    Matrix m = MatrixFactory.linkToValue(0.001);
059                    eta.addMatrix(m);
060                    setVariable(ETA, eta);
061    
062                    Variable sampleWeight = VariableFactory.singleValue("Sample Weight", 1);
063                    Matrix s = MatrixFactory.linkToValue(1);
064                    sampleWeight.addMatrix(s);
065                    setVariable(SAMPLEWEIGHT, sampleWeight);
066            }
067    
068            public void setLearningRate(double v) {
069                    Matrix m = MatrixFactory.linkToValue(v);
070                    setMatrix(ETA, m);
071            }
072    
073            public double getLearningRate() {
074                    return getMatrix(ETA).doubleValue();
075            }
076    
077            public void setSampleWeight(double v) {
078                    Matrix m = MatrixFactory.linkToValue(v);
079                    setMatrix(SAMPLEWEIGHT, m);
080            }
081    
082            @Override
083            public Map<Object, Object> calculateObjects(Map<Object, Object> matrices)
084                            throws MatrixException {
085                    Map<Object, Object> result = new HashMap<Object, Object>();
086    
087                    Matrix weight = MathUtil.getMatrix(matrices.get(WEIGHT));
088                    double eta = MathUtil.getMatrix(matrices.get(ETA)).doubleValue();
089                    Matrix contactDeviation = MathUtil.getMatrix(matrices.get(CONTACTDEVIATION));
090                    double sampleWeight = MathUtil.getMatrix(matrices.get(SAMPLEWEIGHT)).doubleValue();
091    
092                    Matrix transposedInput = MathUtil.getMatrix(matrices.get(INPUT)).toColumnVector();
093    
094                    switch (biasType) {
095                    case SINGLE:
096                            transposedInput = transposedInput.addColumnWithOnes();
097                            break;
098                    case MULTIPLE:
099                            Matrix bias = MatrixFactory.ones(transposedInput.getSize());
100                            for (long[] c : transposedInput.allCoordinates()) {
101                                    if (MathUtil.isNaNOrInfinite(transposedInput.getAsDouble(c))) {
102                                            bias.setAsDouble(Double.NaN, c);
103                                    }
104                            }
105                            transposedInput = MatrixFactory.horCat(transposedInput, bias);
106                            break;
107                    case NONE:
108                            break;
109                    }
110    
111                    double totalValueCount = transposedInput.getValueCount();
112                    double missingValueCount = transposedInput.countMissing(Ret.NEW, ALL).doubleValue();
113    
114                    double boost = 1.0;
115    
116                    // if (useBoost) {
117                    // boost = 1.0 / (totalValueCount - missingValueCount);
118                    boost = totalValueCount / (totalValueCount - missingValueCount);
119                    // }
120    
121                    Matrix product = contactDeviation.mtimes(transposedInput);
122                    Matrix weightChange = product.times(eta * sampleWeight * boost);
123                    Matrix newWeight = weight.minus(Ret.NEW, true, weightChange);
124    
125                    result.put(TARGET, newWeight);
126    
127                    return result;
128            }
129    
130            public void setWeightVariable(Variable v) {
131                    setVariable(WEIGHT, v);
132                    setVariable(TARGET, v);
133            }
134    
135            public void setInputVariable(Variable v) {
136                    setVariable(INPUT, v);
137            }
138    
139            public void setContactDeviationVariable(Variable v) {
140                    setVariable(CONTACTDEVIATION, v);
141            }
142    
143            public void setLayer(int nr) {
144                    setLabel("Weight Update (" + nr + ")");
145            }
146    }