use of org.knime.base.data.neural.Perceptron in project knime-core by knime.
the class PMMLNeuralNetworkTranslator method addInputLayer.
/**
* Writes the PMML input layer of the MLP.
*
* @param nnModel
* the Neural Network model.
* @param mlp
* the underlying {@link MultiLayerPerceptron}.
*/
protected void addInputLayer(final NeuralNetwork nnModel, final MultiLayerPerceptron mlp) {
Layer inputlayer = mlp.getLayer(0);
Perceptron[] inputperceptrons = inputlayer.getPerceptrons();
HashMap<String, Integer> inputmap = mlp.getInputMapping();
NeuralInputs neuralInputs = nnModel.addNewNeuralInputs();
neuralInputs.setNumberOfInputs(BigInteger.valueOf(inputperceptrons.length));
for (int i = 0; i < inputperceptrons.length; i++) {
NeuralInput neuralInput = neuralInputs.addNewNeuralInput();
neuralInput.setId(0 + "," + i);
// search corresponding input column
String colname = "";
for (Entry<String, Integer> e : inputmap.entrySet()) {
if (e.getValue().equals(i)) {
colname = e.getKey();
}
}
DerivedField df = neuralInput.addNewDerivedField();
df.setOptype(OPTYPE.CONTINUOUS);
df.setDataType(DATATYPE.DOUBLE);
FieldRef fieldRef = df.addNewFieldRef();
fieldRef.setField(m_nameMapper.getDerivedFieldName(colname));
}
}
use of org.knime.base.data.neural.Perceptron in project knime-core by knime.
the class PMMLNeuralNetworkTranslator method initInputLayer.
/**
* @param nnModel the PMML neural network model
*/
private void initInputLayer(final NeuralNetwork nnModel) {
NeuralInputs neuralInputs = nnModel.getNeuralInputs();
m_idPosMap = new HashMap<String, Integer>();
m_curPerceptrons = new Vector<Perceptron>();
m_inputmap = new HashMap<String, Integer>();
m_counter = 0;
m_curLayer = 0;
for (NeuralInput ni : neuralInputs.getNeuralInputArray()) {
m_curPercpetronID = ni.getId();
String fieldName = m_nameMapper.getColumnName(ni.getDerivedField().getFieldRef().getField());
Perceptron p = new InputPerceptron();
p.setClassValue(fieldName);
m_inputmap.put(fieldName, m_counter);
m_curPerceptrons.add(p);
m_idPosMap.put(m_curPercpetronID, m_counter);
m_counter++;
}
Perceptron[] curPerceptrons = new Perceptron[m_curPerceptrons.size()];
curPerceptrons = m_curPerceptrons.toArray(curPerceptrons);
m_predLayer = new InputLayer(curPerceptrons);
m_allLayers.add(m_curLayer, new InputLayer(curPerceptrons));
m_predPerceptrons = curPerceptrons;
m_predidPosMap = new HashMap<String, Integer>(m_idPosMap);
}
use of org.knime.base.data.neural.Perceptron in project knime-core by knime.
the class PMMLNeuralNetworkTranslator method addLayer.
/**
* Writes a layer of the MLP.
*
* @param nnModel
* the NeuralNetwork model.
* @param mlp
* the underlying {@link MultiLayerPerceptron}.
* @param layer
* the number of the current layer.
*/
protected void addLayer(final NeuralNetwork nnModel, final MultiLayerPerceptron mlp, final int layer) {
Layer curLayer = mlp.getLayer(layer);
Perceptron[] perceptrons = curLayer.getPerceptrons();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute(null, null, "numberOfNeurons", CDATA, "" + perceptrons.length);
NeuralLayer neuralLayer = nnModel.addNewNeuralLayer();
for (int i = 0; i < perceptrons.length; i++) {
Neuron neuron = neuralLayer.addNewNeuron();
neuron.setId(layer + "," + i);
neuron.setBias(-1 * perceptrons[i].getThreshold());
double[] weights = perceptrons[i].getWeights();
int predLayerLength = weights.length;
for (int j = 0; j < predLayerLength; j++) {
Con con = neuron.addNewCon();
con.setFrom((layer - 1) + "," + j);
con.setWeight(weights[j]);
}
}
}
use of org.knime.base.data.neural.Perceptron in project knime-core by knime.
the class PMMLNeuralNetworkTranslator method initiateFinalLayer.
/**
* @param nnModel
* the PMML neural network model
*/
private void initiateFinalLayer(final NeuralNetwork nnModel) {
NeuralLayer hiddenLayer = nnModel.getNeuralLayerArray(nnModel.getNeuralLayerArray().length - 1);
m_counter = 0;
m_idPosMap = new HashMap<String, Integer>();
m_curLayer++;
m_curPerceptrons = new Vector<Perceptron>();
for (Neuron neuron : hiddenLayer.getNeuronArray()) {
m_weights = new double[m_predPerceptrons.length];
m_curPercpetronID = neuron.getId();
m_curThreshold = -1 * neuron.getBias();
for (Con con : neuron.getConArray()) {
String fromID = con.getFrom();
double weight = con.getWeight();
int pos = m_predidPosMap.get(fromID);
m_weights[pos] = weight;
}
Perceptron p = new SigmoidPerceptron(m_weights, m_predPerceptrons);
p.setThreshold(m_curThreshold);
m_curPerceptrons.add(p);
m_idPosMap.put(m_curPercpetronID, m_counter);
m_counter++;
}
Perceptron[] curPerceptrons = new Perceptron[m_curPerceptrons.size()];
curPerceptrons = m_curPerceptrons.toArray(curPerceptrons);
m_allLayers.add(m_curLayer, new HiddenLayer(m_predLayer, curPerceptrons));
m_predLayer = m_allLayers.get(m_curLayer);
m_predPerceptrons = curPerceptrons;
m_predidPosMap = new HashMap<String, Integer>(m_idPosMap);
}
use of org.knime.base.data.neural.Perceptron in project knime-core by knime.
the class PMMLNeuralNetworkTranslator method addInputLayer.
/**
* Writes the PMML input layer of the MLP.
*
* @param nnModel
* the Neural Network model.
* @param mlp
* the underlying {@link MultiLayerPerceptron}.
*/
protected void addInputLayer(final NeuralNetwork nnModel, final MultiLayerPerceptron mlp) {
Layer inputlayer = mlp.getLayer(0);
Perceptron[] inputperceptrons = inputlayer.getPerceptrons();
HashMap<String, Integer> inputmap = mlp.getInputMapping();
NeuralInputs neuralInputs = nnModel.addNewNeuralInputs();
neuralInputs.setNumberOfInputs(BigInteger.valueOf(inputperceptrons.length));
for (int i = 0; i < inputperceptrons.length; i++) {
NeuralInput neuralInput = neuralInputs.addNewNeuralInput();
neuralInput.setId(0 + "," + i);
// search corresponding input column
String colname = "";
for (Entry<String, Integer> e : inputmap.entrySet()) {
if (e.getValue().equals(i)) {
colname = e.getKey();
}
}
DerivedField df = neuralInput.addNewDerivedField();
df.setOptype(OPTYPE.CONTINUOUS);
df.setDataType(DATATYPE.DOUBLE);
FieldRef fieldRef = df.addNewFieldRef();
fieldRef.setField(m_nameMapper.getDerivedFieldName(colname));
}
}
Aggregations