Search in sources :

Example 1 with NeuralOutputs

use of org.dmg.pmml.neural_network.NeuralOutputs in project jpmml-r by jpmml.

the class ElmNNConverter method encodeModel.

@Override
public NeuralNetwork encodeModel(Schema schema) {
    RGenericVector elmNN = getObject();
    RDoubleVector inpweight = (RDoubleVector) elmNN.getValue("inpweight");
    RDoubleVector biashid = (RDoubleVector) elmNN.getValue("biashid");
    RDoubleVector outweight = (RDoubleVector) elmNN.getValue("outweight");
    RStringVector actfun = (RStringVector) elmNN.getValue("actfun");
    RDoubleVector nhid = (RDoubleVector) elmNN.getValue("nhid");
    Label label = schema.getLabel();
    List<? extends Feature> features = schema.getFeatures();
    switch(actfun.asScalar()) {
        case "purelin":
            break;
        default:
            throw new IllegalArgumentException();
    }
    NeuralInputs neuralInputs = NeuralNetworkUtil.createNeuralInputs(features, DataType.DOUBLE);
    List<? extends Entity> entities = neuralInputs.getNeuralInputs();
    List<NeuralLayer> neuralLayers = new ArrayList<>(2);
    NeuralLayer hiddenNeuralLayer = new NeuralLayer();
    int rows = ValueUtil.asInt(nhid.asScalar());
    int columns = 1 + features.size();
    for (int row = 0; row < rows; row++) {
        List<Double> weights = FortranMatrixUtil.getRow(inpweight.getValues(), rows, columns, row);
        Double bias = biashid.getValue(row);
        bias += weights.remove(0);
        Neuron neuron = NeuralNetworkUtil.createNeuron(entities, weights, bias).setId("hidden/" + String.valueOf(row + 1));
        hiddenNeuralLayer.addNeurons(neuron);
    }
    neuralLayers.add(hiddenNeuralLayer);
    entities = hiddenNeuralLayer.getNeurons();
    NeuralLayer outputNeuralLayer = new NeuralLayer();
    // XXX
    columns = 1;
    for (int column = 0; column < columns; column++) {
        List<Double> weights = FortranMatrixUtil.getColumn(outweight.getValues(), rows, columns, column);
        Double bias = Double.NaN;
        Neuron neuron = NeuralNetworkUtil.createNeuron(entities, weights, bias).setId("output/" + String.valueOf(column + 1));
        outputNeuralLayer.addNeurons(neuron);
    }
    neuralLayers.add(outputNeuralLayer);
    entities = outputNeuralLayer.getNeurons();
    NeuralOutputs neuralOutputs = NeuralNetworkUtil.createRegressionNeuralOutputs(entities, (ContinuousLabel) label);
    NeuralNetwork neuralNetwork = new NeuralNetwork(MiningFunction.REGRESSION, NeuralNetwork.ActivationFunction.IDENTITY, ModelUtil.createMiningSchema(label), neuralInputs, neuralLayers).setNeuralOutputs(neuralOutputs);
    return neuralNetwork;
}
Also used : NeuralOutputs(org.dmg.pmml.neural_network.NeuralOutputs) NeuralInputs(org.dmg.pmml.neural_network.NeuralInputs) ContinuousLabel(org.jpmml.converter.ContinuousLabel) Label(org.jpmml.converter.Label) ArrayList(java.util.ArrayList) NeuralLayer(org.dmg.pmml.neural_network.NeuralLayer) NeuralNetwork(org.dmg.pmml.neural_network.NeuralNetwork) Neuron(org.dmg.pmml.neural_network.Neuron)

Aggregations

ArrayList (java.util.ArrayList)1 NeuralInputs (org.dmg.pmml.neural_network.NeuralInputs)1 NeuralLayer (org.dmg.pmml.neural_network.NeuralLayer)1 NeuralNetwork (org.dmg.pmml.neural_network.NeuralNetwork)1 NeuralOutputs (org.dmg.pmml.neural_network.NeuralOutputs)1 Neuron (org.dmg.pmml.neural_network.Neuron)1 ContinuousLabel (org.jpmml.converter.ContinuousLabel)1 Label (org.jpmml.converter.Label)1