Search in sources :

Example 1 with ActivationLinear

use of org.encog.engine.network.activation.ActivationLinear in project shifu by ShifuML.

the class FloatNeuralStructure method finalizeStruct.

/**
 * Build the synapse and layer structure. This method should be called afteryou are done adding layers to a network,
 * or change the network's logic property.
 */
public void finalizeStruct() {
    if (this.getLayers().size() < 2) {
        throw new NeuralNetworkError("There must be at least two layers before the structure is finalized.");
    }
    final FlatLayer[] flatLayers = new FlatLayer[this.getLayers().size()];
    for (int i = 0; i < this.getLayers().size(); i++) {
        final BasicLayer layer = (BasicLayer) this.getLayers().get(i);
        if (layer.getActivation() == null) {
            layer.setActivation(new ActivationLinear());
        }
        flatLayers[i] = layer;
    }
    this.setFlat(new FloatFlatNetwork(flatLayers, true));
    finalizeLimit();
    this.getLayers().clear();
    enforceLimit();
}
Also used : NeuralNetworkError(org.encog.neural.NeuralNetworkError) ActivationLinear(org.encog.engine.network.activation.ActivationLinear) FlatLayer(org.encog.neural.flat.FlatLayer) BasicLayer(org.encog.neural.networks.layers.BasicLayer)

Example 2 with ActivationLinear

use of org.encog.engine.network.activation.ActivationLinear in project shifu by ShifuML.

the class NNTrainer method buildNetwork.

@SuppressWarnings("unchecked")
public void buildNetwork() {
    network = new BasicNetwork();
    network.addLayer(new BasicLayer(new ActivationLinear(), true, trainSet.getInputSize()));
    int numLayers = (Integer) modelConfig.getParams().get(CommonConstants.NUM_HIDDEN_LAYERS);
    List<String> actFunc = (List<String>) modelConfig.getParams().get(CommonConstants.ACTIVATION_FUNC);
    List<Integer> hiddenNodeList = (List<Integer>) modelConfig.getParams().get(CommonConstants.NUM_HIDDEN_NODES);
    if (numLayers != 0 && (numLayers != actFunc.size() || numLayers != hiddenNodeList.size())) {
        throw new RuntimeException("the number of layer do not equal to the number of activation function or the function list and node list empty");
    }
    if (toLoggingProcess)
        LOG.info("    - total " + numLayers + " layers, each layers are: " + Arrays.toString(hiddenNodeList.toArray()) + " the activation function are: " + Arrays.toString(actFunc.toArray()));
    for (int i = 0; i < numLayers; i++) {
        String func = actFunc.get(i);
        Integer numHiddenNode = hiddenNodeList.get(i);
        // java 6
        if ("linear".equalsIgnoreCase(func)) {
            network.addLayer(new BasicLayer(new ActivationLinear(), true, numHiddenNode));
        } else if (func.equalsIgnoreCase("sigmoid")) {
            network.addLayer(new BasicLayer(new ActivationSigmoid(), true, numHiddenNode));
        } else if (func.equalsIgnoreCase("tanh")) {
            network.addLayer(new BasicLayer(new ActivationTANH(), true, numHiddenNode));
        } else if (func.equalsIgnoreCase("log")) {
            network.addLayer(new BasicLayer(new ActivationLOG(), true, numHiddenNode));
        } else if (func.equalsIgnoreCase("sin")) {
            network.addLayer(new BasicLayer(new ActivationSIN(), true, numHiddenNode));
        } else {
            LOG.info("Unsupported activation function: " + func + " !! Set this layer activation function to be Sigmoid ");
            network.addLayer(new BasicLayer(new ActivationSigmoid(), true, numHiddenNode));
        }
    }
    network.addLayer(new BasicLayer(new ActivationSigmoid(), false, trainSet.getIdealSize()));
    network.getStructure().finalizeStructure();
    if (!modelConfig.isFixInitialInput()) {
        network.reset();
    } else {
        int numWeight = 0;
        for (int i = 0; i < network.getLayerCount() - 1; i++) {
            numWeight = numWeight + network.getLayerTotalNeuronCount(i) * network.getLayerNeuronCount(i + 1);
        }
        LOG.info("    - You have " + numWeight + " weights to be initialize");
        loadWeightsInput(numWeight);
    }
}
Also used : ActivationLinear(org.encog.engine.network.activation.ActivationLinear) ActivationSIN(org.encog.engine.network.activation.ActivationSIN) ActivationLOG(org.encog.engine.network.activation.ActivationLOG) BasicNetwork(org.encog.neural.networks.BasicNetwork) ActivationTANH(org.encog.engine.network.activation.ActivationTANH) ActivationSigmoid(org.encog.engine.network.activation.ActivationSigmoid) ArrayList(java.util.ArrayList) List(java.util.List) BasicLayer(org.encog.neural.networks.layers.BasicLayer)

Example 3 with ActivationLinear

use of org.encog.engine.network.activation.ActivationLinear in project shifu by ShifuML.

the class LogisticRegressionTrainer method train.

/**
 * {@inheritDoc}
 * <p>
 * no <code>regularization</code>
 * <p>
 * Regular will be provide later
 * <p>
 *
 * @throws IOException
 *             e
 */
@Override
public double train() throws IOException {
    classifier = new BasicNetwork();
    classifier.addLayer(new BasicLayer(new ActivationLinear(), true, trainSet.getInputSize()));
    classifier.addLayer(new BasicLayer(new ActivationSigmoid(), false, trainSet.getIdealSize()));
    classifier.getStructure().finalizeStructure();
    // resetParams(classifier);
    classifier.reset();
    // Propagation mlTrain = getMLTrain();
    Propagation propagation = new QuickPropagation(classifier, trainSet, (Double) modelConfig.getParams().get("LearningRate"));
    int epochs = modelConfig.getNumTrainEpochs();
    // Get convergence threshold from modelConfig.
    double threshold = modelConfig.getTrain().getConvergenceThreshold() == null ? 0.0 : modelConfig.getTrain().getConvergenceThreshold().doubleValue();
    String formatedThreshold = df.format(threshold);
    LOG.info("Using " + (Double) modelConfig.getParams().get("LearningRate") + " training rate");
    for (int i = 0; i < epochs; i++) {
        propagation.iteration();
        double trainError = propagation.getError();
        double validError = classifier.calculateError(this.validSet);
        LOG.info("Epoch #" + (i + 1) + " Train Error:" + df.format(trainError) + " Validation Error:" + df.format(validError));
        // Convergence judging.
        double avgErr = (trainError + validError) / 2;
        if (judger.judge(avgErr, threshold)) {
            LOG.info("Trainer-{}> Epoch #{} converged! Average Error: {}, Threshold: {}", trainerID, (i + 1), df.format(avgErr), formatedThreshold);
            break;
        }
    }
    propagation.finishTraining();
    LOG.info("#" + this.trainerID + " finish training");
    saveLR();
    return 0.0d;
}
Also used : BasicNetwork(org.encog.neural.networks.BasicNetwork) Propagation(org.encog.neural.networks.training.propagation.Propagation) QuickPropagation(org.encog.neural.networks.training.propagation.quick.QuickPropagation) ActivationLinear(org.encog.engine.network.activation.ActivationLinear) QuickPropagation(org.encog.neural.networks.training.propagation.quick.QuickPropagation) ActivationSigmoid(org.encog.engine.network.activation.ActivationSigmoid) BasicLayer(org.encog.neural.networks.layers.BasicLayer)

Aggregations

ActivationLinear (org.encog.engine.network.activation.ActivationLinear)3 BasicLayer (org.encog.neural.networks.layers.BasicLayer)3 ActivationSigmoid (org.encog.engine.network.activation.ActivationSigmoid)2 BasicNetwork (org.encog.neural.networks.BasicNetwork)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ActivationLOG (org.encog.engine.network.activation.ActivationLOG)1 ActivationSIN (org.encog.engine.network.activation.ActivationSIN)1 ActivationTANH (org.encog.engine.network.activation.ActivationTANH)1 NeuralNetworkError (org.encog.neural.NeuralNetworkError)1 FlatLayer (org.encog.neural.flat.FlatLayer)1 Propagation (org.encog.neural.networks.training.propagation.Propagation)1 QuickPropagation (org.encog.neural.networks.training.propagation.quick.QuickPropagation)1