本文整理汇总了Java中org.encog.neural.networks.BasicNetwork类的典型用法代码示例。如果您正苦于以下问题:Java BasicNetwork类的具体用法?Java BasicNetwork怎么用?Java BasicNetwork使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BasicNetwork类属于org.encog.neural.networks包,在下文中一共展示了BasicNetwork类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createNeuralNetwork
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
private BasicNetwork createNeuralNetwork() {
BasicNetwork network = new BasicNetwork();
// input layer
network.addLayer(new BasicLayer(null, true, inputLayerSize));
// hidden layer
network.addLayer(new BasicLayer(new ActivationSigmoid(), true, inputLayerSize / 6));
network.addLayer(new BasicLayer(new ActivationSigmoid(), true, inputLayerSize / 6 / 4));
// output layer
network.addLayer(new BasicLayer(new ActivationSigmoid(), false, outputLayerSize));
network.getStructure().finalizeStructure();
network.reset();
return network;
}
示例2: trainAndStore
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
@Test
public void trainAndStore() {
BasicMLDataSet dataSet = getData();
// Create network
BasicNetwork network = getNetwork();
// Train
System.out.println("Training network...");
Train train = new ResilientPropagation(network, dataSet);
for (int i = 0; i < TRAIN_ITERATIONS; i++) {
train.iteration();
}
System.out.println("Training finished, error: " + train.getError());
// Save to file
System.out.println("Saving to file...");
saveToFile(network);
System.out.println("Done");
}
示例3: testNewNetwork
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
/**
* Test to see if the respective method creates a new basic neural network
* and correctly assigns all three layer sizes.
*/
@Test
public void testNewNetwork() {
/*
* Create ANN with random layer sizes. Layer sizes are rarely going to
* be larger than 10 neurons each.
*/
int inputSize = Util.PRNG.nextInt(10);
int hiddenSize = Util.PRNG.nextInt(10);
int outputSize = Util.PRNG.nextInt(10);
BasicNetwork net = Util.newNetwork(inputSize, hiddenSize, outputSize);
/*
* All layers should have their sizes assigned correctly.
*/
assertEquals(inputSize, net.getInputCount());
assertEquals(hiddenSize, net.getLayerNeuronCount(1));
assertEquals(outputSize, net.getOutputCount());
}
示例4: initWeights
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
private NNParams initWeights(MasterContext<NNParams, NNParams> context) {
int inputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_INPUT_NODES),
NNConstants.GUAGUA_NN_DEFAULT_INPUT_NODES);
int hiddens = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_HIDDEN_NODES),
NNConstants.GUAGUA_NN_DEFAULT_HIDDEN_NODES);
int outputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_OUTPUT_NODES),
NNConstants.GUAGUA_NN_DEFAULT_OUTPUT_NODES);
this.learningRate = NumberFormatUtils.getDouble(context.getProps().getProperty(
NNConstants.GUAGUA_NN_LEARNING_RATE, NNConstants.GUAGUA_NN_DEFAULT_LEARNING_RATE));
BasicNetwork network = NNUtils.generateNetwork(inputs, hiddens, outputs);
NNParams params = new NNParams();
params.setTrainError(0);
params.setTestError(0);
// prevent null point
params.setGradients(new double[0]);
params.setWeights(network.getFlat().getWeights());
return params;
}
示例5: postApplication
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
@Override
public void postApplication(MasterContext<NNParams, NNParams> context) {
LOG.info("NNOutput starts to write model to files.");
int inputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_INPUT_NODES),
NNConstants.GUAGUA_NN_DEFAULT_INPUT_NODES);
int hiddens = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_HIDDEN_NODES),
NNConstants.GUAGUA_NN_DEFAULT_HIDDEN_NODES);
int outputs = NumberFormatUtils.getInt(context.getProps().getProperty(NNConstants.GUAGUA_NN_OUTPUT_NODES),
NNConstants.GUAGUA_NN_DEFAULT_OUTPUT_NODES);
BasicNetwork network = NNUtils.generateNetwork(inputs, hiddens, outputs);
Path out = new Path(context.getProps().getProperty(NNConstants.GUAGUA_NN_OUTPUT));
FSDataOutputStream fos = null;
try {
fos = FileSystem.get(new Configuration()).create(out);
LOG.info("Writing results to {}", out.toString());
network.getFlat().setWeights(context.getMasterResult().getWeights());
EncogDirectoryPersistence.saveObject(fos, network);
} catch (IOException e) {
LOG.error("Error in writing output.", e);
} finally {
IOUtils.closeStream(fos);
}
}
示例6: getError
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
private double[] getError(NeuralDataSet trainingSet, BasicNetwork network){
double total = 0.0;
double size = 0;
double RSS = 0.0;
double Ry = 0.0;
for (MLDataPair pair : trainingSet) {
final MLData output = network.compute(pair.getInput());
if (Double.isNaN(output.getData(0))) {
throw new RuntimeException("There is a NaN! may be something wrong with the data conversion");
}
double result = (Double.isNaN(output.getData(0)))? pair.getIdeal().getData(0) : output.getData(0);
//System.out.print("Result ********** " +result+"\n");
total += calculateEachMAPE(pair.getIdeal().getData(0),
result);
double difference = pair.getIdeal().getData(0) - result;
RSS += Math.pow(difference, 2);
Ry += Math.pow(pair.getIdeal().getData(0) - meanIdeal, 2);
size++;
}
return new double[]{Ry==0?RSS:RSS/Ry,
total/size};
}
示例7: SimpleNeuralNetVisualizer
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
public SimpleNeuralNetVisualizer(BasicNetwork neuralNet, String[] attributeNames) {
this.neuralNet = neuralNet;
this.attributeNames = attributeNames;
addMouseListener(this);
// calculate maximal absolute weight
this.maxAbsoluteWeight = Double.NEGATIVE_INFINITY;
List<Layer> layers = this.neuralNet.getLayers();
Iterator i = layers.iterator();
while (i.hasNext()) {
Layer layer = (Layer)i.next();
if (layer.hasMatrix()) {
Matrix matrix = layer.getMatrix();
int rows = matrix.getRows();
int cols = matrix.getCols();
for (int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
double weight = matrix.get(r, c);
this.maxAbsoluteWeight = Math.max(this.maxAbsoluteWeight, Math.abs(weight));
}
}
}
}
}
示例8: Perceptron
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
public Perceptron(int in, int out){
this.numberOutputs = out;
this.numberInputs = in;
//this.network = new BasicNetwork();
//In
/*network.addLayer(new BasicLayer(null,false,in));
network.addLayer(new BasicLayer(new ActivationLinear(),false,out));
network.getStructure().finalizeStructure();
network.reset();
*/
ADALINEPattern pattern = new ADALINEPattern();
pattern.setInputNeurons(in);
pattern.setOutputNeurons(out);
this.network = (BasicNetwork)pattern.generate();
}
示例9: enquireNeuralNetwork
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
public int enquireNeuralNetwork(final BasicNetwork neuralNetwork, DataPoint dataPoint) {
MLData input = new BasicMLData(2);
input.setData(0, dataPoint.getX());
input.setData(1, dataPoint.getY());
MLData output = neuralNetwork.compute(input);
// Check to see which button has the highest output
double buttons[] = output.getData();
int buttonIndex = -1;
for (int i = 0; i < buttons.length; i++) {
if (buttons[i] > 0 && (buttonIndex == -1 || buttons[i] > buttons[buttonIndex])) {
buttonIndex = i;
}
}
return buttonIndex;
}
示例10: main
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
/**
* The main method.
* @param args No arguments are used.
*/
public static void main(final String args[]) {
// create a neural network, without using a factory
BasicNetwork network = new BasicNetwork();
network.addLayer(new BasicLayer(null,true,2));
network.addLayer(new BasicLayer(new ActivationSigmoid(),true,3));
network.addLayer(new BasicLayer(new ActivationSigmoid(),false,1));
network.getStructure().finalizeStructure();
network.reset();
// create training data
MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
// train the neural network
final ResilientPropagation train = new ResilientPropagation(network, trainingSet);
int epoch = 1;
do {
train.iteration();
System.out.println("Epoch #" + epoch + " Error:" + train.getError());
epoch++;
} while(train.getError() > 0.01);
train.finishTraining();
// test the neural network
System.out.println("Neural Network Results:");
for(MLDataPair pair: trainingSet ) {
final MLData output = network.compute(pair.getInput());
System.out.println(pair.getInput().getData(0) + "," + pair.getInput().getData(1)
+ ", actual=" + output.getData(0) + ",ideal=" + pair.getIdeal().getData(0));
}
Encog.getInstance().shutdown();
}
示例11: Classifier
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
public Classifier(File trainedNetwork, Characteristic characteristic, List<VocabularyWord> vocabulary, NGramStrategy nGramStrategy) {
if (characteristic == null ||
characteristic.getName().equals("") ||
characteristic.getPossibleValues() == null ||
characteristic.getPossibleValues().size() == 0 ||
vocabulary == null ||
vocabulary.size() == 0 ||
nGramStrategy == null) {
throw new IllegalArgumentException();
}
this.characteristic = characteristic;
this.vocabulary = vocabulary;
this.inputLayerSize = vocabulary.size();
this.outputLayerSize = characteristic.getPossibleValues().size();
this.nGramStrategy = nGramStrategy;
if (trainedNetwork == null) {
this.network = createNeuralNetwork();
} else {
// load neural network from file
try {
this.network = (BasicNetwork) loadObject(trainedNetwork);
} catch (PersistError e) {
throw new IllegalArgumentException();
}
}
}
示例12: NeuralAgent
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
public NeuralAgent(NeuralAgent neuralAgent, BasicNetwork brain, EnvironmentMap map) {
this.brain = neuralAgent.brain;
this.setMap(map);
if (neuralAgent.getAlgorithm() != null) {
setAlgorithm(neuralAgent.getAlgorithm().clone());
}
setPosition(neuralAgent.getPosition().getX(), neuralAgent.getPosition().getY());
this.vision = new BasicNeuralData(NeuralConstants.VISION_POINTS);
}
示例13: generateAgent
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
public static NeuralAgent generateAgent(EnvironmentMap map)
{
BasicNetwork network = new BasicNetwork();
network.addLayer(new BasicLayer(NeuralConstants.INPUT_NEURON_COUNT));
network.addLayer(new BasicLayer(60));
network.addLayer(new BasicLayer(30));
network.addLayer(new BasicLayer(NeuralConstants.OUTPUT_NEURON_COUNT));
network.getStructure().finalizeStructure();
network.reset();
NeuralAgent agent = new NeuralAgent(network, map);
return agent;
}
示例14: runMLPRace
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
private void runMLPRace(String mlpFile) {
PersistBasicNetwork persistence = new PersistBasicNetwork();
try {
FileInputStream fis = new FileInputStream(mlpFile);
BasicNetwork nn = (BasicNetwork)persistence.read(fis);
runRace(new EvolvedController(nn));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
示例15: saveToFile
import org.encog.neural.networks.BasicNetwork; //导入依赖的package包/类
private void saveToFile(BasicNetwork network) {
PersistBasicNetwork ps = new PersistBasicNetwork();
try {
FileOutputStream os = new FileOutputStream(Configuration.ENCOG_TRAINED_FILE);
ps.save(os, network);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}