我的输出很糟糕。实际不等于理想。哪些代码有误?
我的输出:
Epoch #129 Error:8.755514431853456E-6
Neural Network Results:
0.0,0.0, actual=0.57600,ideal=0.0
1.0,0.0, actual=0.58016,ideal=1.0
0.0,1.0, actual=0.58886,ideal=1.0
1.0,1.0, actual=0.59317,ideal=0.0
这是我的代码:
public class XOR {
public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 } };
/**
* The ideal data necessary for XOR.
*/
public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } };
//public static BasicNetwork network = new BasicNetwork();
public static BasicNetwork createNetwork()
{
// 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();
return network;
}
/**
* The main method.
* @param args No arguments are used.
*/
public static void main(final String args[]) {
BasicNetwork network = createNetwork();
MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
// train the neural network
CalculateScore score = new TrainingSetScore(trainingSet);
MLTrain train = new MLMethodGeneticAlgorithm(new MethodFactory(){
@Override
public MLMethod factor() {
final BasicNetwork result = createNetwork();
((MLResettable)result).reset();
return result;
}}, score, 500);
int epoch = 1;
do {
train.iteration();;
System.out
.println("Epoch #" + epoch + " Error:" + train.getError());
epoch++;
} while( train.getError() > 0.01);
// 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));
}
}
}
答案 0 :(得分:1)
遗传算法培训师与Encog中的其他培训师略有不同。它正在构建一群神经网络。您传入的神经网络只是一个模板,其中包含您拥有的隐藏层数以及输入/输出层的外观。这个模板网络实际上并没有通过培训来修改,只是人口。完成训练后,您需要获得人口中的顶级神经网络。有几种方法可以做到这一点,但最简单的方法就是调用train.getMethod()。将以下行添加到您的代码中它将起作用:
} while( train.getError() > 0.01);
network = (BasicNetwork)train.getMethod(); // Add this
// test the neural network