/
Layer.java
219 lines (183 loc) · 5.04 KB
/
Layer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.github.neuralnetworks.architecture;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import com.github.neuralnetworks.util.Pair;
import com.github.neuralnetworks.util.UniqueList;
import com.github.neuralnetworks.util.Util;
/**
* A layer of neurons. Each layer contains a set of connections that link it to other layers.
* In that sense every neural network is a graph. This is done for maximum versatility.
* It makes possible the representation of various architectures - committee of machines or parallel networks to be calculated on different GPU devices.
*/
public class Layer implements Serializable
{
private String name = null;
private int[] layerDimension = new int[0];
private static final long serialVersionUID = 1035633207383317489L;
/**
* Set of links to other layers
*/
private List<Connections> connections;
public Layer()
{
this(null);
}
public Layer(String name)
{
super();
this.name=name;
this.connections = new UniqueList<>();
}
/**
* @param network
* @return list of connections within the specific neural network
*/
public List<Connections> getConnections(NeuralNetwork network)
{
return connections.stream().filter(c -> network.getLayers().contains(Util.getOppositeLayer(c, this))).collect(Collectors.toList());
}
public List<Connections> getConnections()
{
return connections;
}
public void setConnections(List<Connections> connections)
{
this.connections = connections;
}
public void addConnection(Connections connection)
{
if (connections == null)
{
connections = new UniqueList<>();
}
connections.add(connection);
}
public int getUnitCount(Collection<Connections> connections)
{
int result = 0;
for (Connections c : connections)
{
if (c.getInputLayer() == this)
{
if (result == 0)
{
result = c.getInputUnitCount();
}
if (result != c.getInputUnitCount())
{
throw new IllegalArgumentException("Some connections require different unit count");
}
} else if (c.getOutputLayer() == this)
{
if (result == 0)
{
result = c.getOutputUnitCount();
}
if (result != c.getOutputUnitCount())
{
throw new IllegalArgumentException("Some connections require different unit count");
}
} else
{
throw new IllegalArgumentException("A connection doesn't have the targetLayer as either input or output");
}
}
return result;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int[] getLayerDimension()
{
return layerDimension;
}
public void setLayerDimension(int[] layerDimension)
{
if (layerDimension == null)
{
throw new IllegalArgumentException("layerDimension must be not null!");
}
this.layerDimension = layerDimension;
}
public int getNeuronCount()
{
int count = 0;
boolean first = true;
for (int d : layerDimension)
{
if (first)
{
count = d;
first = false;
} else
{
count = count * d;
}
}
return count;
}
public Pair<List<Layer>,List<Layer>> getInputAndOutputLayer()
{
List<Layer> listOfInputLayer=new ArrayList<>();
List<Layer> listOfOutputLayer=new ArrayList<>();
for (Connections connection : connections) {
if(connection.getOutputLayer()==this)
{
listOfInputLayer.add(connection.getInputLayer());
}
if(connection.getInputLayer()==this)
{
listOfOutputLayer.add(connection.getOutputLayer());
}
}
return new Pair<List<Layer>,List<Layer>>(listOfInputLayer,listOfOutputLayer);
}
public Pair<List<Connections>,List<Connections>> getInputAndOutputConnection()
{
List<Connections> listOfInputConnections=new ArrayList<>();
List<Connections> listOfOutputConnections=new ArrayList<>();
for (Connections connection : connections) {
if(connection.getOutputLayer()==this)
{
listOfInputConnections.add(connection);
}
if(connection.getInputLayer()==this)
{
listOfOutputConnections.add(connection);
}
}
return new Pair<List<Connections>,List<Connections>>(listOfInputConnections,listOfOutputConnections);
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append(name).append("\n");
if (this.getLayerDimension() != null)
{
builder.append("neurons: ").append(this.getNeuronCount()).append("\n");
}
builder.append("\n");
Pair<List<Layer>, List<Layer>> inputAndOutputLayer = getInputAndOutputLayer();
builder.append("input:\n");
for (Layer layer : inputAndOutputLayer.getLeft())
{
builder.append(layer.getName()).append("\n");
}
builder.append("\n");
builder.append("output:\n");
for (Layer layer : inputAndOutputLayer.getRight())
{
builder.append(layer.getName()).append("\n");
}
return builder.toString();
}
}