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
package org.shapelogic.machinelearning;

import org.shapelogic.streams.*;
import org.shapelogic.calculation.RecursiveContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.shapelogic.util.Constants;

/** FFNeuralNetworkStream a feed forward neural network wrapped in a stream.<br />
*
* Streams involved: <br />
* <lu>
* <li>ArrayOutputListStream: A list of names of input features that need to be streams</li>
* <li>FFNeuralNetwork: Creates stream of double[] from the neural network</li>
* <li>ConfidenceArrayListStream: Creat result stream of String</li>
* </lu>
*
* Which of these needs names? None of them.<br />
* <br />
* It seems like the last stream will have the same output as the overall stream
* is the overall then really needed? No.<br />
* <br />
* There should probably be a stack of Streams.<br />
* This could possibly be a LISP list, no there is no advantage to this.<br />
* The whole think should be created lazily, but when it gets created, you can
* directly pipe one into the next.<br />
* What should trigger the setup?<br />
* As long as the first have a lazy setup we should be fine and it does.<br />
* So the first should be any type of ListStream, while the next have to be
* ListCalcStream1 or any ListStream with one input.<br />
* <br />
*
* @author Sami Badawi
*/
public class FFNeuralNetworkStream {
public final static String DEFAULT_RESULT_NAME = "Result";
public final static String DEFAULT_OUTPUT_NAME = "FFNeuralNetworkStreamOutput";

ListStream<double[]> _featureStream;
ListCalcStream1<double[], double[]> _neuralNetworkStream;
ListCalcStream1<double[], String> _outputStream;
RecursiveContext _recursiveContext;

/** FFNeuralNetworkStream is not a normal stream, 3 streams are created here.<br />
*
* @param featureList Names of feature input streams
* @param ohList Names of output
* @param weights Weights of neural network
* @param streamNames if this is given then add 3 new streams as named streams to the context
* @param recursiveContext RecursiveContext to look for streams
* @param maxLast Set fixed length of stream
*/
public FFNeuralNetworkStream(List<String> featureList, List<String> ohList,
double[][] weights, String[] streamNames,
RecursiveContext recursiveContext, int maxLast
)
{
_recursiveContext = recursiveContext;
Map context = _recursiveContext.getContext();
if (ohList == null || ohList.size() == 0) {
ohList = new ArrayList<String>();
ohList.add(DEFAULT_RESULT_NAME);
}
if (streamNames == null || 0 == streamNames.length ) {
streamNames = new String[3];
streamNames[2] = DEFAULT_OUTPUT_NAME;
}
if (streamNames.length < 3)
throw new RuntimeException("Too few arguments for streamNames: " + streamNames.length);

// Setup stream 1
_featureStream = new ArrayOutputListStream(featureList,
recursiveContext, streamNames[0], Constants.LAST_UNKNOWN);

// Setup stream 2
FFNeuralNetwork fFNeuralNetwork =
new FFNeuralNetwork(featureList.size(), ohList.size());
for (double[] weight: weights)
fFNeuralNetwork.addLayer(weight);
_neuralNetworkStream = new ListCalcStream1<double[], double[]>(
fFNeuralNetwork, _featureStream);
if (streamNames[1] != null)
context.put(streamNames[1], _neuralNetworkStream);

// Setup stream 3
ConfidenceArraySelector confidenceArraySelector =
new ConfidenceArraySelector(ohList);
_outputStream = new ListCalcStream1<double[], String>(
confidenceArraySelector, _neuralNetworkStream);
if (streamNames[2] != null)
context.put(streamNames[2], _outputStream);
}

public FFNeuralNetworkStream(List<String> featureList, List<String> ohList,
double[][] weights, String[] streamNames,
RecursiveContext recursiveContext)
{
this(featureList, ohList, weights, streamNames, recursiveContext,
Constants.LAST_UNKNOWN);
}

public FFNeuralNetworkStream(List<String> featureList, List<String> ohList,
double[][] weights, RecursiveContext recursiveContext)
{
this(featureList, ohList, weights, null, recursiveContext,
Constants.LAST_UNKNOWN);
}


//Constructors using String[] instead of List<String>

public FFNeuralNetworkStream(String[] featureList, String[] ohList,
double[][] weights, String[] streamNames,
RecursiveContext recursiveContext, int maxLast
)
{
this(Arrays.asList(featureList), Arrays.asList(ohList), weights,
streamNames, recursiveContext, maxLast );
}

public FFNeuralNetworkStream(String[] featureList, String[] ohList,
double[][] weights, String[] streamNames,
RecursiveContext recursiveContext
)
{
this(Arrays.asList(featureList), Arrays.asList(ohList), weights,
streamNames, recursiveContext, Constants.LAST_UNKNOWN );
}

public FFNeuralNetworkStream(String[] featureList, String[] ohList,
double[][] weights, RecursiveContext recursiveContext)
{
this(Arrays.asList(featureList), Arrays.asList(ohList), weights,
null, recursiveContext, Constants.LAST_UNKNOWN );
}

public FFNeuralNetworkStream(FFNeuralNetworkWeights fFNeuralNetworkWeights,
RecursiveContext recursiveContext)
{
this(fFNeuralNetworkWeights.getFeatureList(),
fFNeuralNetworkWeights.getOhList(),
fFNeuralNetworkWeights.getWeights(), null, recursiveContext);
}

public ListStream<double[]> getFeatureStream() {
return _featureStream;
}

public ListCalcStream1<double[], double[]> getNeuralNetworkStream() {
return _neuralNetworkStream;
}

public ListCalcStream1<double[], String> getOutputStream() {
return _outputStream;
}
}

Change log

r1077 by sami.badawi on Apr 30, 2009   Diff
You can now load neural networks from an
external file in the color particle
analyzer.
Go to: 
Project members, sign in to write a code review

Older revisions

r1072 by sami.badawi on Apr 19, 2009   Diff
Better Javadoc.
r1006 by sami.badawi on Jan 4, 2009   Diff
Added constructors with default values
set.
r999 by sami.badawi on Jan 4, 2009   Diff
FFNeuralNetworkStream contains 3
streams but is not a stream.
All revisions of this file

File info

Size: 6208 bytes, 159 lines
Powered by Google Project Hosting