use of org.encog.neural.networks.layers.BasicLayer 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.neural.networks.layers.BasicLayer 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.neural.networks.layers.BasicLayer 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.neural.networks.layers.BasicLayer in project shifu by ShifuML.
the class NNTrainerTest method setUp.
@BeforeClass
public void setUp() {
trainSet = new BasicMLDataSet();
network = new BasicNetwork();
network.addLayer(new BasicLayer(new ActivationLinear(), true, 2));
network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 4));
network.addLayer(new BasicLayer(new ActivationLOG(), true, 3));
network.addLayer(new BasicLayer(new ActivationSIN(), true, 3));
network.addLayer(new BasicLayer(new ActivationTANH(), false, 1));
network.getStructure().finalizeStructure();
network.reset();
}
use of org.encog.neural.networks.layers.BasicLayer in project shifu by ShifuML.
the class DTrainUtils method generateNetwork.
// public static BasicNetwork generateNetwork(int in, int out, int numLayers, List<String> actFunc,
// List<Integer> hiddenNodeList, boolean isRandomizeWeights, double dropoutRate) {
// return generateNetwork(in, out, numLayers, actFunc, hiddenNodeList, isRandomizeWeights, dropoutRate,
// WGT_INIT_DEFAULT);
// }
public static BasicNetwork generateNetwork(int in, int out, int numLayers, List<String> actFunc, List<Integer> hiddenNodeList, boolean isRandomizeWeights, double dropoutRate, String wgtInit, boolean isLinearTarget, String outputActivationFunc) {
final BasicFloatNetwork network = new BasicFloatNetwork();
// in shifuconfig, we have a switch to control enable input layer dropout
if (Boolean.valueOf(Environment.getProperty(CommonConstants.SHIFU_TRAIN_NN_INPUTLAYERDROPOUT_ENABLE, "true"))) {
// we need to guarantee that input layer dropout rate is 40% of hiddenlayer dropout rate
network.addLayer(new BasicDropoutLayer(new ActivationLinear(), true, in, dropoutRate * 0.4d));
} else {
network.addLayer(new BasicDropoutLayer(new ActivationLinear(), true, in, 0d));
}
// int hiddenNodes = 0;
for (int i = 0; i < numLayers; i++) {
String func = actFunc.get(i);
Integer numHiddenNode = hiddenNodeList.get(i);
// hiddenNodes += numHiddenNode;
if (func.equalsIgnoreCase(NNConstants.NN_LINEAR)) {
network.addLayer(new BasicDropoutLayer(new ActivationLinear(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_SIGMOID)) {
network.addLayer(new BasicDropoutLayer(new ActivationSigmoid(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_TANH)) {
network.addLayer(new BasicDropoutLayer(new ActivationTANH(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_LOG)) {
network.addLayer(new BasicDropoutLayer(new ActivationLOG(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_SIN)) {
network.addLayer(new BasicDropoutLayer(new ActivationSIN(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_RELU)) {
network.addLayer(new BasicDropoutLayer(new ActivationReLU(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_LEAKY_RELU)) {
network.addLayer(new BasicDropoutLayer(new ActivationLeakyReLU(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_SWISH)) {
network.addLayer(new BasicDropoutLayer(new ActivationSwish(), true, numHiddenNode, dropoutRate));
} else if (func.equalsIgnoreCase(NNConstants.NN_PTANH)) {
network.addLayer(new BasicDropoutLayer(new ActivationPTANH(), true, numHiddenNode, dropoutRate));
} else {
network.addLayer(new BasicDropoutLayer(new ActivationSigmoid(), true, numHiddenNode, dropoutRate));
}
}
if (isLinearTarget) {
if (NNConstants.NN_RELU.equalsIgnoreCase(outputActivationFunc)) {
network.addLayer(new BasicLayer(new ActivationReLU(), true, out));
} else if (NNConstants.NN_LEAKY_RELU.equalsIgnoreCase(outputActivationFunc)) {
network.addLayer(new BasicLayer(new ActivationLeakyReLU(), true, out));
} else if (NNConstants.NN_SWISH.equalsIgnoreCase(outputActivationFunc)) {
network.addLayer(new BasicLayer(new ActivationSwish(), true, out));
} else {
network.addLayer(new BasicLayer(new ActivationLinear(), true, out));
}
} else {
network.addLayer(new BasicLayer(new ActivationSigmoid(), false, out));
}
NeuralStructure structure = network.getStructure();
if (network.getStructure() instanceof FloatNeuralStructure) {
((FloatNeuralStructure) structure).finalizeStruct();
} else {
structure.finalizeStructure();
}
if (isRandomizeWeights) {
if (wgtInit == null || wgtInit.length() == 0) {
// default randomization
network.reset();
} else if (wgtInit.equalsIgnoreCase(WGT_INIT_GAUSSIAN)) {
new GaussianRandomizer(0, 1).randomize(network);
} else if (wgtInit.equalsIgnoreCase(WGT_INIT_XAVIER)) {
new XavierWeightRandomizer().randomize(network);
} else if (wgtInit.equalsIgnoreCase(WGT_INIT_HE)) {
new HeWeightRandomizer().randomize(network);
} else if (wgtInit.equalsIgnoreCase(WGT_INIT_LECUN)) {
new LecunWeightRandomizer().randomize(network);
} else if (wgtInit.equalsIgnoreCase(WGT_INIT_DEFAULT)) {
// default randomization
network.reset();
} else {
// default randomization
network.reset();
}
}
return network;
}
Aggregations