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