吳岸城老師神經網絡識別動物感知機的實現

  1. 首先課本中都是相關的代碼片段,而且因爲時間問題還有API的變動,感覺都爲學習者的代碼實現帶來了很大的難度,即使是配合源碼有時候還是會有很多問題,浪費了不少時間,最後還是把吳老師的GitHub倉庫直接下載到本地去參考後才運行出來的,下邊直接粘貼代碼,代碼下邊是自己根據代碼片段和源碼去實現的過程中遇到的坑,估計會很囉嗦,另外自己變通一下,比如文本文件的位置,注意細節
  2. 最近總碰上Markdown列表後的代碼段格式錯誤問題,我看百度上也很多問的,看着比較亂,先這麼湊合着吧,跟着序號走就好了
  3. IdentityAnimal
import java.util.Arrays;
import org.junit.Test;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.core.events.LearningEvent;
import org.neuroph.core.events.LearningEventListener;
import org.neuroph.core.learning.SupervisedLearning;
import org.neuroph.nnet.MultiLayerPerceptron;
import org.neuroph.nnet.learning.MomentumBackpropagation;

public class IdentityAnimal extends NeuralNetwork implements LearningEventListener{

	@Test
	public void excute() {
		//定義animals訓練數據
		String trainingSetFileName = "./src/animals_data.txt";
		//定義兩個變量分別爲輸入層神經元個數20,輸出層神經元個數7個
		int inputsCount = 20;
		int outputsCount = 7;
		
		//取出並裏訓練數據集
		DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, "\t", true);
		
		System.out.println("Creating neural network...");
		//建立神經網絡,定義一個隱層,神經元設爲22
		MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(inputsCount, 22, outputsCount);
		
		//定義學習規則、BP
		MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
		learningRule.addListener(this);
		
		//設置最大誤差、學習速度
		learningRule.setLearningRate(0.2);
		learningRule.setMaxError(0.01);
		
		System.out.println("Traning network...");
		//開始訓練
		neuralNet.learn(dataSet);
		
		System.out.println("Training completed.");
		System.out.println("Testing network...");
		testNeuralNetwork(neuralNet, dataSet);
	}
	
	public static void testNeuralNetwork(NeuralNetwork<?> nnet, DataSet test) {

		for (DataSetRow dataRow : test.getRows()) {

			nnet.setInput(dataRow.getInput());
			nnet.calculate();
			double[] networkOutput = nnet.getOutput();
			System.out.print("Input: " + Arrays.toString(dataRow.getInput()));
			System.out.println(" Output: " + Arrays.toString(networkOutput));
		}
	}

	@Override
	public void handleLearningEvent(LearningEvent event) {
		SupervisedLearning bp = (SupervisedLearning) event.getSource();
		if (event.getEventType() != LearningEvent.Type.LEARNING_STOPPED) {
			System.out.println(bp.getCurrentIteration() + ". iteration : " + bp.getTotalNetworkError());
		}
	}
	
}
  1. animals_data.txt
    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 0 0 0 0 0 0 1 1 0 0 1 0 0 1 1 1 1 0 0 0 0 1 0 0 1 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 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 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 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 1 0 0 1 1 1 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1 0 1 1 0 1 0 0 0 1 1 0 0 0 0 0 1 0 1 1 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1 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 1 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 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 0 0 0 0 0 0 0 1 0 1 1 0 1 0 1 0 1 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1 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 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

  2. 代碼有了來說自己寫的時候的坑,最初的時候IdentityAnimal這個類的問題不是很大,這個類最主要的問題就是DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, "\t", true);這就了,首先,要了解這幾個參數的意義,最好的辦法就是去源碼裏看源碼了,這部分源碼很簡單,說主要的“\t”是分割符,學會變通,沒必要按他的我最開始用的是,最後一個參數,源碼裏說如果是csv格式的文件是否加載列名,這個格式說白了就是個特殊的文本,記事本和Excel都可以直接打開,下邊是源碼
    在這裏插入圖片描述
    在這裏插入圖片描述

  3. 一開始按我自己所想設置的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;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.events.LearningEvent;
import org.neuroph.core.events.LearningEventListener;
import org.neuroph.core.learning.DataSet;
import org.neuroph.core.learning.DataSetRow;
import org.neuroph.core.learning.SupervisedLearning;
import org.neuroph.nnet.MultiLayerPerceptron;
import org.neuroph.nnet.learning.MomentumBackpropagation;

public class IdentityAnimal extends NeuralNetwork implements LearningEventListener{

	@Test
	public void excute() {
		//定義animals訓練數據
		String trainingSetFileName = "./src/animals_data.txt";
		//定義兩個變量分別爲輸入層神經元個數20,輸出層神經元個數7個
		int inputsCount = 20;
		int outputsCount = 7;
		
		//取出並裏訓練數據集(這裏包路徑變了,沒有重寫5個參數的方法)
		DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, "\t");
		
		System.out.println("Creating neural network...");
		//建立神經網絡,定義一個隱層,神經元設爲22
		MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(inputsCount, 22, outputsCount);
		
		//定義學習規則、BP
		MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
		learningRule.addListener(this);
		
		//設置最大誤差、學習速度
		learningRule.setLearningRate(0.2);
		learningRule.setMaxError(0.01);
		
		System.out.println("Traning network...");
		//開始訓練
		neuralNet.learn(dataSet);
		
		System.out.println("Training completed.");
		System.out.println("Testing network...");
		testNeuralNetwork(neuralNet, dataSet);
	}
	
	public static void testNeuralNetwork(NeuralNetwork nnet, DataSet test) {

		for (DataSetRow dataRow : test.getRows()) {

			nnet.setInput(dataRow.getInput());
			nnet.calculate();
			double[] networkOutput = nnet.getOutput();
			System.out.print("Input: " + Arrays.toString(dataRow.getInput()));
			System.out.println(" Output: " + Arrays.toString(networkOutput));
		}
	}

	@Override
	public void handleLearningEvent(LearningEvent event) {
		SupervisedLearning bp = (SupervisedLearning) event.getSource();
		System.out.println(bp.getCurrentIteration() + ". iteration : " + bp.getTotalNetworkError());
	}
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章