private void createNetwork(Perceptron[] ps) { if (perceptrons == null) { perceptrons = new Perceptron[neurons_per_layer.Sum()]; int p_index = 0; for (int i = 0; i < hidden_layers_count; i++) { // Check if is last layer (output layer) bool isOutputLayer = false; if (i == hidden_layers_count - 1) { isOutputLayer = true; } for (int p = 0; p < neurons_per_layer[i]; p++) { int input_nodes; if (i == 0) { input_nodes = this.input_nodes; } else { input_nodes = neurons_per_layer[i - 1]; } perceptrons[p_index++] = new Perceptron(input_nodes, random, isOutputLayer); } } } else { perceptrons = ps; } }
public double[] Output => Neurons.Select(n => n.Output).ToArray(); //Linq expressions help with basic selections /// <summary> /// Constructs a Layer object /// </summary> /// <param name="activation">the activation function to be set in all neurons of the constructed layer</param> /// <param name="inputCount">the number of inputs of the layer</param> /// <param name="neuronCount">the number of neurons in the layer</param> public Layer(Func <double, double> activation, int inputCount, int neuronCount) { Neurons = new Perceptron[neuronCount]; for (int i = 0; i < Neurons.Length; i++) { Neurons[i] = new Perceptron(activation, inputCount); } }
private static Perceptron Train_And() { var ts = new TrainingSet_And(); var perceptron = new Perceptron(ts); perceptron.TrainPerceptron(8); TestLine(0, 0, perceptron.CalculateOutput(0, 0)); TestLine(1, 0, perceptron.CalculateOutput(1, 0)); TestLine(0, 1, perceptron.CalculateOutput(0, 1)); TestLine(1, 1, perceptron.CalculateOutput(1, 1)); return(perceptron); }
// Split genome to cross-over in genetic controller public object[] crossOverSplit() { int p1_count = (int)Math.Ceiling(perceptrons.Length / 2.0), p2_count = perceptrons.Length - p1_count; Perceptron[] p1 = new Perceptron[p1_count], p2 = new Perceptron[p2_count]; for (int i = 0; i < p1_count; i++) { p1[i] = perceptrons[i]; } for (int i = p1_count; i < p2_count; i++) { p2[i] = perceptrons[i]; } return(new object[] { p1, p2 }); }
public Neural(int inputNodes, int hiddenNodes, int outputNodes) { m_iNumInputNodes = inputNodes; m_iNumHiddenNodes = hiddenNodes; m_iNumOutputNodes = outputNodes; int i, j, k; m_arInputNodes = new Perceptron[m_iNumInputNodes + 1]; for (i = 0; i <= m_iNumInputNodes; i++) { m_arInputNodes[i] = new Perceptron(PerceptionType.PERCEPTION_INPUT, ActionvationFunction.SIGMOID_FUNCTION); } m_arInputNodes[m_iNumInputNodes].SetBiasNode(); m_arHiddenNodes = new Perceptron[m_iNumHiddenNodes + 1]; for (j = 0; j <= m_iNumHiddenNodes; j++) { m_arHiddenNodes[j] = new Perceptron(PerceptionType.PERCEPTION_HIDDEN, ActionvationFunction.SIGMOID_FUNCTION); } m_arHiddenNodes[m_iNumHiddenNodes].SetBiasNode(); m_arOutputNodes = new Perceptron[m_iNumOutputNodes]; for (k = 0; k < m_iNumOutputNodes; k++) { m_arOutputNodes[k] = new Perceptron(PerceptionType.PERCEPTION_OUTPUT, ActionvationFunction.SIGMOID_FUNCTION); } m_arInputHiddenConn = new double[m_iNumInputNodes + 1, m_iNumHiddenNodes]; m_arHiddenOutputConn = new double[m_iNumHiddenNodes + 1, m_iNumOutputNodes]; Random rand = new Random(); for (i = 0; i <= m_iNumInputNodes; i++) { for (j = 0; j < m_iNumHiddenNodes; j++) { m_arInputHiddenConn[i, j] = rand.NextDouble(); } } for (j = 0; j <= m_iNumHiddenNodes; j++) { for (k = 0; k < m_iNumOutputNodes; k++) { m_arHiddenOutputConn[j, k] = rand.NextDouble(); } } }
public Form1() { InitializeComponent(); PerceptronInputs = new List <Input>(); myDrawing = new MyDrawing(); // Transfer function 0 == binary, 1 == sigmoid _neuralNetwork = new NeuralNetwork.NeuralNetwork(2, 2, 1); _perceptron = new Perceptron(ParseData("AND.txt")); //_perceptron.PerceptronNeuron.TrainUntil(_perceptron.TrainingSet, 0.2); myDrawing.DrawPerceptron(_perceptron, perceptronPictureBox); InitControls(); refreshNetwork(); errorChart.Series.Clear(); //Hopfield _hopfield = new Hopfield(5, 7, 50); _hopImage = new List <double>(); }
private void button2_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { string sFileName = openFileDialog1.FileName; _perceptron = new Perceptron(ParseData(sFileName)); PerceptronInputs.Clear(); for (int i = 0; i < _perceptron.TrainingSet[0].Item1.Length; i++) { PerceptronInputs.Add(new Input() { Index = (i + 1), Name = "Input " + (i + 1) }); } comboInputs.DataSource = PerceptronInputs; myDrawing.DrawPerceptron(_perceptron, perceptronPictureBox); } }
public void DrawPerceptron(Perceptron perceptron, PictureBox picBox) { PerceptronHeight = 100; PerceptronWidth = 100; var inputCount = perceptron.TrainingSet[0].Item1.Length; var inputHeight = picBox.Height / inputCount; var inputSpace = 50; var pic = new Bitmap(picBox.Height, picBox.Width); var g = Graphics.FromImage(pic); var penNeuron = new Pen(Color.Black, 2); var penInput = new Pen(Color.Black, 1); var penRect = new Pen(Color.Black, 1); var brush = Brushes.Orange; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; var centerX = (pic.Width / 2) - (PerceptronWidth / 2); var centerY = (pic.Height / 2) - (PerceptronHeight / 2); var picCenterX = (pic.Width / 2); var picCenterY = (pic.Height / 2); g.DrawEllipse(penNeuron, centerX, centerY, PerceptronWidth, PerceptronHeight); var centerPoint = new PointF(picCenterX, picCenterY); //var connectionPoint = new PointF(PerceptronWidth / 10, (pic.Height / 3)); //var intersectPoint = ClosestIntersection(picCenterX, picCenterY, PerceptronWidth/2, connectionPoint, centerPoint); //g.DrawLine(pen, connectionPoint, intersectPoint); //connectionPoint = new PointF(PerceptronWidth / 10, pic.Height / 3); //intersectPoint = LineIntersectEllipse(centerPoint, connectionPoint, PerceptronHeight, PerceptronWidth); // X = 0, Y = 461 // X = 0; Y = 661 var linePoints = (inputCount - 1) * inputSpace; var inputX = 50; var inputY = picCenterY - (linePoints / 2); var pointA = new PointF(); var pointB = new PointF(); #region Inputs for (int i = 0; i < perceptron.TrainingSet.Count; i++) { for (int j = 0; j < perceptron.TrainingSet[i].Item1.Length; j++) { pointA = new PointF(inputX, inputY + (inputSpace * j)); pointB = ClosestIntersection(picCenterX, picCenterY, PerceptronWidth / 2, pointA, centerPoint); //var rect = new RectangleF(inputX -50, inputY + (inputSpace * j) - (inputSpace / 4), 50, 25); //g.DrawString("Input " + (j+1), new Font("Arial", 10, FontStyle.Regular), Brushes.Black, rect); //g.DrawRectangle(penRect, Rectangle.Round(rect)); g.DrawEllipse(penNeuron, inputX - inputSpace, inputY + (inputSpace * j) - (inputSpace / 2), 50, 50); g.DrawString("Input " + (j + 1), new Font("Arial", 8, FontStyle.Regular), Brushes.Black, inputX - 40, inputY + (inputSpace * j) - (inputSpace / 5)); g.DrawLine(penInput, pointA, pointB); g.DrawString("Weight " + (j + 1) + ": " + Math.Round(perceptron.PerceptronNeuron.InputSynapses[j].Weight, 2), new Font("Arial", 8, FontStyle.Regular), Brushes.Black, inputX + (inputSpace / 5), inputY + (inputSpace * j) + (inputSpace / 5)); //g.DrawString(value.ToString("0.00"), new Font("Arial", 7), Brushes.Green, X + 5, Y + 10); } } #endregion #region Output pointA = new PointF(picCenterX + (PerceptronWidth / 2), picCenterY); pointB = new PointF(picCenterX + (PerceptronWidth / 2) + 75, picCenterY); g.DrawLine(penInput, pointA, pointB); g.DrawEllipse(penNeuron, picCenterX + (PerceptronWidth / 2) + 75, picCenterY - (50 / 2), 50, 50); g.DrawString("Out: " + perceptron.PerceptronNeuron.Output, new Font("Arial", 8, FontStyle.Regular), Brushes.Black, picCenterX + (PerceptronWidth / 2) + 85, picCenterY - (50 / 6)); #endregion //var pointA = new PointF(inputX, inputY); //var pointB = new PointF(inputX, inputY + inputSpace); //g.DrawLine(pen, pointA, pointB); //pointA = new PointF(25, picCenterY - (PerceptronWidth / 2)); //pointB = new PointF(25, picCenterY + (PerceptronWidth / 2)); //g.DrawLine(pen, pointA, pointB); picBox.Image = pic; }
void setup() { perceptron = new Perceptron(3); // generate trainers Random random = new Random(); for (int i = 0; i < trainers.Count(); i++) { float x = (float)((this.Width * random.NextDouble()) - (this.Width / 2)); float y = (float)((this.Height * random.NextDouble()) - (this.Height / 2)); int answer = 1; if (y < lineFunction(x)) answer = -1; trainers[i] = new Trainer(x, y, answer); } }