Introduction to Neural Networks for C#, Session 6
| Course Name | Introduction to Neural Networks for C# |
| Instructor | jeffheaton |
| Session Title | Discuss Program 1 |
| Session Number | 6 |
Session Material
I present here my solution for program 1. You can use the link below to download an Visual Studio project for my solution.
First you will need to create training data in a CSV file for Program 1.
Listing: CSV Input for Program 1
0,0,0,1,0,0 0,1,0,1,0,0 1,0,0,1,0,0 1,1,1,1,0,0 0,0,0,0,1,0 0,1,1,0,1,0 1,0,1,0,1,0 1,1,1,0,1,0 0,0,0,0,0,1 0,1,1,0,0,1 1,0,1,0,0,1 1,1,0,0,0,1
Solution for program 1.
Listing: Solution for Program 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeatonResearchNeural.Feedforward;
using HeatonResearchNeural.Feedforward.Train;
using HeatonResearchNeural.Feedforward.Train.Backpropagation;
using HeatonResearchNeural.Util;
using System.Threading;
namespace Program1
{
class Program1
{
const int COLUMNS = 6;
const int OPP_AND = 0;
const int OPP_OR = 1;
const int OPP_XOR = 2;
private FeedforwardNetwork network;
private double[][] input;
private double[][] ideal;
public void CreateNetwork()
{
this.network = new FeedforwardNetwork();
network.AddLayer(new FeedforwardLayer(Program1.COLUMNS));
network.AddLayer(new FeedforwardLayer(3));
network.AddLayer(new FeedforwardLayer(1));
network.Reset();
}
public void Train()
{
// train the neural network
Train train = new Backpropagation(network, this.input,
this.ideal,
0.7, 0.8);
int epoch = 1;
do
{
train.Iteration();
Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
epoch++;
} while ((epoch < 5000) && (train.Error > 0.001));
}
public double Evaluate(double op1, double op2, int operation)
{
double[] input = new double[Program1.COLUMNS];
input[0] = op1;
input[1] = op2;
// AND
if (operation == Program1.OPP_AND)
input[2] = 1.0;
else
input[2] = 0.0;
// OR
if (operation == Program1.OPP_OR)
input[3] = 1.0;
else
input[3] = 0.0;
// XOR
if (operation == Program1.OPP_XOR)
input[4] = 1.0;
else
input[4] = 0.0;
double[] output = this.network.ComputeOutputs(input);
return output[0];
}
public void Load(String filename)
{
int size = 0;
// count the size of the file
ReadCSV csv = new ReadCSV(filename, false);
while (csv.Next())
{
size++;
}
csv.Close();
// allocate enough space
this.input = new double[size][];
this.ideal = new double[size][];
// now load it
int index = 0;
csv = new ReadCSV(filename, false);
while (csv.Next())
{
this.input[index] = new double[Program1.COLUMNS];
this.ideal[index] = new double[1];
this.input[index][0] = Double.Parse(csv.Get(0));
this.input[index][1] = Double.Parse(csv.Get(1));
this.input[index][2] = Double.Parse(csv.Get(3));
this.input[index][3] = Double.Parse(csv.Get(4));
this.input[index][4] = Double.Parse(csv.Get(5));
this.ideal[index][0] = Double.Parse(csv.Get(2));
index++;
}
csv.Close();
}
static void Main(string[] args)
{
Program1 prg = new Program1();
prg.Load("c:\\data\\logic.csv");
prg.CreateNetwork();
prg.Train();
Console.WriteLine("1 AND 1 = " + prg.Evaluate(1, 1, Program1.OPP_AND));
Console.WriteLine("1 XOR 1 = " + prg.Evaluate(1, 1, Program1.OPP_XOR));
Console.WriteLine("1 AND 0 = " + prg.Evaluate(1, 0, Program1.OPP_AND));
Console.WriteLine("1 OR 1 = " + prg.Evaluate(1, 1, Program1.OPP_OR));
}
}
}
Videos for this Session
| Video | Title |
|---|---|
![]() | Introduction to Neural Networks for C#(Class 6) |




