source: anac2020/BlingBling/src/main/java/geniusweb/blingbling/Ranknet4j/Ranknet.java

Last change on this file was 1, checked in by wouter, 4 years ago

#1910 added anac2020 parties

File size: 6.3 KB
Line 
1package geniusweb.blingbling.Ranknet4j;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.neuroph.core.Layer;
7import org.neuroph.core.NeuralNetwork;
8import org.neuroph.core.input.WeightedSum;
9import org.neuroph.core.transfer.Linear;
10import org.neuroph.nnet.comp.neuron.BiasNeuron;
11import org.neuroph.nnet.comp.neuron.InputNeuron;
12//import org.neuroph.nnet.learning.BackPropagation;
13//import org.neuroph.nnet.learning.MomentumBackpropagation;
14import org.neuroph.util.ConnectionFactory;
15import org.neuroph.util.LayerFactory;
16import org.neuroph.util.NeuralNetworkFactory;
17import org.neuroph.util.NeuralNetworkType;
18import org.neuroph.util.NeuronProperties;
19import org.neuroph.util.TransferFunctionType;
20import org.neuroph.util.random.RangeRandomizer;
21
22/**
23 * Multi Layer Perceptron neural network with Back propagation learning algorithm.
24 *
25 * @author Zoran Sevarac <sevarac@gmail.com>
26 */
27public class Ranknet extends NeuralNetwork<BackPropagation> {
28
29 /**
30 * The class fingerprint that is set to indicate serialization
31 * compatibility with a previous version of the class.
32 */
33 private static final long serialVersionUID = 2L;
34
35 /**
36 * Creates new MultiLayerPerceptron with specified number of neurons in layers
37 *
38 * @param neuronsInLayers collection of neuron number in layers
39 */
40 public Ranknet(List<Integer> neuronsInLayers) {
41 // init neuron settings
42 NeuronProperties neuronProperties = new NeuronProperties();
43 neuronProperties.setProperty("useBias", true);
44 neuronProperties.setProperty("transferFunction", TransferFunctionType.SIGMOID);
45
46 this.createNetwork(neuronsInLayers, neuronProperties);
47 }
48
49 public Ranknet(int... neuronsInLayers) {
50 // init neuron settings
51 NeuronProperties neuronProperties = new NeuronProperties();
52 neuronProperties.setProperty("useBias", true);
53 neuronProperties.setProperty("transferFunction", TransferFunctionType.SIGMOID);
54 neuronProperties.setProperty("inputFunction", WeightedSum.class);
55
56 List<Integer> neuronsInLayersVector = new ArrayList<>();
57 for (int i = 0; i < neuronsInLayers.length; i++) {
58 neuronsInLayersVector.add(Integer.valueOf(neuronsInLayers[i]));
59 }
60
61 this.createNetwork(neuronsInLayersVector, neuronProperties);
62 }
63
64 public Ranknet(TransferFunctionType transferFunctionType, int... neuronsInLayers) {
65 // init neuron settings
66 NeuronProperties neuronProperties = new NeuronProperties();
67 neuronProperties.setProperty("useBias", true);
68 neuronProperties.setProperty("transferFunction", transferFunctionType);
69 neuronProperties.setProperty("inputFunction", WeightedSum.class);
70
71
72 List<Integer> neuronsInLayersVector = new ArrayList<>();
73 for (int i = 0; i < neuronsInLayers.length; i++) {
74 neuronsInLayersVector.add(Integer.valueOf(neuronsInLayers[i]));
75 }
76
77 this.createNetwork(neuronsInLayersVector, neuronProperties);
78 }
79
80 public Ranknet(List<Integer> neuronsInLayers, TransferFunctionType transferFunctionType) {
81 // init neuron settings
82 NeuronProperties neuronProperties = new NeuronProperties();
83 neuronProperties.setProperty("useBias", true);
84 neuronProperties.setProperty("transferFunction", transferFunctionType);
85
86 this.createNetwork(neuronsInLayers, neuronProperties);
87 }
88
89 /**
90 * Creates new MultiLayerPerceptron net with specified number neurons in
91 * getLayersIterator
92 *
93 * @param neuronsInLayers collection of neuron numbers in layers
94 * @param neuronProperties neuron properties
95 */
96 public Ranknet(List<Integer> neuronsInLayers, NeuronProperties neuronProperties) {
97 this.createNetwork(neuronsInLayers, neuronProperties);
98 }
99
100 /**
101 * Creates MultiLayerPerceptron Network architecture - fully connected
102 * feed forward with specified number of neurons in each layer
103 *
104 * @param neuronsInLayers collection of neuron numbers in getLayersIterator
105 * @param neuronProperties neuron properties
106 */
107 private void createNetwork(List<Integer> neuronsInLayers, NeuronProperties neuronProperties) {
108
109 // set network type
110 this.setNetworkType(NeuralNetworkType.MULTI_LAYER_PERCEPTRON);
111
112 // create input layer
113 NeuronProperties inputNeuronProperties = new NeuronProperties(InputNeuron.class, Linear.class);
114 Layer layer = LayerFactory.createLayer(neuronsInLayers.get(0), inputNeuronProperties);
115
116 boolean useBias = true; // use bias neurons by default
117 if (neuronProperties.hasProperty("useBias")) {
118 useBias = (Boolean) neuronProperties.getProperty("useBias");
119 }
120
121 if (useBias) {
122 layer.addNeuron(new BiasNeuron());
123 }
124
125 this.addLayer(layer);
126
127 // create layers
128 Layer prevLayer = layer;
129
130 //for(Integer neuronsNum : neuronsInLayers)
131 for (int layerIdx = 1; layerIdx < neuronsInLayers.size(); layerIdx++) {
132 Integer neuronsNum = neuronsInLayers.get(layerIdx);
133 // createLayer layer
134 layer = LayerFactory.createLayer(neuronsNum, neuronProperties);
135
136 if (useBias && (layerIdx < (neuronsInLayers.size() - 1))) {
137 layer.addNeuron(new BiasNeuron());
138 }
139
140 // add created layer to network
141 this.addLayer(layer);
142 // createLayer full connectivity between previous and this layer
143 if (prevLayer != null) {
144 ConnectionFactory.fullConnect(prevLayer, layer);
145 }
146
147 prevLayer = layer;
148 }
149
150 // set input and output cells for network
151 NeuralNetworkFactory.setDefaultIO(this);
152
153 // set learnng rule
154// this.setLearningRule(new BackPropagation());
155 BackPropagation bp = new BackPropagation();
156 bp.setMaxIterations(4000);
157 bp.setLearningRate(0.003);
158// bp.setMaxError(0.1);
159// bp.setBatchMode(true);
160 this.setLearningRule(bp);
161 // this.setLearningRule(new DynamicBackPropagation());
162
163 this.randomizeWeights(new RangeRandomizer(-1.0, 1.0));
164
165 }
166
167 public void connectInputsToOutputs() {
168 // connect first and last layer
169 ConnectionFactory.fullConnect(getLayerAt(0), getLayerAt(getLayersCount() - 1), false);
170 }
171
172}
Note: See TracBrowser for help on using the repository browser.