Search in sources :

Example 1 with ActivationFunction

use of org.dmg.pmml.neural_network.NeuralNetwork.ActivationFunction in project shifu by ShifuML.

the class PMMLEncogNeuralNetworkModel method adaptMLModelToPMML.

/**
 * The function which converts an Encog NeuralNetwork to a PMML
 * NeuralNetwork Model.
 * <p>
 * This function reads the weights from the Encog NeuralNetwork model and assign them to the corresponding
 * connections of Neurons in PMML model.
 *
 * @param bNetwork
 *            Encog NeuralNetwork
 * @param pmmlModel
 *            DataFieldUtility that provides supplementary data field for
 *            the model conversion
 * @return The generated PMML NeuralNetwork Model
 */
public org.dmg.pmml.neural_network.NeuralNetwork adaptMLModelToPMML(org.encog.neural.networks.BasicNetwork bNetwork, org.dmg.pmml.neural_network.NeuralNetwork pmmlModel) {
    network = bNetwork.getFlat();
    pmmlModel = new NeuralNetworkModelIntegrator().adaptPMML(pmmlModel);
    int[] layerCount = network.getLayerCounts();
    int[] layerFeedCount = network.getLayerFeedCounts();
    double[] weights = network.getWeights();
    ActivationFunction[] functionList = transformActivationFunction(network.getActivationFunctions());
    int numLayers = layerCount.length;
    int weightID = 0;
    List<NeuralLayer> layerList = new ArrayList<NeuralLayer>();
    // pmmlModel.withFunctionName(MiningFunctionType.REGRESSION);
    for (int i = 0; i < numLayers - 1; i++) {
        NeuralLayer layer = new NeuralLayer();
        layer.setNumberOfNeurons(layerFeedCount[i]);
        layer.setActivationFunction(functionList[i]);
        int layerID = numLayers - i - 1;
        for (int j = 0; j < layerFeedCount[i]; j++) {
            Neuron neuron = new Neuron();
            neuron.setId(String.valueOf(layerID + "," + j));
            for (int k = 0; k < layerFeedCount[i + 1]; k++) {
                neuron.addConnections(new Connection(String.valueOf(layerID - 1 + "," + k), weights[weightID++]));
            }
            // weights
            int tmp = layerCount[i + 1] - layerFeedCount[i + 1];
            for (int k = 0; k < tmp; k++) {
                neuron.setBias(weights[weightID++]);
            }
            // bias neuron for each layer
            layer.addNeurons(neuron);
        }
        // finish build Neuron
        layerList.add(layer);
    }
    // finish build layer
    // reserve the layer list to fit fot PMML format
    Collections.reverse(layerList);
    pmmlModel.addNeuralLayers(layerList.toArray(new NeuralLayer[layerList.size()]));
    // set neural output based on target id
    pmmlModel.setNeuralOutputs(PMMLAdapterCommonUtil.getOutputFields(pmmlModel.getMiningSchema(), numLayers - 1));
    return pmmlModel;
}
Also used : Neuron(org.dmg.pmml.neural_network.Neuron) ActivationFunction(org.dmg.pmml.neural_network.NeuralNetwork.ActivationFunction) ArrayList(java.util.ArrayList) Connection(org.dmg.pmml.neural_network.Connection) NeuralLayer(org.dmg.pmml.neural_network.NeuralLayer)

Aggregations

ArrayList (java.util.ArrayList)1 Connection (org.dmg.pmml.neural_network.Connection)1 NeuralLayer (org.dmg.pmml.neural_network.NeuralLayer)1 ActivationFunction (org.dmg.pmml.neural_network.NeuralNetwork.ActivationFunction)1 Neuron (org.dmg.pmml.neural_network.Neuron)1