Search in sources :

Example 11 with Perceptron

use of org.knime.base.data.neural.Perceptron in project knime-core by knime.

the class PMMLNeuralNetworkTranslator method addOutputLayer.

/**
 * Writes the PMML output layer of the MLP.
 *
 * @param nnModel
 *            the neural network model.
 * @param mlp
 *            the underlying {@link MultiLayerPerceptron}.
 * @param spec
 *            the port object spec
 */
protected void addOutputLayer(final NeuralNetwork nnModel, final MultiLayerPerceptron mlp, final PMMLPortObjectSpec spec) {
    int lastlayer = mlp.getNrLayers() - 1;
    String targetCol = spec.getTargetFields().iterator().next();
    Layer outputlayer = mlp.getLayer(lastlayer);
    Perceptron[] outputperceptrons = outputlayer.getPerceptrons();
    HashMap<DataCell, Integer> outputmap = mlp.getClassMapping();
    NeuralOutputs neuralOuts = nnModel.addNewNeuralOutputs();
    neuralOuts.setNumberOfOutputs(BigInteger.valueOf(outputperceptrons.length));
    for (int i = 0; i < outputperceptrons.length; i++) {
        NeuralOutput neuralOutput = neuralOuts.addNewNeuralOutput();
        neuralOutput.setOutputNeuron(lastlayer + "," + i);
        // search corresponding output value
        String colname = "";
        for (Entry<DataCell, Integer> e : outputmap.entrySet()) {
            if (e.getValue().equals(i)) {
                colname = ((StringValue) e.getKey()).getStringValue();
            }
        }
        DerivedField df = neuralOutput.addNewDerivedField();
        df.setOptype(OPTYPE.CATEGORICAL);
        df.setDataType(DATATYPE.STRING);
        if (mlp.getMode() == MultiLayerPerceptron.CLASSIFICATION_MODE) {
            df.setOptype(OPTYPE.CATEGORICAL);
            df.setDataType(DATATYPE.STRING);
        } else if (mlp.getMode() == MultiLayerPerceptron.REGRESSION_MODE) {
            df.setOptype(OPTYPE.CONTINUOUS);
            df.setDataType(DATATYPE.DOUBLE);
        }
        if (mlp.getMode() == MultiLayerPerceptron.CLASSIFICATION_MODE) {
            NormDiscrete normDiscrete = df.addNewNormDiscrete();
            normDiscrete.setField(targetCol);
            normDiscrete.setValue(colname);
        } else if (mlp.getMode() == MultiLayerPerceptron.REGRESSION_MODE) {
            FieldRef fieldRef = df.addNewFieldRef();
            fieldRef.setField(targetCol);
        }
    }
}
Also used : NeuralOutputs(org.dmg.pmml.NeuralOutputsDocument.NeuralOutputs) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) Layer(org.knime.base.data.neural.Layer) InputLayer(org.knime.base.data.neural.InputLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer) NeuralOutput(org.dmg.pmml.NeuralOutputDocument.NeuralOutput) BigInteger(java.math.BigInteger) NormDiscrete(org.dmg.pmml.NormDiscreteDocument.NormDiscrete) SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) MultiLayerPerceptron(org.knime.base.data.neural.MultiLayerPerceptron) Perceptron(org.knime.base.data.neural.Perceptron) InputPerceptron(org.knime.base.data.neural.InputPerceptron) DataCell(org.knime.core.data.DataCell) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 12 with Perceptron

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);
}
Also used : BigInteger(java.math.BigInteger) SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) MultiLayerPerceptron(org.knime.base.data.neural.MultiLayerPerceptron) Perceptron(org.knime.base.data.neural.Perceptron) InputPerceptron(org.knime.base.data.neural.InputPerceptron) InputLayer(org.knime.base.data.neural.InputLayer) NeuralInputs(org.dmg.pmml.NeuralInputsDocument.NeuralInputs) InputPerceptron(org.knime.base.data.neural.InputPerceptron) NeuralInput(org.dmg.pmml.NeuralInputDocument.NeuralInput)

Example 13 with Perceptron

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]);
        }
    }
}
Also used : SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) MultiLayerPerceptron(org.knime.base.data.neural.MultiLayerPerceptron) Perceptron(org.knime.base.data.neural.Perceptron) InputPerceptron(org.knime.base.data.neural.InputPerceptron) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Neuron(org.dmg.pmml.NeuronDocument.Neuron) Con(org.dmg.pmml.ConDocument.Con) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) Layer(org.knime.base.data.neural.Layer) InputLayer(org.knime.base.data.neural.InputLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer)

Aggregations

MultiLayerPerceptron (org.knime.base.data.neural.MultiLayerPerceptron)13 Perceptron (org.knime.base.data.neural.Perceptron)13 InputPerceptron (org.knime.base.data.neural.InputPerceptron)12 SigmoidPerceptron (org.knime.base.data.neural.SigmoidPerceptron)12 BigInteger (java.math.BigInteger)10 NeuralLayer (org.dmg.pmml.NeuralLayerDocument.NeuralLayer)10 HiddenLayer (org.knime.base.data.neural.HiddenLayer)10 InputLayer (org.knime.base.data.neural.InputLayer)8 Con (org.dmg.pmml.ConDocument.Con)6 Neuron (org.dmg.pmml.NeuronDocument.Neuron)6 Layer (org.knime.base.data.neural.Layer)6 DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)4 FieldRef (org.dmg.pmml.FieldRefDocument.FieldRef)4 NeuralInput (org.dmg.pmml.NeuralInputDocument.NeuralInput)4 NeuralInputs (org.dmg.pmml.NeuralInputsDocument.NeuralInputs)4 NeuralOutput (org.dmg.pmml.NeuralOutputDocument.NeuralOutput)2 NeuralOutputs (org.dmg.pmml.NeuralOutputsDocument.NeuralOutputs)2 NormDiscrete (org.dmg.pmml.NormDiscreteDocument.NormDiscrete)2 DataCell (org.knime.core.data.DataCell)2 AttributesImpl (org.xml.sax.helpers.AttributesImpl)2