Introduction to Neural Networks for C#, Session 8

Course NameIntroduction to Neural Networks for C#
Instructorjeffheaton
Session TitleDiscuss MidTerm
Session Number8

Session Material

I present here my solution for the mid-term. You can use the link below to download a Visual Studio project for my solution.

[My Solution for Mid-Term]

Claudia Toll Free: 1-800-962-7798 Local: 702-384-2211 Address: 800 South 4th Street Las Vegas, NV. 89101 http://www.aspecialmemory.com/?s=vegascom April 10, 2009, 11:30 MGM Call with room number 314-960-7482 636-405-7482

Analyze.cs: Used to calculate the min/max values for most of the inputs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeatonResearchNeural.Util;

namespace Program1
{
    public class Analyze
    {
        public static String[] COVER_TYPES = {
	    "Spruce/Fir", 			// 1
	    "Lodgepole Pine", 		// 2
	    "Ponderosa Pine", 		// 3
	    "Cottonwood/Willow", 	// 4
	    "Aspen", 				// 5
	    "Douglas-fir", 			// 6
	    "Krummholz" 			// 7
	};

        private double maxElevation;
        private double minElevation;
        private double maxAspect;
        private double minAspect;
        private double maxSlope;
        private double minSlope;
        private double maxHWater;
        private double minHWater;
        private double maxVWater;
        private double minVWater;
        private double maxRoad;
        private double minRoad;
        private double maxShade9;
        private double minShade9;
        private double maxShade12;
        private double minShade12;
        private double maxShade3;
        private double minShade3;

        private int[] coverCount;
        private int count;

        public void analyze(String filename)
        {
            this.count = 0;
            this.maxElevation = Double.MinValue;
            this.minElevation = Double.MaxValue;
            this.maxAspect = Double.MinValue;
            this.minAspect = Double.MaxValue;
            this.maxSlope = Double.MinValue;
            this.minSlope = Double.MaxValue;
            this.maxHWater = Double.MinValue;
            this.minHWater = Double.MaxValue;
            ; this.maxVWater = -1000;//Double.MIN_VALUE;
            this.minVWater = Double.MaxValue;
            this.maxShade9 = Double.MinValue;
            this.minShade9 = Double.MaxValue;
            this.maxShade12 = Double.MinValue;
            this.minShade12 = Double.MaxValue;
            this.maxShade3 = Double.MinValue;
            this.minShade3 = Double.MaxValue;
            this.maxRoad = Double.MinValue;
            this.minRoad = Double.MaxValue;

            this.coverCount = new int[Analyze.COVER_TYPES.Length];

            Console.WriteLine("Please wait, performing initial analysis of data.");
            ReadCSV csv = new ReadCSV(filename, false);
            while (csv.Next())
            {


                double elevation = Double.Parse(csv.Get(0));
                double aspect = Double.Parse(csv.Get(1));
                double slope = Double.Parse(csv.Get(2));
                double hWater = Double.Parse(csv.Get(3));
                double vWater = Double.Parse(csv.Get(4));
                double road = Double.Parse(csv.Get(5));
                double shade9 = Double.Parse(csv.Get(6));
                double shade12 = Double.Parse(csv.Get(7));
                double shade3 = Double.Parse(csv.Get(8));
                int cover = int.Parse(csv.Get(54));
                maxElevation = Math.Max(maxElevation, elevation);
                minElevation = Math.Min(minElevation, elevation);
                maxAspect = Math.Max(maxAspect, aspect);
                minAspect = Math.Min(minAspect, aspect);
                maxSlope = Math.Max(maxSlope, slope);
                minSlope = Math.Min(minSlope, slope);
                maxHWater = Math.Max(maxHWater, hWater);
                minHWater = Math.Min(minHWater, hWater);
                maxVWater = Math.Max(maxVWater, vWater);
                minVWater = Math.Min(minVWater, vWater);
                maxRoad = Math.Max(maxRoad, road);
                minRoad = Math.Min(minRoad, road);
                maxShade9 = Math.Max(maxShade9, shade9);
                minShade9 = Math.Min(minShade9, shade9);
                maxShade12 = Math.Max(maxShade12, shade12);
                minShade12 = Math.Min(minShade12, shade12);
                maxShade3 = Math.Max(maxShade3, shade3);
                minShade3 = Math.Min(minShade3, shade3);
                this.coverCount[cover - 1]++;
                count++;

            }

            Console.WriteLine("Record count: " + count);
            Console.WriteLine("Elevation, Max: " + maxElevation);
            Console.WriteLine("Elevation, Min: " + minElevation);
            Console.WriteLine("Aspect, Max: " + maxAspect);
            Console.WriteLine("Aspect, Min: " + minAspect);
            Console.WriteLine("Slope, Max: " + maxSlope);
            Console.WriteLine("Slope, Min: " + minSlope);
            Console.WriteLine("H. Water, Max: " + maxHWater);
            Console.WriteLine("H. Water, Min: " + minHWater);
            Console.WriteLine("V. Water, Max: " + maxVWater);
            Console.WriteLine("V. Water, Min: " + minVWater);

            Console.WriteLine("Cover breakdown:");
            for (int i = 0; i < Analyze.COVER_TYPES.Length; i++)
            {
                Console.WriteLine(Analyze.COVER_TYPES[i] + ": " + this.coverCount[i]);
            }
        }

        public double getMaxElevation()
        {
            return maxElevation;
        }

        public double getMinElevation()
        {
            return minElevation;
        }

        public double getMaxAspect()
        {
            return maxAspect;
        }

        public double getMinAspect()
        {
            return minAspect;
        }

        public double getMaxSlope()
        {
            return maxSlope;
        }

        public double getMinSlope()
        {
            return minSlope;
        }

        public int[] getCoverCount()
        {
            return coverCount;
        }

        public int getCount()
        {
            return count;
        }

        public double getMaxHWater()
        {
            return maxHWater;
        }

        public double getMinHWater()
        {
            return minHWater;
        }

        public double getMaxVWater()
        {
            return maxVWater;
        }

        public double getMinVWater()
        {
            return minVWater;
        }

        public double getMaxRoad()
        {
            return maxRoad;
        }

        public double getMaxShade9()
        {
            return maxShade9;
        }

        public double getMaxShade12()
        {
            return maxShade12;
        }

        public double getMaxShade3()
        {
            return maxShade3;
        }

        public double getMinRoad()
        {
            return minRoad;
        }

        public double getMinShade9()
        {
            return minShade9;
        }

        public double getMinShade12()
        {
            return minShade12;
        }

        public double getMinShade3()
        {
            return minShade3;
        }

    }
}

Config.cs: Used to hold config information for the program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program1
{
    public class Config
    {
        public const string FILENAME = "c:\\data\\covtype.data";
	    public const int TRAINING_SIZE = 1000;
	    public const int HIDDEN_LAYER_1 = 13;
	    public const int HIDDEN_LAYER_2 = 0;
	    public const double LEARNING_RATE = 0.001;
	    public const double LEARNING_MOMENTUM = 0.3;
	    public const double TRUE = 1.0;
	    public const double FALSE = 0.0;
	    public const int EPOCHS = 5000;
	    public const int COVER_SAMPLES = 2000;
    }
}

Convert.cs: Used to convert the file input data into a form useful for the neural network, using percents.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeatonResearchNeural.Util;

namespace Program1
{
    public class Convert
    {

        public static double percent(double value, double max, double min)
        {
            return (value - min) / (max - min);
        }

        public static void convert(Analyze analyze, ReadCSV line, double[] input, double[] ideal)
        {
            double elevation = Double.Parse(line.Get(0));
            double aspect = Double.Parse(line.Get(1));
            double slope = Double.Parse(line.Get(2));
            double hWater = Double.Parse(line.Get(3));
            double vWater = Double.Parse(line.Get(4));
            double road = Double.Parse(line.Get(5));
            double shade9 = Double.Parse(line.Get(6));
            double shade12 = Double.Parse(line.Get(7));
            double shade3 = Double.Parse(line.Get(8));
            int cover = int.Parse(line.Get(54));

            // now build a training input
            input[0] = percent(elevation, analyze.getMaxElevation(), analyze.getMinElevation());
            input[1] = percent(aspect, analyze.getMaxAspect(), analyze.getMinAspect());
            input[2] = percent(slope, analyze.getMaxSlope(), analyze.getMinSlope());
            input[3] = percent(hWater, analyze.getMaxHWater(), analyze.getMinHWater());
            input[4] = percent(vWater, analyze.getMaxVWater(), analyze.getMinVWater());
            input[5] = percent(road, analyze.getMaxRoad(), analyze.getMinRoad());
            input[6] = percent(shade9, analyze.getMaxShade9(), analyze.getMinShade9());
            input[7] = percent(shade12, analyze.getMaxShade12(), analyze.getMinShade12());
            input[8] = percent(shade3, analyze.getMaxShade3(), analyze.getMinShade3());

            if (ideal != null)
            {
                for (int i = 0; i < Analyze.COVER_TYPES.Length; i++)
                    ideal[i] = Config.FALSE;

                ideal[cover - 1] = Config.TRUE;
            }
        }
    }
}

Evaluate.cs: Used to evaluate the trained neural network with data that it was not trained with.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeatonResearchNeural.Util;
using HeatonResearchNeural.Feedforward;

namespace Program1
{
    public class Evaluate
    {
        
	private int correct;
	private int incorrect;

	public double percent(double value, double max, double min) {
		return (value - min) / (max - min);
	}

	public int maxIndex(double[] d) {
		int result = 0;
		for (int i = 0; i < d.Length; i++) {
			if (d[i] > d[result])
				result = i;
		}
		return result;
	}

	public void evaluate(FeedforwardNetwork network, String filename,
			Analyze analyze)  {

		double[] input = new double[9];

		this.correct = 0;
		this.incorrect = 0;

		int count = 0;
		ReadCSV csv = new ReadCSV(filename, false);
		
		Console.WriteLine("Please wait, neural network is being evaluated.");

		while (csv.Next()) {

			count++;
			
			Convert.convert(analyze, csv, input, null);

			
			int cover = int.Parse(csv.Get(54));
			cover--;
			double[] output = network.ComputeOutputs(input);
			int neuralCover = maxIndex(output);

			// System.out.println("Actual=" + cover+",Neural="+neuralCover);

			if (cover == neuralCover)
				correct++;
			else
				incorrect++;
		}

		Console.WriteLine("Correct:" + correct);
		Console.WriteLine("Incorrect:" + incorrect);
        Console.WriteLine((double)correct / ((double)correct
				+ (double) incorrect));
	}
    }
}

MidTerm.cs: Main entry point for the program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeatonResearchNeural.Feedforward;

namespace Program1
{
    class MidTerm
    {

        static void Main(string[] args)
        {
            Analyze analyze = new Analyze();
            analyze.analyze(Config.FILENAME);
            Train train = new Train();
            train.build(Config.FILENAME, analyze);
            FeedforwardNetwork network = train.train();
            Evaluate evaluate = new Evaluate();
            evaluate.evaluate(network, Config.FILENAME, analyze);
        }
    }
}

Train.cs: Used to train the neural net.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeatonResearchNeural.Util;
using HeatonResearchNeural.Feedforward;
using HeatonResearchNeural.Feedforward.Train.Backpropagation;

namespace Program1
{
    public class Train
    {
        
	double[][] training;
	double[][] ideal;
	int [] coverCount;

	double percent(double value, double max, double min) {
		return (value - min) / (max - min);
	}

	public void build(String filename, Analyze analyze)  {
		int trainingSize = Config.COVER_SAMPLES * Analyze.COVER_TYPES.Length;
		this.training = new double[Config.TRAINING_SIZE][];//9
		this.ideal = new double[Config.TRAINING_SIZE][];// Analyze.COVER_TYPES.length
		this.coverCount = new int[Analyze.COVER_TYPES.Length];
		
		
		int skip = (analyze.getCount() / Config.TRAINING_SIZE) - 1;
		int trainingIndex = 0;
		ReadCSV csv = new ReadCSV(filename, false);
		Console.WriteLine("Generating training set, skip = " + skip);
		
		while (csv.Next() && (trainingIndex < Config.TRAINING_SIZE)) {

			int cover = int.Parse(csv.Get(54))-1;
			
			if( coverCount[cover]<Config.COVER_SAMPLES )
			{
                training[trainingIndex] = new double[9];
                ideal[trainingIndex] = new double[Analyze.COVER_TYPES.Length];
				Convert.convert(analyze, csv, training[trainingIndex],
					ideal[trainingIndex]);
				coverCount[cover]++;
				trainingIndex++;
			}			
		}
	}

	public FeedforwardNetwork train() {
		FeedforwardNetwork network = new FeedforwardNetwork();
		network.AddLayer(new FeedforwardLayer(this.training[0].Length));
		network.AddLayer(new FeedforwardLayer(Config.HIDDEN_LAYER_1));
		if (Config.HIDDEN_LAYER_2 > 0)
			network.AddLayer(new FeedforwardLayer(Config.HIDDEN_LAYER_2));
		network.AddLayer(new FeedforwardLayer(this.ideal[0].Length));
		network.Reset();

		// train the neural network
		 Backpropagation train = new Backpropagation(network,
				this.training, this.ideal, Config.LEARNING_RATE,
				Config.LEARNING_MOMENTUM);

		int epoch = 1;

		do {
			train.Iteration();
			Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
			epoch++;
		} while (epoch < Config.EPOCHS);

		return network;
	}
    }
}

Videos for this Session

Videosort iconTitle
Introduction to Neural Networks for C#(Class 8)Introduction to Neural Networks for C#(Class 8)

Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.