Search in sources :

Example 1 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef 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));
    }
}
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) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) NeuralInputs(org.dmg.pmml.NeuralInputsDocument.NeuralInputs) 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) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) NeuralInput(org.dmg.pmml.NeuralInputDocument.NeuralInput)

Example 2 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef in project knime-core by knime.

the class PMMLGeneralRegressionTranslator method updateVectorLengthsBasedOnDerivedFields.

/**
 * @param derivedFieldList
 */
private void updateVectorLengthsBasedOnDerivedFields(final List<DerivedField> derivedFieldList) {
    final Map<String, Integer> lengths = new LinkedHashMap<>();
    for (final DerivedField df : derivedFieldList) {
        final String name = df.getName();
        Optional<NameAndIndex> vni = VectorHandling.parse(name);
        if (vni.isPresent()) {
            final String key = vni.get().getName();
            try {
                String function = df.getApply().getFunction();
                if (!"substring".equals(function)) {
                    continue;
                }
                final List<FieldRef> fieldRefList = df.getApply().getFieldRefList();
                if (fieldRefList.isEmpty() || !key.equals(fieldRefList.get(0).getField())) {
                    LOGGER.debug("Field name is not related to the derived field name: " + fieldRefList + " <-> " + key);
                    continue;
                }
                if (2 != df.getApply().getConstantList().size()) {
                    LOGGER.debug("substring requires two parameters: " + df);
                    continue;
                }
                if (!DATATYPE.INTEGER.equals(df.getDataType())) {
                    LOGGER.debug("Array value should be integer: " + df);
                    continue;
                }
                if (!OPTYPE.CONTINUOUS.equals(df.getOptype())) {
                    LOGGER.debug("The optype should be continuous: " + df);
                    continue;
                }
                int index = vni.get().getIndex();
                int old = Math.max(0, lengths.getOrDefault(key, Integer.valueOf(0)).intValue());
                if (old <= index) {
                    lengths.put(key, index + 1);
                }
            } catch (RuntimeException e) {
                // Ignore
                LOGGER.debug(df.toString(), e);
            }
        }
    }
    LOGGER.debug(lengths);
    m_content.updateVectorLengths(lengths);
}
Also used : BigInteger(java.math.BigInteger) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) NameAndIndex(org.knime.base.node.mine.regression.pmmlgreg.VectorHandling.NameAndIndex) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef 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));
    }
}
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) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) NeuralInputs(org.dmg.pmml.NeuralInputsDocument.NeuralInputs) 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) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) NeuralInput(org.dmg.pmml.NeuralInputDocument.NeuralInput)

Example 4 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef 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 5 with FieldRef

use of org.dmg.pmml.FieldRefDocument.FieldRef in project knime-core by knime.

the class PMMLStringConversionTranslator method createDerivedFields.

private DerivedField[] createDerivedFields() {
    DATATYPE.Enum dataType = PMMLDataDictionaryTranslator.getPMMLDataType(m_parseType);
    OPTYPE.Enum optype = PMMLDataDictionaryTranslator.getOptype(m_parseType);
    int num = m_includeCols.size();
    DerivedField[] derivedFields = new DerivedField[num];
    for (int i = 0; i < num; i++) {
        DerivedField df = DerivedField.Factory.newInstance();
        String name = m_includeCols.get(i);
        df.setDisplayName(name);
        /* The field name must be retrieved before creating a new derived
             * name for this derived field as the map only contains the
             * current mapping. */
        String fieldName = m_mapper.getDerivedFieldName(name);
        df.setName(m_mapper.createDerivedFieldName(name));
        df.setDataType(dataType);
        df.setOptype(optype);
        FieldRef fieldRef = df.addNewFieldRef();
        fieldRef.setField(fieldName);
        derivedFields[i] = df;
    }
    return derivedFields;
}
Also used : DATATYPE(org.dmg.pmml.DATATYPE) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) OPTYPE(org.dmg.pmml.OPTYPE) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Aggregations

DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)11 FieldRef (org.dmg.pmml.FieldRefDocument.FieldRef)11 BigInteger (java.math.BigInteger)6 NeuralLayer (org.dmg.pmml.NeuralLayerDocument.NeuralLayer)4 HiddenLayer (org.knime.base.data.neural.HiddenLayer)4 InputLayer (org.knime.base.data.neural.InputLayer)4 InputPerceptron (org.knime.base.data.neural.InputPerceptron)4 Layer (org.knime.base.data.neural.Layer)4 MultiLayerPerceptron (org.knime.base.data.neural.MultiLayerPerceptron)4 Perceptron (org.knime.base.data.neural.Perceptron)4 SigmoidPerceptron (org.knime.base.data.neural.SigmoidPerceptron)4 ArrayList (java.util.ArrayList)3 NormDiscrete (org.dmg.pmml.NormDiscreteDocument.NormDiscrete)3 Apply (org.dmg.pmml.ApplyDocument.Apply)2 DATATYPE (org.dmg.pmml.DATATYPE)2 NeuralInput (org.dmg.pmml.NeuralInputDocument.NeuralInput)2 NeuralInputs (org.dmg.pmml.NeuralInputsDocument.NeuralInputs)2 NeuralOutput (org.dmg.pmml.NeuralOutputDocument.NeuralOutput)2 NeuralOutputs (org.dmg.pmml.NeuralOutputsDocument.NeuralOutputs)2 OPTYPE (org.dmg.pmml.OPTYPE)2