Search in sources :

Example 1 with NeuralOutput

use of org.dmg.pmml.NeuralOutputDocument.NeuralOutput 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 2 with NeuralOutput

use of org.dmg.pmml.NeuralOutputDocument.NeuralOutput in project knime-core by knime.

the class PMMLNeuralNetworkTranslator method initiateNeuralOutputs.

/**
 * @param nnModel
 *            the PMML neural network model
 */
private void initiateNeuralOutputs(final NeuralNetwork nnModel) {
    NeuralOutputs neuralOutputs = nnModel.getNeuralOutputs();
    m_classmap = new HashMap<DataCell, Integer>();
    for (NeuralOutput no : neuralOutputs.getNeuralOutputArray()) {
        m_curPercpetronID = no.getOutputNeuron();
        DerivedField df = no.getDerivedField();
        if (df.isSetNormDiscrete()) {
            String value = df.getNormDiscrete().getValue();
            int pos = m_idPosMap.get(m_curPercpetronID);
            m_classmap.put(new StringCell(value), pos);
        } else if (df.isSetFieldRef()) {
            int pos = m_idPosMap.get(m_curPercpetronID);
            m_classmap.put(new StringCell(df.getFieldRef().getField()), pos);
        } else {
            LOGGER.error("The expression is not supported in KNIME MLP.");
        }
    }
}
Also used : NeuralOutputs(org.dmg.pmml.NeuralOutputsDocument.NeuralOutputs) BigInteger(java.math.BigInteger) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) NeuralOutput(org.dmg.pmml.NeuralOutputDocument.NeuralOutput)

Example 3 with NeuralOutput

use of org.dmg.pmml.NeuralOutputDocument.NeuralOutput 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 4 with NeuralOutput

use of org.dmg.pmml.NeuralOutputDocument.NeuralOutput in project knime-core by knime.

the class PMMLNeuralNetworkTranslator method initiateNeuralOutputs.

/**
 * @param nnModel
 *            the PMML neural network model
 */
private void initiateNeuralOutputs(final NeuralNetwork nnModel) {
    NeuralOutputs neuralOutputs = nnModel.getNeuralOutputs();
    m_classmap = new HashMap<DataCell, Integer>();
    for (NeuralOutput no : neuralOutputs.getNeuralOutputArray()) {
        m_curPercpetronID = no.getOutputNeuron();
        DerivedField df = no.getDerivedField();
        if (df.isSetNormDiscrete()) {
            String value = df.getNormDiscrete().getValue();
            int pos = m_idPosMap.get(m_curPercpetronID);
            m_classmap.put(new StringCell(value), pos);
        } else if (df.isSetFieldRef()) {
            int pos = m_idPosMap.get(m_curPercpetronID);
            m_classmap.put(new StringCell(df.getFieldRef().getField()), pos);
        } else {
            LOGGER.error("The expression is not supported in KNIME MLP.");
        }
    }
}
Also used : NeuralOutputs(org.dmg.pmml.NeuralOutputsDocument.NeuralOutputs) BigInteger(java.math.BigInteger) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) NeuralOutput(org.dmg.pmml.NeuralOutputDocument.NeuralOutput)

Aggregations

BigInteger (java.math.BigInteger)4 DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)4 NeuralOutput (org.dmg.pmml.NeuralOutputDocument.NeuralOutput)4 NeuralOutputs (org.dmg.pmml.NeuralOutputsDocument.NeuralOutputs)4 DataCell (org.knime.core.data.DataCell)4 FieldRef (org.dmg.pmml.FieldRefDocument.FieldRef)2 NeuralLayer (org.dmg.pmml.NeuralLayerDocument.NeuralLayer)2 NormDiscrete (org.dmg.pmml.NormDiscreteDocument.NormDiscrete)2 HiddenLayer (org.knime.base.data.neural.HiddenLayer)2 InputLayer (org.knime.base.data.neural.InputLayer)2 InputPerceptron (org.knime.base.data.neural.InputPerceptron)2 Layer (org.knime.base.data.neural.Layer)2 MultiLayerPerceptron (org.knime.base.data.neural.MultiLayerPerceptron)2 Perceptron (org.knime.base.data.neural.Perceptron)2 SigmoidPerceptron (org.knime.base.data.neural.SigmoidPerceptron)2 StringCell (org.knime.core.data.def.StringCell)2