Search in sources :

Example 1 with NeuralLayer

use of org.dmg.pmml.NeuralLayerDocument.NeuralLayer 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)

Example 2 with NeuralLayer

use of org.dmg.pmml.NeuralLayerDocument.NeuralLayer 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);
}
Also used : SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) Con(org.dmg.pmml.ConDocument.Con) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer) 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) Neuron(org.dmg.pmml.NeuronDocument.Neuron)

Example 3 with NeuralLayer

use of org.dmg.pmml.NeuralLayerDocument.NeuralLayer 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);
}
Also used : SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) Con(org.dmg.pmml.ConDocument.Con) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer) 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) Neuron(org.dmg.pmml.NeuronDocument.Neuron)

Example 4 with NeuralLayer

use of org.dmg.pmml.NeuralLayerDocument.NeuralLayer in project knime-core by knime.

the class PMMLNeuralNetworkTranslator method initiateHiddenLayers.

/**
 * @param nnModel the PMML neural network model
 */
private void initiateHiddenLayers(final NeuralNetwork nnModel) {
    for (int i = 0; i < nnModel.getNeuralLayerArray().length - 1; i++) {
        NeuralLayer hiddenLayer = nnModel.getNeuralLayerArray(i);
        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);
    }
}
Also used : SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) Con(org.dmg.pmml.ConDocument.Con) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer) 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) Neuron(org.dmg.pmml.NeuronDocument.Neuron)

Example 5 with NeuralLayer

use of org.dmg.pmml.NeuralLayerDocument.NeuralLayer in project knime-core by knime.

the class PMMLNeuralNetworkTranslator method initiateHiddenLayers.

/**
 * @param nnModel the PMML neural network model
 */
private void initiateHiddenLayers(final NeuralNetwork nnModel) {
    for (int i = 0; i < nnModel.getNeuralLayerArray().length - 1; i++) {
        NeuralLayer hiddenLayer = nnModel.getNeuralLayerArray(i);
        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);
    }
}
Also used : SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) Con(org.dmg.pmml.ConDocument.Con) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer) 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) Neuron(org.dmg.pmml.NeuronDocument.Neuron)

Aggregations

Con (org.dmg.pmml.ConDocument.Con)6 NeuralLayer (org.dmg.pmml.NeuralLayerDocument.NeuralLayer)6 Neuron (org.dmg.pmml.NeuronDocument.Neuron)6 HiddenLayer (org.knime.base.data.neural.HiddenLayer)6 InputPerceptron (org.knime.base.data.neural.InputPerceptron)6 MultiLayerPerceptron (org.knime.base.data.neural.MultiLayerPerceptron)6 Perceptron (org.knime.base.data.neural.Perceptron)6 SigmoidPerceptron (org.knime.base.data.neural.SigmoidPerceptron)6 BigInteger (java.math.BigInteger)4 InputLayer (org.knime.base.data.neural.InputLayer)2 Layer (org.knime.base.data.neural.Layer)2 AttributesImpl (org.xml.sax.helpers.AttributesImpl)2