view trunk/perceptron_test.d @ 2:9655c8362b25

Added the Perceptron class and the perceptron_test testing program.
author revcompgeek
date Sat, 05 Apr 2008 23:41:30 -0600
parents
children
line wrap: on
line source

module perceptron_test;

import aid.nn.perceptron;
import std.stdio;
import std.string;
import std.conv;
import std.math;

void main(char[][] args){
	perceptron nn;
	double[][] inputs = [[ 0, 0],
					     [ 0, 1],
					     [ 1, 0],
					     [ 1, 1]];
	double[] outputs = [ 0, 0, 0, 1];
	double learningRate = 0.3;
	int numReps = 20;

	if(args.length > 1) learningRate = toDouble(args[1]);
	if(args.length > 2) numReps = toInt(args[2]);
	
	nn = new perceptron(2,learningRate,true,null);

	int iter = 0;
	double[] weights;
	double t,difference;
	while(iter <= numReps){
		weights=nn.getWeights();
		writefln("[ %f, %f, %f ]",weights[0],weights[1],weights[2]);
		//Evaluate
		for(int i = 0; i < 4; i++){
			t = nn.evaluate(inputs[i]);
			writefln("  D: %f %f",t,outputs[i]);
			difference = abs(t-outputs[i]);
		}
		writefln("%d: Total Difference: %f",iter,difference);
		// End evaluate

		// Train
		for(int i = 0; i < 4; i++){
			nn.train(inputs[i],outputs[i]);
		}
		writefln("%d: Error Value: %f",iter,nn.getErrorValue(inputs,outputs));
		writefln();
		
		iter++;
		//if(iter > 20) break;
	}
	weights=nn.getWeights();
	writefln("[ %f, %f, %f ]",weights[0],weights[1],weights[2]);
}