My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package hef.neuralnet.bitmap.v1;



public class NeuralNet
{
public static void main(String[] args) {
int[][] input;
int[][] output;
NeuralNet net = new NeuralNet(10, 2, 2);


// teach 1
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 2, 2, 18, 18);
output = BitmapUtil.create(10);
BitmapUtil.drawSquare(output, 1, 1, 1, 9, 9);
net.teach(input, output);
System.out.println("\nTEACH 1");
BitmapUtil.print("Input: ", input, "Output: ", output);

// teach 2
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 6, 6, 14, 14);
output = BitmapUtil.create(10);
BitmapUtil.drawSquare(output, 1, 3, 3, 7, 7);
net.teach(input, output);
System.out.println("\nTEACH 2");
BitmapUtil.print("Input: ", input, "Output: ", output);

// teach 3
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 2, 2, 10, 10);
output = BitmapUtil.create(10);
BitmapUtil.drawSquare(output, 1, 1, 1, 5, 5);
net.teach(input, output);
System.out.println("\nTEACH 3");
BitmapUtil.print("Input: ", input, "Output: ", output);

// teach 4
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 10, 10, 16, 16);
output = BitmapUtil.create(10);
BitmapUtil.drawSquare(output, 1, 5, 5, 8, 8);
net.teach(input, output);
System.out.println("\nTEACH 4");
BitmapUtil.print("Input: ", input, "Output: ", output);


// use 1 -- 1 square
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 2, 10, 10, 18);
output = net.use(input);
System.out.println("\nUSE 1 (1 square)");
BitmapUtil.print("Input: ", input, "Output: ", output);

// use 2 -- 1 square
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 6, 8, 10, 12);
output = net.use(input);
System.out.println("\nUSE 2 (1 square)");
BitmapUtil.print("Input: ", input, "Output: ", output);

// use 3 -- 2 squares
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 4, 6, 8, 10);
BitmapUtil.drawSquare(input, 1, 12, 10, 16, 16);
output = net.use(input);
System.out.println("\nUSE 3 (2 squares)");
BitmapUtil.print("Input: ", input, "Output: ", output);

// use 4 -- 3 squares w/ 2 adjacent
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 2, 16, 4, 18);
BitmapUtil.drawSquare(input, 1, 16, 2, 18, 18);
BitmapUtil.drawSquare(input, 1, 6, 4, 16, 14);
output = net.use(input);
System.out.println("\nUSE 4 (3 squares w/ 2 adjacent)");
BitmapUtil.print("Input: ", input, "Output: ", output);

// use 5 -- 2 squares nested in 1 square
input = BitmapUtil.create(20);
BitmapUtil.drawSquare(input, 1, 2, 2, 18, 18);
BitmapUtil.drawSquare(input, 0, 4, 4, 8, 8);
BitmapUtil.drawSquare(input, 0, 12, 6, 16, 16);
output = net.use(input);
System.out.println("\nUSE 5 (2 squares nested in 1 square)");
BitmapUtil.print("Input: ", input, "Output: ", output);
}

private int outputLength;
private int inputLength;
private int inputSampleLength;
private int inputOutputLengthRatio;
private Neuron[][] neurons;

/**
* Constructs a neural net
*
* @param outputLength The length of the sides of the output bitmap
* @param inputOutputLengthRatio The ratio of length of the sides of the input bitmap to the length of the sides of
* the output bitmap (multiplying this number by outputLength yields the expected length of the sides of the
* input bitmap)
* @param inputSampleLength The length of the sides of the bitmap when sampling the input (squaring this number
* yields the input count for each neuron)
*/
public NeuralNet(int outputLength, int inputOutputLengthRatio, int inputSampleLength) {
this.outputLength = outputLength;
this.inputLength = outputLength * inputOutputLengthRatio;
this.inputSampleLength = inputSampleLength;
this.inputOutputLengthRatio = inputOutputLengthRatio;

assert this.inputSampleLength <= this.inputLength;

this.neurons = new Neuron[outputLength][];
for (int i = 0; i < outputLength; i += 1) {
this.neurons[i] = new Neuron[outputLength];
for (int j = 0; j < outputLength; j += 1) {
this.neurons[i][j] = new Neuron(inputOutputLengthRatio * inputOutputLengthRatio);
}
}
}

public void teach(int[][] input, int[][] output) {
assert input.length == this.inputLength;
assert input[0].length == this.inputLength;
assert output.length == this.outputLength;
assert output[0].length == this.outputLength;

for (int i = 0; i < this.outputLength; i += 1) {
for (int j = 0; j < this.outputLength; j += 1) {
int[][] sample = this.extractInputSample(input, i, j);
this.neurons[i][j].teach(flatten(sample), output[i][j]);
}
}
}

public int[][] use(int[][] input) {
assert input.length == this.inputLength;
assert input[0].length == this.inputLength;

int[][] output = new int[this.outputLength][];

for (int i = 0; i < this.outputLength; i += 1) {
output[i] = new int[this.outputLength];
for (int j = 0; j < this.outputLength; j += 1) {
int[][] sample = this.extractInputSample(input, i, j);
output[i][j] = this.neurons[i][j].use(flatten(sample));
}
}

return output;
}

private int[][] extractInputSample(int[][] input, int i, int j) {
int[][] subInput = new int[this.inputSampleLength][];

int xOffset = i * this.inputSampleLength;
if (i > 0) {
xOffset -= (this.inputSampleLength - this.inputOutputLengthRatio) / 2;
xOffset -= (this.inputSampleLength - this.inputOutputLengthRatio) % 2;
}
if (xOffset + this.inputSampleLength > this.inputLength) xOffset = this.inputLength - this.inputSampleLength;

int yOffset = j * this.inputSampleLength;
if (j > 0) {
yOffset -= (this.inputSampleLength - this.inputOutputLengthRatio) / 2;
yOffset -= (this.inputSampleLength - this.inputOutputLengthRatio) % 2;
}
if (yOffset + this.inputSampleLength > this.inputLength) yOffset = this.inputLength - this.inputSampleLength;

for (int x = 0; x < this.inputSampleLength; x += 1) {
subInput[x] = new int[this.inputSampleLength];
for (int y = 0; y < this.inputSampleLength; y += 1) {
subInput[x][y] = input[x + xOffset][y + yOffset];
}
}

return subInput;
}

private static int[] flatten(int[][] input) {
assert input.length > 0;

int[] flattened = new int[input.length * input[0].length];

for (int i = 0; i < input.length; i += 1) {
for (int j = 0; j < input.length; j += 1) {
flattened[(i * input.length) + j] = input[i][j];
}
}

return flattened;
}
}

Change log

r10 by jonathan.hefner on Jul 4, 2008   Diff
sync
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 7631 bytes, 195 lines
Powered by Google Project Hosting