JOONE 인공지능 인 스 턴 스 구현

4351 단어 자바예시Joone
	package com.sweetop.test;
	import java.io.Serializable;
	import java.util.Arrays;
	import java.util.Vector;
	import org.joone.engine.FullSynapse;
	import org.joone.engine.LinearLayer;
	import org.joone.engine.Monitor;
	import org.joone.engine.NeuralNetEvent;
	import org.joone.engine.NeuralNetListener;
	import org.joone.engine.Pattern;
	import org.joone.engine.SigmoidLayer;
	import org.joone.engine.learning.TeachingSynapse;
	import org.joone.io.FileInputSynapse;
	import org.joone.io.FileOutputSynapse;
	import org.joone.io.MemoryInputSynapse;
	import org.joone.io.MemoryOutputSynapse;
	import org.joone.net.NeuralNet;
	
	public class Test implements NeuralNetListener,Serializable {
		/**
		 * 
		 */
		private static final long serialVersionUID = 61750666816215273L;
		private NeuralNet nnet = null;
		private MemoryInputSynapse inputSynapse, desiredOutputSynapse;
		private MemoryOutputSynapse outputSynapse;
		// XOR input
		private double[][] inputArray = new double[][] { { 1.0, 1.0 },
				{ 1.0, 2.0 }, { 2.0, 2.0 }, { 2.0, 3.0 }, { 1.0, 3.0 } };
		// XOR desired output
		private double[][] desiredOutputArray = new double[][] { {  2.0/10 },
				{ 3.0/10 }, { 4.0/10 }, { 5.0/10 }, {  4.0/10 } };
		protected void initNeuralNet() {
			// First create the three layers
			LinearLayer input = new LinearLayer();
			SigmoidLayer hidden = new SigmoidLayer();
			SigmoidLayer output = new SigmoidLayer();
			input.setLayerName("input");
			hidden.setLayerName("hidden");
			output.setLayerName("output");
			// set the dimensions of the layers
			input.setRows(2);
			hidden.setRows(3);
			output.setRows(1);
			// Now create the two Synapses
			FullSynapse synapse_IH = new FullSynapse(); /* input -> hidden conn. */
			FullSynapse synapse_HO = new FullSynapse(); /* hidden -> output conn. */
			// Connect the input layer whit the hidden layer
			input.addOutputSynapse(synapse_IH);
			hidden.addInputSynapse(synapse_IH);
			// Connect the hidden layer whit the output layer
			hidden.addOutputSynapse(synapse_HO);
			output.addInputSynapse(synapse_HO);
			// the input to the neural net
			inputSynapse = new MemoryInputSynapse();
			input.addInputSynapse(inputSynapse);
			// the output of the neural net
			outputSynapse = new MemoryOutputSynapse();
			output.addOutputSynapse(outputSynapse);
			// The Trainer and its desired output
			desiredOutputSynapse = new MemoryInputSynapse();
			TeachingSynapse trainer = new TeachingSynapse();
			trainer.setDesired(desiredOutputSynapse);
			nnet = new NeuralNet();
			nnet.addLayer(input, NeuralNet.INPUT_LAYER);
			nnet.addLayer(hidden, NeuralNet.HIDDEN_LAYER);
			nnet.addLayer(output, NeuralNet.OUTPUT_LAYER);
			nnet.setTeacher(trainer);
			output.addOutputSynapse(trainer);
			train();
			//File
		}
		public void test(double[][] d){
			inputSynapse.setInputArray(d);
			inputSynapse.setAdvancedColumnSelector("1,2");
			nnet.getMonitor().setTotCicles(1);
			nnet.start();
			nnet.getMonitor().Go();
			for (Object o : outputSynapse.getAllPatterns()) {
				Pattern p = (Pattern) o;
				System.out.println(10*p.getArray()[0]);
			}       
		}
		public void train() {
			// set the inputs
			inputSynapse.setInputArray(inputArray);
			inputSynapse.setAdvancedColumnSelector("1,2");
			// set the desired outputs
			desiredOutputSynapse.setInputArray(desiredOutputArray);
			desiredOutputSynapse.setAdvancedColumnSelector("1");
			// get the monitor object to train or feed forward
			Monitor monitor = nnet.getMonitor();
			// set the monitor parameters
			monitor.setLearningRate(0.8);
			monitor.setMomentum(0.3);
			monitor.setTrainingPatterns(inputArray.length);
			monitor.setTotCicles(100000);
			monitor.setLearning(true);
			nnet.addNeuralNetListener(this);
			nnet.start();
			nnet.getMonitor().Go();
			for (Object o : outputSynapse.getAllPatterns()) {
				Pattern p = (Pattern) o;
				System.out.println(10*p.getArray()[0]);
			}
		}
		
		public void cicleTerminated(NeuralNetEvent e) {
			
		}
		public void errorChanged(NeuralNetEvent e) {
		}
		public void netStarted(NeuralNetEvent e) {
			// TODO Auto-generated method stub
		}
		public void netStopped(NeuralNetEvent e) {
			// TODO Auto-generated method stub
		}
		public void netStoppedError(NeuralNetEvent e, String error) {
		}
	}

좋은 웹페이지 즐겨찾기