101-20 guruh talabasi Shokirova Hayitgulning Su’niy intellekt va neyronto’rli texnologiyalar fanidan 2-amaliy topshiriq
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool draw = false;
bool train = true;
Point begin;
int lineNumber = 0;
List
lineOne = new List
();
float[,] lineOneNormalized;
float[,] lineOneNormalizedSquared;
List
lineTwo = new List
();
float[,] lineTwoNormalized;
float[,] lineTwoNormalizedSquared;
float[,] coordinatesAllNormalized;
float[,] coordinatesAllNormalizedSquared;
static int[] layers = new int[4] { 2, 5, 5, 1 };
static string[] activation = new string[3] { "tanh", "tanh", "tanh"};
NeuralNetwork net = new NeuralNetwork(layers, activation);
Random rand = new Random();
int minX = 10000, minY = 10000, maxX = 0, maxY = 0;
int xyOffset = 5;
int xBorderMin, xBorderMax, yBorderMin, yBorderMax;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen p = new Pen(Color.Aqua, 4);
Pen p2 = new Pen(Color.Orange, 4);
reDrawAll(e.Graphics, p, p2);
}
private void reDraw(Graphics g, Pen p1, Pen p2)
{
for (int i = 0; i < lineOne.Count - 1; i++)
g.DrawLine(p1, lineOne[i], lineOne[i + 1]);
for (int i = 0; i < lineTwo.Count - 1; i++)
g.DrawLine(p2, lineTwo[i], lineTwo[i + 1]);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
draw = true;
begin.X = e.X;
begin.Y = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Aqua, 4);
Pen p2 = new Pen(Color.Orange, 4);
if (draw)
{
Point newPoint = new Point(e.X, e.Y);
if (lineNumber == 0)
{
lineOne.Add(begin);
g.DrawLine(p, begin, newPoint);
}
if (lineNumber == 1)
{
lineTwo.Add(begin);
g.DrawLine(p2, begin, newPoint);
}
begin = newPoint;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
lineNumber++;
}
async private void button1_Click(object sender, EventArgs e)
{
//net = new NeuralNetwork(layers, activation);
int randomNumber = 0;
train = true;
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Aqua, 4);
Pen p2 = new Pen(Color.Orange, 4);
int count = 0;
int countLearningRate = 0;
for (int i = 0; train; count++)
{
if (i == 0)
{
randomNumber = rand.Next(0, (lineOne.Count-1));
net.BackPropagate(new float[] { lineOneNormalized[randomNumber,0] ,
lineOneNormalized[randomNumber, 1] }, new float[] { 1 });
i = 1;
}
else
{
randomNumber = rand.Next(0, (lineTwo.Count - 1));
net.BackPropagate(new float[] { lineTwoNormalized[randomNumber, 0],
lineTwoNormalized[randomNumber, 1] }, new float[] { -1 });
i = 0;
}
if (count == 20000)
{
reDrawAll(g, p, p2);
await Task.Delay(1000);
count = 0;
countLearningRate++;
if ( (net.learningRate > 0.0001f) && (countLearningRate == 10))
{
net.learningRate /= (float)2;
countLearningRate = 0;
}
}
}
}
private void drawNeuralNetAnswer(Graphics g)
{
Brush aBrush = (Brush)Brushes.Green;
Brush aBrush2 = (Brush)Brushes.Red;
float xnormalized;
float ynormalized;
float[] netA;
for(int i = xBorderMin; i < xBorderMax; i++)
for (int j = yBorderMin; j < yBorderMax; j++)
{
//xnormalized = normalizeX(i);
//ynormalized = normalizeY(j);
netA = net.FeedForward(new float[] { normalizeX(i), normalizeY(j) });
if (netA[0] > 0.0f)
g.FillRectangle(aBrush, i, j, 1 , 1);
else
g.FillRectangle(aBrush2, i, j, 1, 1);
//g.DrawLine(p2, new Point(i, j), new Point(i , j ));
}
}
private void drawNeuralNetAnswerSquared(Graphics g)
{
Brush aBrush = (Brush)Brushes.Green;
Brush aBrush2 = (Brush)Brushes.Red;
float xnormalized;
float ynormalized;
float[] netA;
for (int i = xBorderMin; i < xBorderMax; i++)
for (int j = yBorderMin; j < yBorderMax; j++)
{
//xnormalized = normalizeX(i);
//ynormalized = normalizeY(j);
netA = net.FeedForward(new float[] { normalizeX(i), normalizeY(j) });
if (netA[0] > 0)
g.FillRectangle(aBrush, i, j, 1, 1);
else
g.FillRectangle(aBrush2, i, j, 1, 1);
//g.DrawLine(p2, new Point(i, j), new Point(i , j ));
}
}
private void reDrawAll(Graphics g, Pen p1, Pen p2)
{
drawNeuralNetAnswer(g);
reDraw(g, p1, p2);
}
private void button2_Click(object sender, EventArgs e)
{
train = false;
}
private void button3_Click(object sender, EventArgs e)
{
findBorder();
calcuateBorders();
normalizeCoordinates();
}
private void findBorder()
{
for (int i = 0; i < lineOne.Count; i++)
{
if (minX > lineOne[i].X)
minX = lineOne[i].X;
if (minY > lineOne[i].Y)
minY = lineOne[i].Y;
if (maxX < lineOne[i].X)
maxX = lineOne[i].X;
if (maxY < lineOne[i].Y)
maxY = lineOne[i].Y;
}
for (int i = 0; i < lineTwo.Count; i++)
{
if (minX > lineTwo[i].X)
minX = lineTwo[i].X;
if (minY > lineTwo[i].Y)
minY = lineTwo[i].Y;
if (maxX < lineTwo[i].X)
maxX = lineTwo[i].X;
if (maxY < lineTwo[i].Y)
maxY = lineTwo[i].Y;
}
}
private void calcuateBorders()
{
xBorderMin = minX - xyOffset;
xBorderMax = maxX + xyOffset;
yBorderMin = minY - xyOffset;
yBorderMax = maxY + xyOffset;
}
private void normalizeCoordinates()
{
lineOneNormalized = new float[lineOne.Count, 2];
lineTwoNormalized = new float[lineTwo.Count, 2];
for (int i = 0; i < lineOne.Count; i++)
{
lineOneNormalized[i, 0] = normalizeX(lineOne[i].X);
lineOneNormalized[i, 1] = normalizeY(lineOne[i].Y);
}
for (int i = 0; i < lineTwo.Count; i++)
{
lineTwoNormalized[i, 0] = normalizeX(lineTwo[i].X);
lineTwoNormalized[i, 1] = normalizeY(lineTwo[i].Y);
}
}
private void squareNormalizedLineCoordinates()
{
lineOneNormalizedSquared = new float[lineOne.Count, 2];
lineTwoNormalizedSquared = new float[lineTwo.Count, 2];
for (int i = 0; i < lineOne.Count; i++)
{
lineOneNormalizedSquared[i, 0] = squareInput(lineOneNormalized[i, 0]);
lineOneNormalizedSquared[i, 1] = squareInput(lineOneNormalized[i, 1]);
}
for (int i = 0; i < lineTwo.Count; i++)
{
lineTwoNormalizedSquared[i, 0] = squareInput(lineTwoNormalized[i, 0]);
lineTwoNormalizedSquared[i, 1] = squareInput(lineTwoNormalized[i, 1]);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void normalizeCoordinatesAll()
{
coordinatesAllNormalized = new float[xBorderMax - xBorderMin , yBorderMax - yBorderMin];
for (int k = 0, i = xBorderMin; i < xBorderMax; k++, i++)
for (int m = 0, j = yBorderMin; j < yBorderMax; m++, j++)
{
coordinatesAllNormalized[k, m] = 0;
}
}
private float normalizeX(int x)
{
return normalization(x, xBorderMin, xBorderMax);
}
private float normalizeY(int y)
{
return normalization(y, yBorderMin, yBorderMax);
}
private float normalization(int x, int minXvalue, int maxXvalue)
{
return ((float)(x - minXvalue) / (float)(maxXvalue - minXvalue));
}
private int normalizationint(int x, int minXvalue, int maxXvalue)
{
return (x - ((maxXvalue + minXvalue) / 2));
}
private float squareInput(float x)
{
return x * x;
}
}
}
Do'stlaringiz bilan baham: |