import java.util.Arrays;
import org.junit.Test;
NeuralNetwork;
data.DataSet;
data.DataSetRow;
vents.LearningEvent;
vents.LearningEventListener;
learning.SupervisedLearning;
MultiLayerPerceptron;
learning.MomentumBackpropagation;public class IdentityAnimal extends NeuralNetwork implements LearningEventListener{@Testpublic void excute() {//定义animals训练数据String trainingSetFileName = "./src/";//定义两个变量分别为输入层神经元个数20,输出层神经元个数7个int inputsCount = 20;int outputsCount = 7;//取出并里训练数据集DataSet dataSet = ateFromFile(trainingSetFileName, inputsCount, outputsCount, "t", true);System.out.println("Creating ");//建立神经网络,定义一个隐层,神经元设为22MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(inputsCount, 22, outputsCount);//定义学习规则、BPMomentumBackpropagation learningRule = (MomentumBackpropagation) LearningRule();learningRule.addListener(this);//设置最大误差、学习速度learningRule.setLearningRate(0.2);learningRule.setMaxError(0.01);System.out.println("");//开始训练neuralNet.learn(dataSet);System.out.println("Training completed.");System.out.println("");testNeuralNetwork(neuralNet, dataSet);}public static void testNeuralNetwork(NeuralNetwork<?> nnet, DataSet test) {for (DataSetRow dataRow : Rows()) {nnet.Input());nnet.calculate();double[] networkOutput = Output();System.out.print("Input: " + Input()));System.out.println(" Output: " + String(networkOutput));}}@Overridepublic void handleLearningEvent(LearningEvent event) {SupervisedLearning bp = (SupervisedLearning) Source();if (EventType() != LearningEvent.Type.LEARNING_STOPPED) {System.out.CurrentIteration() + ". iteration : " + bp.getTotalNetworkError());}}}
hair feathers eggs milk airborne aquatic predator toothed backbone bretahes venomous fins legs tail domestic catsize class1 class2 class3 class4 class5 class6 class7 1 0 0 1 0 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 1 0 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 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0 0 1 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0
代码有了来说自己写的时候的坑,最初的时候IdentityAnimal这个类的问题不是很大,这个类最主要的问题就是DataSet dataSet = ateFromFile(trainingSetFileName, inputsCount, outputsCount, "t", true);
这就了,首先,要了解这几个参数的意义,最好的办法就是去源码里看源码了,这部分源码很简单,说主要的“t”
是分割符,学会变通,没必要按他的我最开始用的是,
,最后一个参数,源码里说如果是csv格式的文件是否加载列名,这个格式说白了就是个特殊的文本,记事本和Excel都可以直接打开,下边是源码
一开始按我自己所想设置的TXT文本内容,执行后的结果如下
好吧,其实最开始执行时一直输出149528. iteration : NAN
来着,另外吴老师文本第三组数据的输出中有2个1,怎么感觉是写错了,最后的7位应该只有一个1才对吧
最后课本上写明的jar包版本是2.7,但是我在2.7API文档里发现没有重写五个参数的createFromFile()方法,另外DataSet类的包路径也变了,注意自己,下边是2.7版本的代码,注意这个版本的文本内容部允许有列名,估计是不支持CSV格式
import java.util.Arrays;
import org.junit.Test;
NeuralNetwork;
vents.LearningEvent;
vents.LearningEventListener;
learning.DataSet;
learning.DataSetRow;
learning.SupervisedLearning;
MultiLayerPerceptron;
learning.MomentumBackpropagation;public class IdentityAnimal extends NeuralNetwork implements LearningEventListener{@Testpublic void excute() {//定义animals训练数据String trainingSetFileName = "./src/";//定义两个变量分别为输入层神经元个数20,输出层神经元个数7个int inputsCount = 20;int outputsCount = 7;//取出并里训练数据集(这里包路径变了,没有重写5个参数的方法)DataSet dataSet = ateFromFile(trainingSetFileName, inputsCount, outputsCount, "t");System.out.println("Creating ");//建立神经网络,定义一个隐层,神经元设为22MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(inputsCount, 22, outputsCount);//定义学习规则、BPMomentumBackpropagation learningRule = (MomentumBackpropagation) LearningRule();learningRule.addListener(this);//设置最大误差、学习速度learningRule.setLearningRate(0.2);learningRule.setMaxError(0.01);System.out.println("");//开始训练neuralNet.learn(dataSet);System.out.println("Training completed.");System.out.println("");testNeuralNetwork(neuralNet, dataSet);}public static void testNeuralNetwork(NeuralNetwork nnet, DataSet test) {for (DataSetRow dataRow : Rows()) {nnet.Input());nnet.calculate();double[] networkOutput = Output();System.out.print("Input: " + Input()));System.out.println(" Output: " + String(networkOutput));}}@Overridepublic void handleLearningEvent(LearningEvent event) {SupervisedLearning bp = (SupervisedLearning) Source();System.out.CurrentIteration() + ". iteration : " + bp.getTotalNetworkError());}}
本文发布于:2024-02-02 16:17:16,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170686183344957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |