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.AbstractAlgorithm;
030 import org.jdmp.core.algorithm.Algorithm;
031 import org.jdmp.core.algorithm.AlgorithmTwoSources;
032 import org.jdmp.core.algorithm.basic.Clone;
033 import org.jdmp.core.algorithm.basic.LogisticFunction;
034 import org.jdmp.core.algorithm.basic.Mean;
035 import org.jdmp.core.algorithm.basic.Sum;
036 import org.jdmp.core.algorithm.basic.Tanh;
037 import org.jdmp.core.algorithm.basic.TanhPlusOne;
038 import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.Aggregation;
039 import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.BiasType;
040 import org.jdmp.core.algorithm.classification.mlp.MultiLayerNetwork.Transfer;
041 import org.jdmp.core.variable.Variable;
042 import org.jdmp.core.variable.VariableFactory;
043 import org.ujmp.core.Matrix;
044
045 public class NetworkLayerForward extends AbstractAlgorithm {
046 private static final long serialVersionUID = -2738909213636005084L;
047
048 public static final String INPUT = "Input";
049
050 public static final String WEIGHT = "Weight";
051
052 public static final String WEIGHTINGFUNCTION = "WeightingFunction";
053
054 public static final String WEIGHTEDINPUT = "WeightedInput";
055
056 public static final String AGGREGATIONFUNCTION = "AggregationFunction";
057
058 public static final String NETINPUT = "NetInput";
059
060 public static final String TRANSFERFUNCTION = "TransferFunction";
061
062 public static final String OUTPUT = "Output";
063
064 public NetworkLayerForward(BiasType biasType) {
065 this(Aggregation.MEAN, Transfer.TANH, biasType);
066 }
067
068 public NetworkLayerForward(Aggregation aggregationFunction, Transfer transferFunction,
069 BiasType biasType) {
070 setDescription("One layer of a multi-layer network, forward path");
071
072 setVariable(WEIGHTEDINPUT, VariableFactory.labeledVariable("Weighted Input"));
073 setVariable(NETINPUT, VariableFactory.labeledVariable("Net Input"));
074
075 Weighting aw = new Weighting(biasType);
076 aw.setVariable(Weighting.TARGET, getWeightedInputVariable());
077 setAlgorithm(WEIGHTINGFUNCTION, aw);
078
079 setAggregationFunction(aggregationFunction);
080 setTransferFunction(transferFunction);
081 }
082
083 @Override
084 public Map<Object, Object> calculateObjects(Map<Object, Object> input) throws Exception {
085 Map<Object, Object> result = new HashMap<Object, Object>();
086
087 Algorithm weightingFunction = getWeightingFunction();
088 weightingFunction.calculate();
089
090 Algorithm aggregationFunction = getAggregationFunction();
091 aggregationFunction.calculate();
092
093 Algorithm transferFunction = getTransferFunction();
094 transferFunction.calculate();
095
096 return result;
097 }
098
099 public Matrix getInputMatrix() {
100 return getMatrix(INPUT);
101 }
102
103 public Variable getInputVariable() {
104 Variable v = getVariables().get(INPUT);
105 return v;
106 }
107
108 public void setInputVariable(Variable v) {
109 setVariable(INPUT, v);
110 getWeightingFunction().setVariable(AlgorithmTwoSources.SOURCE2, v);
111 }
112
113 public Matrix getWeightMatrix() {
114 return getMatrix(WEIGHT);
115 }
116
117 public Variable getWeightVariable() {
118 Variable v = getVariables().get(WEIGHT);
119 return v;
120 }
121
122 public void setWeightVariable(Variable v) {
123 setVariable(WEIGHT, v);
124 getWeightingFunction().setVariable(AlgorithmTwoSources.SOURCE1, v);
125 }
126
127 public Matrix getWeightedInputMatrix() {
128 return getMatrix(WEIGHTEDINPUT);
129 }
130
131 public Variable getWeightedInputVariable() {
132 Variable v = getVariables().get(WEIGHTEDINPUT);
133 return v;
134 }
135
136 public void setAggregationFunction(Aggregation aggregationFunction) {
137 Algorithm a = null;
138 switch (aggregationFunction) {
139 case MEAN:
140 a = new Mean(Matrix.COLUMN);
141 break;
142 case SUM:
143 a = new Sum(Matrix.COLUMN);
144 }
145 a.setVariable(SOURCE, getWeightedInputVariable());
146 a.setVariable(TARGET, getNetInputVariable());
147 setAlgorithm(AGGREGATIONFUNCTION, a);
148 }
149
150 public void setTransferFunction(Transfer transferFunction) {
151 Algorithm a = null;
152 switch (transferFunction) {
153 case TANH:
154 a = new Tanh();
155 break;
156 case LINEAR:
157 a = new Clone();
158 break;
159 case TANHPLUSONE:
160 a = new TanhPlusOne();
161 break;
162 case SIGMOID:
163 a = new LogisticFunction();
164 break;
165 }
166 a.setVariable(SOURCE, getNetInputVariable());
167 setAlgorithm(TRANSFERFUNCTION, a);
168 }
169
170 public void setWeightedInputVariable(Variable v) {
171 setVariable(WEIGHTEDINPUT, v);
172 }
173
174 public Algorithm getWeightingFunction() {
175 Algorithm a = getAlgorithms().get(WEIGHTINGFUNCTION);
176 return a;
177 }
178
179 public Algorithm getAggregationFunction() {
180 Algorithm a = getAlgorithms().get(AGGREGATIONFUNCTION);
181 return a;
182 }
183
184 public Algorithm getTransferFunction() {
185 Algorithm a = getAlgorithms().get(TRANSFERFUNCTION);
186 return a;
187 }
188
189 public Variable getNetInputVariable() {
190 Variable v = getVariables().get(NETINPUT);
191 return v;
192 }
193
194 public Matrix getNetInputMatrix() {
195 return getMatrix(NETINPUT);
196 }
197
198 public void setNetInputVariable(Variable v) {
199 setVariable(NETINPUT, v);
200 }
201
202 public Variable getOutputVariable() {
203 Variable v = getVariables().get(OUTPUT);
204 return v;
205 }
206
207 public Matrix getOutputMatrix() {
208 return getMatrix(OUTPUT);
209 }
210
211 public void setOutputVariable(Variable v) {
212 setVariable(OUTPUT, v);
213 getAlgorithms().get(TRANSFERFUNCTION).setVariable(TARGET, v);
214 }
215
216 // public static void main(String[] args) throws Exception {
217 // AlgorithmNetworkLayerForward a = new
218 // AlgorithmNetworkLayerForward(Aggregation.MEAN, Transfer.TANH);
219 // Variable input = new DefaultVariable("Input");
220 // Matrix x = Matrix.fromArray(new double[][] { { 1 }, { 2 }, { 3 }, { 4 }
221 // });
222 // input.addMatrix(x);
223 // a.setInputVariable(input);
224 // Variable weight = new DefaultVariable("Weight");
225 // Matrix w = Matrix.fromArray(new double[][] { { 0.1, 0.2, 0.3, 0.4 }, {
226 // 0.1, 0.2, 0.3, 0.4 },
227 // { 0.1, 0.2, 0.3, 0.4 }, { 0.1, 0.2, 0.3, 0.4 }, { 0.1, 0.2, 0.3, 0.4 }, {
228 // 0.1, 0.2, 0.3, 0.4 } });
229 // weight.addMatrix(w);
230 // a.setWeightVariable(weight);
231 // System.out.println(a.calculate());
232 // }
233
234 public void setLayer(int nr) {
235 setLabel("Network Layer Forward (" + nr + ")");
236 }
237
238 }