-
Notifications
You must be signed in to change notification settings - Fork 1
/
CompositeNeuralNetworks.java
73 lines (60 loc) · 1.6 KB
/
CompositeNeuralNetworks.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
// Composite design pattern - neural networks
package structural.composite;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
interface SomeNeurons extends Iterable<Neuron>
{
default void connectTo(SomeNeurons other)
{
if (this == other) return;
for (Neuron from : this)
for (Neuron to : other)
{
from.out.add(to);
to.in.add(from);
}
}
}
class Neuron implements SomeNeurons
{
public ArrayList<Neuron> in, out;
@Override
public Iterator<Neuron> iterator() {
return Collections.singleton(this).iterator();
}
@Override
public void forEach(Consumer<? super Neuron> action) {
action.accept(this);
}
@Override
public Spliterator<Neuron> spliterator() {
return Collections.singleton(this).spliterator();
}
// Connecting a pair of neurons
// public void connectTo(Neuron other)
// {
// out.add(other);
// other.in.add(this);
// }
}
// ArrayList already implements Iterable<Neuron>
class NeuronLayer extends ArrayList<Neuron>
implements SomeNeurons
{
}
class CompositeNetworksDemo
{
public static void main(String[] args) {
Neuron neuron = new Neuron();
Neuron neuron2 = new Neuron();
NeuronLayer layer = new NeuronLayer();
NeuronLayer layer2 = new NeuronLayer();
neuron.connectTo(neuron2);
neuron.connectTo(layer);
layer.connectTo(neuron);
layer.connectTo(layer2);
}
}