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.io.Serializable;
027    
028    import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.Aggregation;
029    import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.BiasType;
030    import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.Transfer;
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.exceptions.MatrixException;
036    
037    public class NetworkLayer implements Serializable {
038            private static final long serialVersionUID = 3933537008613985003L;
039    
040            private NetworkLayer previousLayer = null;
041    
042            private NetworkLayer nextLayer = null;
043    
044            private NetworkLayerForward algorithmForward = null;
045    
046            private NetworkLayerBackward algorithmBackward = null;
047    
048            private WeightUpdate algorithmWeightUpdate = null;
049    
050            private BiasType biasType = null;
051    
052            private Aggregation aggregation = null;
053    
054            public NetworkLayer(Aggregation aggregationFunction, Transfer transferFunction,
055                            BiasType biasType) {
056                    algorithmForward = new NetworkLayerForward(aggregationFunction, transferFunction, biasType);
057                    algorithmBackward = new NetworkLayerBackward(transferFunction, biasType);
058                    algorithmWeightUpdate = new WeightUpdate(biasType);
059                    algorithmWeightUpdate.setContactDeviationVariable(algorithmBackward
060                                    .getContactDeviationVariable());
061                    this.biasType = biasType;
062                    this.aggregation = aggregationFunction;
063            }
064    
065            public NetworkLayer(Aggregation aggregationFunction, Transfer transferFunction,
066                            BiasType biasType, int neuronCount) {
067                    this(aggregationFunction, transferFunction, biasType);
068                    Variable outputDeviation = VariableFactory.labeledVariable("Output Deviation");
069                    outputDeviation.setSize(neuronCount, 1);
070                    setOutputDeviationVariable(outputDeviation);
071                    Variable output = VariableFactory.labeledVariable("Output");
072                    output.setSize(neuronCount, 1);
073                    setOutputVariable(output);
074            }
075    
076            public void setLayer(int nr) {
077                    algorithmBackward.setLayer(nr);
078                    algorithmForward.setLayer(nr);
079                    algorithmWeightUpdate.setLayer(nr);
080            }
081    
082            public NetworkLayerBackward getAlgorithmBackward() {
083                    return algorithmBackward;
084            }
085    
086            public void setLearningRate(double v) {
087                    getAlgorithmWeightUpdate().setLearningRate(v);
088            }
089    
090            public double getLearningRate() {
091                    return getAlgorithmWeightUpdate().getLearningRate();
092            }
093    
094            public void setSampleWeight(double v) {
095                    getAlgorithmWeightUpdate().setSampleWeight(v);
096            }
097    
098            public WeightUpdate getAlgorithmWeightUpdate() {
099                    return algorithmWeightUpdate;
100            }
101    
102            public void setAlgorithmBackward(NetworkLayerBackward algorithmBackward) {
103                    this.algorithmBackward = algorithmBackward;
104            }
105    
106            public NetworkLayerForward getAlgorithmForward() {
107                    return algorithmForward;
108            }
109    
110            public void setAlgorithmForward(NetworkLayerForward algorithmForward) {
111                    this.algorithmForward = algorithmForward;
112            }
113    
114            public NetworkLayer getNextLayer() {
115                    return nextLayer;
116            }
117    
118            public void setNextLayer(NetworkLayer nextLayer) {
119                    this.nextLayer = nextLayer;
120    
121                    nextLayer.setInputVariable(getOutputVariable());
122                    setOutputDeviationVariable(nextLayer.getInputDeviationVariable());
123    
124            }
125    
126            public NetworkLayer getPreviousLayer() {
127                    return previousLayer;
128            }
129    
130            public void setPreviousLayer(NetworkLayer previousLayer) {
131                    this.previousLayer = previousLayer;
132    
133                    if (previousLayer != null) {
134                            setInputVariable(previousLayer.getOutputVariable());
135                            previousLayer.setOutputDeviationVariable(getInputDeviationVariable());
136                    }
137            }
138    
139            public Variable getInputDeviationVariable() {
140                    return algorithmBackward.getInputDeviationVariable();
141            }
142    
143            public void setWeightVariable(Variable weight) {
144                    algorithmForward.setWeightVariable(weight);
145                    algorithmBackward.setWeightVariable(weight);
146                    algorithmWeightUpdate.setWeightVariable(weight);
147            }
148    
149            public void calculateForward() throws Exception {
150                    createVariablesIfNecessary();
151                    algorithmForward.calculate();
152            }
153    
154            public void calculateBackward() throws Exception {
155                    createVariablesIfNecessary();
156                    algorithmBackward.calculate();
157            }
158    
159            public void calculateWeightUpdate() throws Exception {
160                    algorithmWeightUpdate.calculate();
161            }
162    
163            public Variable getWeightVariable() {
164                    return algorithmForward.getWeightVariable();
165            }
166    
167            public void createVariablesIfNecessary() throws MatrixException {
168                    if (getWeightVariable() == null) {
169                            Variable weight = VariableFactory.labeledVariable("Weight");
170                            Matrix w = null;
171    
172                            double scale = 1;
173                            if (aggregation == Aggregation.SUM) {
174                                    scale = 1.0 / getInputVariable().getRowCount()
175                                                    / getInputVariable().getColumnCount();
176                            }
177    
178                            switch (biasType) {
179                            case SINGLE:
180                                    w = MatrixFactory.randn(
181                                                    getOutputVariable().getRowCount() * getOutputVariable().getColumnCount(),
182                                                    getInputVariable().getRowCount() * getInputVariable().getColumnCount() + 1)
183                                                    .times(scale);
184                                    break;
185                            case MULTIPLE:
186                                    w = MatrixFactory.randn(
187                                                    getOutputVariable().getRowCount() * getOutputVariable().getColumnCount(),
188                                                    getInputVariable().getRowCount() * getInputVariable().getColumnCount() * 2)
189                                                    .times(scale);
190                                    break;
191                            case NONE:
192                                    w = MatrixFactory.randn(
193                                                    getOutputVariable().getRowCount() * getOutputVariable().getColumnCount(),
194                                                    getInputVariable().getRowCount() * getInputVariable().getColumnCount())
195                                                    .times(scale);
196                                    break;
197                            }
198    
199                            weight.addMatrix(w);
200                            setWeightVariable(weight);
201                            // System.out.println("weight " + getLabel() + ": " +
202                            // weight.getColumnCount() + "->" + weight.getRowCount());
203                    }
204            }
205    
206            public String getLabel() {
207                    return algorithmForward.getLabel();
208            }
209    
210            public void reset() throws MatrixException {
211                    createVariablesIfNecessary();
212                    Matrix w = MatrixFactory.randn(
213                                    getOutputVariable().getRowCount() * getOutputVariable().getColumnCount(),
214                                    getInputVariable().getRowCount() * getInputVariable().getColumnCount()).times(0.01);
215                    getWeightVariable().addMatrix(w);
216            }
217    
218            @Override
219            public String toString() {
220                    StringBuilder s = new StringBuilder();
221                    if (getPreviousLayer() != null) {
222                            s.append("(" + getPreviousLayer().getNeuronCount() + " -->) ");
223                    }
224                    s.append(getNeuronCount());
225                    if (getNextLayer() != null) {
226                            s.append(" (--> " + getNextLayer().getNeuronCount() + ")");
227                    }
228                    return s.toString();
229            }
230    
231            public int getNeuronCount() {
232                    return (int) getOutputDeviationVariable().getRowCount();
233            }
234    
235            public void setInputVariable(Variable v) {
236                    algorithmForward.setInputVariable(v);
237                    algorithmWeightUpdate.setInputVariable(v);
238            }
239    
240            public void setOutputDeviationVariable(Variable v) {
241                    algorithmBackward.setOutputDeviationVariable(v);
242            }
243    
244            public void setOutputVariable(Variable v) {
245                    algorithmForward.setOutputVariable(v);
246                    algorithmBackward.setOutputVariable(v);
247            }
248    
249            public Variable getInputVariable() {
250                    return algorithmForward.getInputVariable();
251            }
252    
253            public Variable getOutputDeviationVariable() {
254                    return algorithmBackward.getOutputDeviationVariable();
255            }
256    
257            public Variable getOutputVariable() {
258                    return algorithmForward.getOutputVariable();
259            }
260    
261            public void addInputMatrix(Matrix matrix) {
262                    algorithmForward.setMatrix(NetworkLayerForward.INPUT, matrix);
263            }
264    
265    }