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);
}
}
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;
}
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);
}
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);
}
Aggregations