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    import java.util.logging.Level;
029    
030    import org.jdmp.core.algorithm.AbstractAlgorithm;
031    import org.jdmp.core.algorithm.Algorithm;
032    import org.jdmp.core.algorithm.AlgorithmTwoSources;
033    import org.jdmp.core.algorithm.basic.Clone;
034    import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.BiasType;
035    import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.Transfer;
036    import org.jdmp.core.variable.Variable;
037    import org.jdmp.core.variable.VariableFactory;
038    import org.ujmp.core.Matrix;
039    import org.ujmp.core.MatrixFactory;
040    
041    public class NetworkLayerBackward extends AbstractAlgorithm {
042            private static final long serialVersionUID = -8051094919417324985L;
043    
044            public static final String WEIGHT = "Weight";
045    
046            public static final String OUTPUT = "Output";
047    
048            public static final String OUTPUTDEVIATION = "OutputDeviation";
049    
050            public static final String RETURNINGFUNCTION = "ReturningFunction";
051    
052            public static final String NETDEVIATION = "NetDeviation";
053    
054            public static final String SPLITTINGFUNCTION = "SplittingFunction";
055    
056            public static final String CONTACTDEVIATION = "ContactDeviation";
057    
058            public static final String DIMMINGFUNCTION = "DimmingFunction";
059    
060            public static final String INPUTDEVIATION = "InputDeviation";
061    
062            public NetworkLayerBackward(Transfer transferFunction, BiasType biasType) {
063                    setDescription("One layer of a multi-layer network, backward path");
064                    setVariable(NETDEVIATION, VariableFactory.labeledVariable("Net Deviation"));
065                    setVariable(CONTACTDEVIATION, VariableFactory.labeledVariable("Contact Deviation"));
066                    setVariable(INPUTDEVIATION, VariableFactory.labeledVariable("Input Deviation"));
067    
068                    AlgorithmTwoSources ar = null;
069                    switch (transferFunction) {
070                    case TANH:
071                            ar = new ReturningFunctionTanh();
072                            break;
073                    case TANHPLUSONE:
074                            ar = new ReturningFunctionTanh();
075                            break;
076                    case LINEAR:
077                            ar = new ReturningFunctionOne();
078                            break;
079                    default:
080                            logger.log(Level.WARNING, "not implemented: " + transferFunction);
081                    }
082    
083                    ar.setVariable(ReturningFunctionTanh.TARGET, getNetDeviationVariable());
084                    setAlgorithm(RETURNINGFUNCTION, ar);
085    
086                    Clone as = new Clone();
087                    as.setVariable(Clone.SOURCE, getNetDeviationVariable());
088                    as.setVariable(Clone.TARGET, getContactDeviationVariable());
089                    setAlgorithm(SPLITTINGFUNCTION, as);
090    
091                    DimmingFunction ad = new DimmingFunction(biasType);
092                    ad.setVariable(DimmingFunction.SOURCE2, getContactDeviationVariable());
093                    ad.setVariable(DimmingFunction.TARGET, getInputDeviationVariable());
094                    setAlgorithm(DIMMINGFUNCTION, ad);
095    
096            }
097    
098            public static void main(String[] args) throws Exception {
099                    NetworkLayerBackward a = new NetworkLayerBackward(Transfer.TANH, BiasType.SINGLE);
100    
101                    Variable outputDeviation = VariableFactory.labeledVariable("Output Deviation");
102                    Matrix d = MatrixFactory.linkToArray(new double[][] { { 1 }, { 2 }, { 3 }, { 4 }, { 5 },
103                                    { 6 } });
104                    outputDeviation.addMatrix(d);
105                    a.setOutputDeviationVariable(outputDeviation);
106    
107                    Variable output = VariableFactory.labeledVariable("Output");
108                    Matrix o = MatrixFactory.linkToArray(new double[][] { { 5 }, { 6 }, { 7 }, { 8 }, { 9 },
109                                    { 10 } });
110                    output.addMatrix(o);
111                    a.setOutputVariable(output);
112    
113                    Variable weight = VariableFactory.labeledVariable("Weight");
114                    Matrix w = MatrixFactory.linkToArray(new double[][] { { 0.1, 0.2, 0.3, 0.4 },
115                                    { 0.1, 0.2, 0.3, 0.4 }, { 0.1, 0.2, 0.3, 0.4 }, { 0.1, 0.2, 0.3, 0.4 },
116                                    { 0.1, 0.2, 0.3, 0.4 }, { 0.1, 0.2, 0.3, 0.4 } });
117                    weight.addMatrix(w);
118                    a.setWeightVariable(weight);
119                    System.out.println(a.calculate());
120            }
121    
122            @Override
123            public Map<Object, Object> calculateObjects(Map<Object, Object> input) throws Exception {
124                    Map<Object, Object> result = new HashMap<Object, Object>();
125    
126                    Algorithm returningFunction = getReturningFunction();
127                    returningFunction.calculate();
128    
129                    Algorithm splittingFunction = getSplittingFunction();
130                    splittingFunction.calculate();
131    
132                    Algorithm dimmingFunction = getDimmingFunction();
133                    dimmingFunction.calculate();
134    
135                    return result;
136            }
137    
138            public Variable getOutputDeviationVariable() {
139                    Variable v = getVariables().get(OUTPUTDEVIATION);
140                    return v;
141            }
142    
143            public void setOutputDeviationVariable(Variable v) {
144                    setVariable(OUTPUTDEVIATION, v);
145                    getReturningFunction().setVariable(AlgorithmTwoSources.SOURCE2, v);
146            }
147    
148            public Matrix getOutputDeviationMatrix() {
149                    return getMatrix(OUTPUTDEVIATION);
150            }
151    
152            public Algorithm getReturningFunction() {
153                    Algorithm a = getAlgorithms().get(RETURNINGFUNCTION);
154                    return a;
155            }
156    
157            public void setReturningFunction(Algorithm a) {
158                    setAlgorithm(RETURNINGFUNCTION, a);
159            }
160    
161            public Algorithm getSplittingFunction() {
162                    Algorithm a = getAlgorithms().get(SPLITTINGFUNCTION);
163                    return a;
164            }
165    
166            public void setSplittingFunction(Algorithm a) {
167                    setAlgorithm(SPLITTINGFUNCTION, a);
168            }
169    
170            public Algorithm getDimmingFunction() {
171                    Algorithm a = getAlgorithms().get(DIMMINGFUNCTION);
172                    return a;
173            }
174    
175            public void setDimmingFunction(Algorithm a) {
176                    setAlgorithm(DIMMINGFUNCTION, a);
177            }
178    
179            public Variable getNetDeviationVariable() {
180                    Variable v = getVariables().get(NETDEVIATION);
181                    return v;
182            }
183    
184            public void setNetDeviationVariable(Variable v) {
185                    setVariable(NETDEVIATION, v);
186            }
187    
188            public Matrix getNetDeviationMatrix() {
189                    return getMatrix(NETDEVIATION);
190            }
191    
192            public Variable getContactDeviationVariable() {
193                    Variable v = getVariables().get(CONTACTDEVIATION);
194                    return v;
195            }
196    
197            public void setContactDeviationVariable(Variable v) {
198                    setVariable(CONTACTDEVIATION, v);
199            }
200    
201            public Matrix getContactDeviationMatrix() {
202                    return getMatrix(CONTACTDEVIATION);
203            }
204    
205            public Variable getInputDeviationVariable() {
206                    Variable v = getVariables().get(INPUTDEVIATION);
207                    return v;
208            }
209    
210            public void setInputDeviationVariable(Variable v) {
211                    setVariable(INPUTDEVIATION, v);
212            }
213    
214            public Matrix getInputDeviationMatrix() {
215                    return getMatrix(INPUTDEVIATION);
216            }
217    
218            public Variable getWeightVariable() {
219                    Variable v = getVariables().get(WEIGHT);
220                    return v;
221            }
222    
223            public void setWeightVariable(Variable v) {
224                    setVariable(WEIGHT, v);
225                    getDimmingFunction().setVariable(AlgorithmTwoSources.SOURCE1, v);
226            }
227    
228            public Matrix getWeightMatrix() {
229                    return getMatrix(WEIGHT);
230            }
231    
232            public Variable getOutputVariable() {
233                    Variable v = getVariables().get(OUTPUT);
234                    return v;
235            }
236    
237            public void setOutputVariable(Variable v) {
238                    setVariable(OUTPUT, v);
239                    getReturningFunction().setVariable(AlgorithmTwoSources.SOURCE1, v);
240            }
241    
242            public Matrix getOutputMatrix() {
243                    return getMatrix(OUTPUT);
244            }
245    
246            public void setLayer(int nr) {
247                    setLabel("Network Layer Backward (" + nr + ")");
248            }
249    }