1. 程式人生 > >基於開源JOONE 神經網路例項

基於開源JOONE 神經網路例項

作者:北郵小生-chaosju

1.入門書籍推薦: 人工神經網路教程-韓力群  北京郵電大學出版社

      寫演算法要了解演算法的原理,方能寫出好演算法,明白原理實現演算法 事半功倍

2.Joone

JOONE(Java Object Oriented Neural Network)是sourceforge.net上一個用java語言迅速開發神經網路的開源專案。JOONE支援很多的特性,比如多執行緒和分散式計算,這意味著可以JOONE可以利用多處理器或是多計算機來均衡附載。

JOONE主要有三個大的模組:

      joone-engine:joone的核心模組。
      joone-editor

:joone的gui開發環境。不用編寫一行程式碼就建立神經網路模型,並可以進行訓練和驗證。Joone中提供了一個用joone-editor建立xor網路模型的例子,本文中的神經網路就是參考這個例子所完成的。
      joone-distributed-environment :joone用於支援分散式計算的模組。

文件,原始碼下載地址:http://www.codertodeveloper.com/docs/documentation.html

-----------學會讀一手的東西,直接看文件原始碼

3.XOR(異或)例項的實現


public class XOR_Test implements NeuralNetListener,Serializable {

	private static final long serialVersionUID = -3597853311948127352L;
	private FileInputSynapse inputStream = null;
	private NeuralNet nnet = null;
	public static void main(String args[]) {
		XOR_Test xor = new XOR_Test();

		xor.initNeuralNet();
	}

	protected void initNeuralNet() {

		// 三層:輸入層 ,隱層,輸出層
		// create the three layers (using the sigmoid transfer function for the
		// hidden and the output layers)
		LinearLayer input = new LinearLayer();
		SigmoidLayer hidden = new SigmoidLayer();
		SigmoidLayer output = new SigmoidLayer();

		// set their dimensions(神經元):
		// 設定每層神經元的個數
		input.setRows(2);
		hidden.setRows(3);
		output.setRows(1);

		// Now build the neural net connecting the layers by creating the two
		// synapses(突觸)
		// 三層需要兩個突觸對其進行連線,建立神經元
		FullSynapse synapse_IH = new FullSynapse(); /* Input -> Hidden conn. */
		FullSynapse synapse_HO = new FullSynapse(); /* Hidden -> Output conn. */

		// 連線操作
		// Next connect the input layer with the hidden layer:
		input.addOutputSynapse(synapse_IH);
		hidden.addInputSynapse(synapse_IH);
		// and then, the hidden layer with the output layer:
		hidden.addOutputSynapse(synapse_HO);
		output.addInputSynapse(synapse_HO);

		// need a NeuralNet object that will contain all the Layers of the
		// network
		// 建立一個網路來容納各層
		nnet = new NeuralNet();
		nnet.addLayer(input, NeuralNet.INPUT_LAYER);
		nnet.addLayer(hidden, NeuralNet.HIDDEN_LAYER);
		nnet.addLayer(output, NeuralNet.OUTPUT_LAYER);
		
		
		Monitor monitor = nnet.getMonitor();
		// 設定神經網路的學習率,
		monitor.setLearningRate(0.8);
		// 設定神經網路的動量 為 0.3 這兩個變數與步長有關
		monitor.setMomentum(0.3);
		monitor.addNeuralNetListener(this);
		
		
		
		// 輸入流
		inputStream = new FileInputSynapse();
		/* The first two columns contain the input values */
		inputStream.setAdvancedColumnSelector("1,2");
		/* This is the file that contains the input data */
		inputStream.setInputFile(new File("E:\\joone\\XOR.txt"));
		// Next add the input synapse to the first layer.
		input.addInputSynapse(inputStream);

	
		

		TeachingSynapse trainer = new TeachingSynapse();
		/*
		 * Setting of the file containing the desired responses, provided by a
		 * FileInputSynapse
		 */
		FileInputSynapse samples = new FileInputSynapse();
		samples.setInputFile(new File("e:\\joone\\XOR.txt"));
		/* The output values are on the third column of the file */
		samples.setAdvancedColumnSelector("3");
		trainer.setDesired(samples);
		
		
		output.addOutputSynapse(trainer);
		/* We attach the teacher to the network */
		nnet.setTeacher(trainer);
		
		monitor.setTrainingPatterns(4); /* # of rows in the input file */
		monitor.setTotCicles(100000); /* How many times the net must be trained*/
		
		monitor.setLearning(true); /* The net must be trained */
		nnet.go(); /* The network starts the training phase */

	}


	@Override
	public void netStarted(NeuralNetEvent e) {
		System.out.println("Training...");

	}

	@Override
	public void cicleTerminated(NeuralNetEvent e) {
		Monitor mon = (Monitor)e.getSource();
		long c = mon.getCurrentCicle();
		/* We want print the results every 100 epochs */
		if (c % 100 == 0)
		System.out.println(c + " epochs remaining - RMSE = " +
		mon.getGlobalError());

	}

	@Override
	public void netStopped(NeuralNetEvent e) {
		System.out.println("Training Stopped...");
		long mystr = System.currentTimeMillis(); // 初始化當前的系統時間
		System.out.println(mystr);

		saveNeuralNet("d://xor.snet"); // 儲存生成當前時間的myxor.snet神經網路
		test();

	}
    public void test(){
    	NeuralNet xorNNet = this.restoreNeuralNet("D://xor.snet");
    	if (xorNNet != null) {
    	// we get the output layer
    	Layer output = xorNNet.getOutputLayer();
    	// we create an output synapse
    	FileOutputSynapse fileOutput = new FileOutputSynapse();
    	fileOutput.setFileName("d://xor_out.txt");
    	// we attach the output synapse to the last layer of the NN
    	output.addOutputSynapse(fileOutput);
    	// we run the neural network only once (1 cycle) in recall mode
    	xorNNet.getMonitor().setTotCicles(1);
    	xorNNet.getMonitor().setLearning(false);
    	xorNNet.go();
    	}
    }
	@Override
	public void errorChanged(NeuralNetEvent e) {
		Monitor mon = (Monitor) e.getSource();// 得到監控層的資訊
		long c = mon.getCurrentCicle();
		if (c % 100 == 0)
		System.out.println("Cycle: "
				+ (mon.getTotCicles() - mon.getCurrentCicle()) + " RMSE:"
				+ mon.getGlobalError()); // 輸出 訓練的次數和 rmse 均方誤差

	}
	
	public void saveNeuralNet(String fileName) {
		try {
			FileOutputStream stream = new FileOutputStream(fileName);
			ObjectOutputStream out = new ObjectOutputStream(stream);
			out.writeObject(nnet);// 寫入nnet物件
			out.close();
		} catch (Exception excp) {
			excp.printStackTrace();
		}
	}
	
	NeuralNet restoreNeuralNet(String fileName) {
		NeuralNetLoader loader = new NeuralNetLoader(fileName);
		NeuralNet nnet = loader.getNeuralNet();
		return nnet;
		}
	@Override
	public void netStoppedError(NeuralNetEvent e, String error) {
		// TODO Auto-generated method stub

	}
}

程式的輸出結果:

0.0022572691083591304
0.9972511752900466
0.9972455081943005
0.0037839413733784474


和異或的結果一直,訓練的次數約多,結果越接近-------0.1

相關推薦

基於開源JOONE 神經網路例項

作者:北郵小生-chaosju 1.入門書籍推薦: 人工神經網路教程-韓力群  北京郵電大學出版社       寫演算法要了解演算法的原理,方能寫出好演算法,明白原理實現演算法 事半功倍 2.Joone JOONE(Java Object Oriented Neural

CIKM 18 | 螞蟻金服論文:基於異構圖神經網路的惡意賬戶識別方法

小螞蟻說: ACM CIKM 2018 全稱是 The 27th ACM International Conference on Information and Knowledge Management,會議於2018年10月22日-26日在義大利都靈省舉行。 CIMK 是國際計算機學會(ACM

Tensorpack,一個基於TensorFlow的神經網路訓練介面,原始碼包含很多示例

  Tensorpack是一個基於TensorFlow的神經網路訓練介面。    https://github.com/tensorpack/tensorpack 特徵: 它是另一個TF高階API,具有速度,可讀性和靈活性。

DeepMind 開源神經網路的程式碼

用於支援論文《Relational inductive biases, deep learning, and graph networks》。 github A graph network takes a graph as input and returns a graph as outpu

基於卷積神經網路的貓種類的識別

1 引言 我也是剛剛接觸卷積神經網路不久,理解的也不是特別深入,以下都是我自己的個人想法,如有錯誤希望大家指正。 這個是一個基於類似於VGG模型(模型來源於keras文件)的卷積神經網路實現的圖片分類器,實現的是對貓的種類識別,因為資料集比較難找所以只用了320張圖片做訓練集,用

基於OpenCV 人工神經網路的噴碼字元識別(C++)

新手上路記本人做的第一個影象處理實驗 噴碼字元識別流程:讀圖——濾波——二值化——腐蝕——分割——ANN訓練——識別 本例程訓練類別14類(0-9,C、L、冒號、空格),每類樣本數量50張 搭配環境:VS2017+opencv3.4.1 語言:C++ 由於工程有點大,下面進行簡單介紹,詳情見

基於卷積神經網路特徵圖的二值影象分割

       目標檢測是當前大火的一個研究方向,FasterRCNN、Yolov3等一系列結構也都在多目標檢測的各種應用場景或者競賽中取得了很不錯的成績。但是想象一下,假設我們需要通過影象檢測某個產品上是否存在缺陷,或者通過衛星圖判斷某片海域是否有某公司的船隻

基於長短期記憶神經網路LSTM的多步長時間序列預測

        基於長短期記憶神經網路LSTM的多步長多變數時間序列預測      長短時記憶網路(LSTM)是一種能夠學習和預測長序列的遞迴神經網路。LSTMs除了學習長序列外,還可以學習一次多步預測,這對於時間序列的預測非

cs231n斯坦福基於卷積神經網路的CV學習筆記(二)神經網路訓練細節

五,神經網路 注意點part1 例項:邏輯迴歸二層神經網路訓練函式 使用權重w和偏差值biase計算出第一個隱含層h,然後計算損失,評分,進行反向傳播回去 多種常用啟用函式(一般預設max(0,x)),如sigmoid函式具有飽和區梯度0,非零點中心,計算x複

基於卷積神經網路電視節目推薦

本文使用文字卷積神經網路,並使用自己的電視節目資料集完成電影推薦的任務。  需要安裝TensorFlow1.0,Python3.5  文字卷積神經網路的圖如下,    圖片來自Kim Yoon的論文:Convolutional Neural Net

神經網路例項及錯誤筆記

第一個例子是P=[1 2;2 3;3 4;4 5],T=[3 3;4 4;5 5;6 6]。 問題來了,第一次用nntool建立,錯誤是  insufficient number of outputs from right hand side of equal sign

06《基於卷積神經網路LeNet-5的車牌字元識別研究》學習總結

一、本篇介紹 二、本文主要內容(知識點) 1、概要 2、卷積神經網路介紹 1.卷積層 2.次抽樣層 3、LeNet-5介

基於卷積神經網路實現圖片風格的遷移 3

實現圖片的風格轉換 一、實驗介紹 1.1 實驗內容 上一節課我們介紹了經典的CNN模型 VGG ,以及影象風格遷移演算法的基本原理。本節課我們將使用另外一個經典的模型 GoogLenet 來實現我們的專案(這是由於環境的限制,用 googlenet可以更快的完成我們的風格轉換),如果你完成了上節課的作業,那

論文理解:基於卷積神經網路的人臉識別方法

本文是對陳耀丹、王連明的基於卷積神經網路的人臉識別方法的理解。 摘要:實現了一種基於卷積神經網路的人臉識別方法,該網路由兩個卷積層,兩個池化層、一個全連線層和一個softmax迴歸層組成,它能自動提取人臉特徵並進行分類,網路通過批量梯度下降法訓練特徵提取器和分

基於卷積神經網路的近紅外夜間道路行人識別

near infrared nighttime road pedestrians recognition based on convolutional neural network 題目:基於卷積神經網路的近紅外夜間道路行人識別 摘要:在行人預測系統中,行人識別是行人檢測的核心技術。這篇文章

基於卷積神經網路和tensorflow實現的人臉識別

以前在學習卷積神經網路的時候,發現了很多很有趣的demo,有一次發現了上面這個人臉識別的例子,不過當時還看不懂,經過一段時間之後決定試試能不能將上面的例子改一下,調以調參什麼的,於是就有了這篇文章。本以為我的程式碼和原文沒有什麼太大的區別,應該不會出現什麼錯誤,但是實際自己上

斯坦福大學2017年春季_基於卷積神經網路的視覺識別課程視訊教程及ppt分享

計算機視覺已經在我們的社會中普遍存在,應用於搜尋,影象理解,應用程式,繪圖,醫學,無人機和自駕車。許多這些應用程式的核心是視覺識別任務,如影象分類,定位和檢測。神經網路(又稱“深度學習”)方法的最新進展,大大提高了這些最先進的視覺識別系統的效能。 本課程深入瞭解深入

基於卷積神經網路(CNN)的中文垃圾郵件檢測

前言 文字分類任務是一個經久不衰的課題,其應用包括垃圾郵件檢測、情感分析等。 傳統機器學習的做法是先進行特徵工程,構建出特徵向量後,再將特徵向量輸入各種分類模型(貝葉斯、SVM、神經網路等)進行分類。 隨著深度學習的發展以及RNN、CNN的陸續出現,特

基於卷積神經網路實現圖片風格的遷移 2

VGG與風格遷移演算法原理 一、實驗介紹 1.1 實驗內容 上節課我們學習了卷積神經網路的基本原理,本節實驗我們將學習用於影象風格遷移的經典的卷積神經網路模型VGG,並用caffe提供的 draw_net.py 實現模型的視覺化,本節實驗我們也將學習影象風格轉換的演算法原理。 1.2 實驗知識點 經典CNN

基於卷積神經網路實現圖片風格的遷移 1

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!