Search in sources :

Example 1 with ActivationSigmoid

use of org.encog.engine.network.activation.ActivationSigmoid 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 2 with ActivationSigmoid

use of org.encog.engine.network.activation.ActivationSigmoid 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)

Example 3 with ActivationSigmoid

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

the class AbstractNNWorker method initGradient.

@SuppressWarnings("unchecked")
private void initGradient(FloatMLDataSet training, FloatMLDataSet testing, double[] weights, boolean isCrossOver) {
    int numLayers = (Integer) this.validParams.get(CommonConstants.NUM_HIDDEN_LAYERS);
    List<String> actFunc = (List<String>) this.validParams.get(CommonConstants.ACTIVATION_FUNC);
    List<Integer> hiddenNodeList = (List<Integer>) this.validParams.get(CommonConstants.NUM_HIDDEN_NODES);
    String outputActivationFunc = (String) validParams.get(CommonConstants.OUTPUT_ACTIVATION_FUNC);
    BasicNetwork network = DTrainUtils.generateNetwork(this.featureInputsCnt, this.outputNodeCount, numLayers, actFunc, hiddenNodeList, false, this.dropoutRate, this.wgtInit, CommonUtils.isLinearTarget(modelConfig, columnConfigList), outputActivationFunc);
    // use the weights from master
    network.getFlat().setWeights(weights);
    FlatNetwork flat = network.getFlat();
    // copy Propagation from encog, fix flat spot problem
    double[] flatSpot = new double[flat.getActivationFunctions().length];
    for (int i = 0; i < flat.getActivationFunctions().length; i++) {
        flatSpot[i] = flat.getActivationFunctions()[i] instanceof ActivationSigmoid ? 0.1 : 0.0;
    }
    LOG.info("Gradient computing thread count is {}.", modelConfig.getTrain().getWorkerThreadCount());
    this.gradient = new ParallelGradient((FloatFlatNetwork) flat, training, testing, flatSpot, new LinearErrorFunction(), isCrossOver, modelConfig.getTrain().getWorkerThreadCount(), this.lossStr, this.batchs);
}
Also used : LinearErrorFunction(org.encog.neural.error.LinearErrorFunction) FloatFlatNetwork(ml.shifu.shifu.core.dtrain.dataset.FloatFlatNetwork) FlatNetwork(org.encog.neural.flat.FlatNetwork) FloatFlatNetwork(ml.shifu.shifu.core.dtrain.dataset.FloatFlatNetwork) BasicNetwork(org.encog.neural.networks.BasicNetwork) ActivationSigmoid(org.encog.engine.network.activation.ActivationSigmoid) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with ActivationSigmoid

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

the class DTrainTest method initGradient.

public Gradient initGradient(MLDataSet training) {
    FlatNetwork flat = network.getFlat().clone();
    // copy Propagation from encog
    double[] flatSpot = new double[flat.getActivationFunctions().length];
    for (int i = 0; i < flat.getActivationFunctions().length; i++) {
        final ActivationFunction af = flat.getActivationFunctions()[i];
        if (af instanceof ActivationSigmoid) {
            flatSpot[i] = 0.1;
        } else {
            flatSpot[i] = 0.0;
        }
    }
    return new Gradient(flat, training.openAdditional(), training, flatSpot, new LinearErrorFunction(), false);
}
Also used : LinearErrorFunction(org.encog.neural.error.LinearErrorFunction) FlatNetwork(org.encog.neural.flat.FlatNetwork) ActivationFunction(org.encog.engine.network.activation.ActivationFunction) ActivationSigmoid(org.encog.engine.network.activation.ActivationSigmoid)

Aggregations

ActivationSigmoid (org.encog.engine.network.activation.ActivationSigmoid)4 BasicNetwork (org.encog.neural.networks.BasicNetwork)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ActivationLinear (org.encog.engine.network.activation.ActivationLinear)2 LinearErrorFunction (org.encog.neural.error.LinearErrorFunction)2 FlatNetwork (org.encog.neural.flat.FlatNetwork)2 BasicLayer (org.encog.neural.networks.layers.BasicLayer)2 FloatFlatNetwork (ml.shifu.shifu.core.dtrain.dataset.FloatFlatNetwork)1 ActivationFunction (org.encog.engine.network.activation.ActivationFunction)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 Propagation (org.encog.neural.networks.training.propagation.Propagation)1 QuickPropagation (org.encog.neural.networks.training.propagation.quick.QuickPropagation)1