///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// // draw the score function f(x,y)=ax+by+c static public void DrawScore(float a, float b, float c, int CAT) { Graphique G = Form1.G; for (int yecran = 0; yecran < G.GetH(); yecran++) { for (int xecran = 0; xecran < G.GetL(); xecran++) { float x = G.XPixToVal(xecran); float y = G.YPixToVal(yecran); float score = a * x + b * y + c; Color cc = G.GetPixel(xecran, yecran); float level = Utils.ColorToScore(cc); if (score < level) { continue; } G.SetPixel(xecran, yecran, Utils.ScoreToColor(score, CAT)); } } }
public static void Final(Graphique G) { Element.InitSpirale(); List <Element> data = Element.List; //////////////////////////////////////////////////////////////////////// int round_nbr = 2000; // gradient descent loop for (int round = 0; round < round_nbr; round++) { float data_loss = 0; if (round % 20 == 0) { G.ClearBlack(); DrawScore(); G.DrawAxis(); G.DrawData(); Form1.Schema.Invalidate(); Form1.Schema.Update(); Form1.iteration2.Text += "Iteration " + round + " : Loss = " + data_loss + "\r\n"; Form1.iteration2.Update(); } } }
///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// static public void LevelSet(float[,] W, float value) { Graphique G = Form1.G; for (int yecran = 0; yecran < G.GetH(); yecran++) { for (int xecran = 0; xecran < G.GetL(); xecran++) { float x = G.XPixToVal(xecran); float y = G.YPixToVal(yecran); float[] sco = new float[3]; for (int i = 0; i < 3; i++) { sco[i] = W[i, 0] * x + W[i, 1] * y + W[i, 2]; } float mx = sco.Max(); if (Math.Abs(mx - value) < 0.001) { G.SetPixel(xecran, yecran, Color.Red); } } } }
public static void Optim2D(Graphique G) { G.ClearWhite(); DrawFnt2D(Fnt); G.DrawAxis(); double x = Utils.RND(); double y = Utils.RND(); G.Cross(x, y, Color.White); ////////////////////////////////////////////////////// double k = 0.1; double x_prev = 0; double y_prev = 0; while ((x_prev == 0 && y_prev == 0) || (Math.Abs(x_prev) - Math.Abs(x) > zero || Math.Abs(y_prev) - Math.Abs(y) > zero)) { x_prev = x; y_prev = y; x = x_prev - (k * Fnt_dx(x_prev, y_prev)); y = y_prev - (k * Fnt_dy(x_prev, y_prev)); G.Cross(x, y, Color.White); } ////////////////////////////////////////////////////// }
public static void Final(Graphique G) { List <Element> data = Element.List; Form1.iteration.Text = ""; int K = 3; // nombre de classes int D = 3; // dimension 2 + 1 for the biais Matrix W = new Matrix(K, D); W.RandomInit(); double data_loss = 0; ////////////////////////////////////////////////////////////////////////////////////////////// // gradient descent loop for (int round = 0; round < 200; round++) { ///////////////////////////////////////////////////////////////////////////////////////// if (round % 10 == 0) { G.ClearBlack(); DrawScore(W.matrix); G.DrawAxis(); G.DrawData(); Form1.Schema.Invalidate(); Form1.Schema.Update(); Form1.iteration.Text += "Iteration " + round + " : Loss = " + data_loss + "\r\n"; Form1.iteration.Update(); } } }
public static void Optim2Dmax(Graphique G) { G.ClearWhite(); DrawFnt2D(Fntmax); G.DrawAxis(); var r = new Random(); double gamma = .01; // step size multiplier double x = r.NextDouble() - 0.5; double y = r.NextDouble() - 0.5; double z = Fntmax(x, y); /////////////////////////////////////////////////////////////////// for (int i = 0; i < 1000; i++) { if (Fntmax(x, y) == Fntv1(x, y)) { x += -gamma *dFntv1x(x, y); y += -gamma *dFntv1y(x, y); } else { x += -gamma *dFntv2x(x, y); y += -gamma *dFntv2y(x, y); } G.Cross(x, y, Color.White); } /////////////////////////////////////////////////////////////////// }
public static void test3(Graphique G) { G.ClearBlack(); float[,] W = new float[3, 3] { { 1, 0, 0 }, { -1, 0, 1.5f }, { 0, 1, 0 } }; OneLayer.DrawScore(W); OneLayer.LevelSet(W, 0.5f); G.DrawAxis(); }
public static void DrawFnt2D(FntXY Score) { Graphique G = Form1.G; for (int yecran = 0; yecran < G.GetL(); yecran++) { for (int xecran = 0; xecran < G.GetH(); xecran++) { double x = Form1.G.XPixToVal(xecran); double y = Form1.G.YPixToVal(yecran); double s = Score(x, y); Color C = Utils.ScoreToHUE(s); Form1.G.SetPixel(xecran, yecran, C); } } }
public static void GradLitteral(Graphique G) { G.ClearWhite(); DrawFnt(g, Color.Red); double x = 1; double gamma = 0.01; // step size multiplier double precision = 0.001; // range limit double previous_step_size = x; while (previous_step_size > precision) { double prev_x = x; x += -gamma *dg(prev_x); previous_step_size = Math.Abs(x - prev_x); G.Cross(x, g(x), Color.Blue); } }
public static void test4(Graphique G) { G.ClearBlack(); float[,] W = new float[3, 3] { { 1, 0, 0 }, { -1, 0, 0 }, { 1f, 1f, 0 } }; /*float[,] W = new float[3, 3] * { * { 1, 0f, 0f }, * { -1, 0f, 0f }, * { 1f, 0.8f, 0f } * };*/ OneLayer.DrawScore(W); OneLayer.LevelSet(W, 0.4f); G.DrawAxis(); }
public static void GradLitteral(Graphique G) { G.ClearWhite(); DrawFnt(g, Color.Red); double x = 1; double y = g(x); G.Cross(x, y, Color.Blue); /////////////////////////////////////////////////////// double k = 0.01; double y_prev = y; while (y <= y_prev) // if y is larger than the previous y, then the minima has already been found { y_prev = y; x = x - (k * g_d(x)); y = g(x); G.Cross(x, y, Color.Blue); } /////////////////////////////////////////////////////// }
public static void GradEstimation(Graphique G) { G.ClearWhite(); DrawFnt(f, Color.Red); double x = 0.7; double y = f(x); G.Cross(x, y, Color.Blue); //////////////////////////////////////////////////////////// double eps = 0.05; double k = 0.01; // Rate of steps while (Math.Abs(x) > zero) // Keep trying to find minima that is located in x=0 { x = x - (k * f_d(x, eps)); // x progression towards minima y = f(x); G.Cross(x, y, Color.Blue); } /////////////////////////////////////////////////////// }
static public void DrawFnt(FntX F, Color c) { Graphique G = Form1.G; for (int xecran = 1; xecran < G.GetH(); xecran++) { double x = G.XPixToVal(xecran - 1); double y = F(x); int yecran = G.YValToPix(y); double x2 = G.XPixToVal(xecran); double y2 = F(x2); int yecran2 = G.YValToPix(y2); Utils.minmax(ref yecran, ref yecran2); for (int yy = yecran; yy <= yecran2; yy++) { G.SetPixel(xecran, yy, c); } } G.DrawAxis(); }
public static void Optim2D(Graphique G) { G.ClearWhite(); DrawFnt2D(Fnt); G.DrawAxis(); var r = new Random(); double gamma = .1; // step size multiplier //double precision = .1; // range limit double x = r.NextDouble() - 0.5; double y = r.NextDouble() - 0.5; double z = Fnt(x, y); for (int i = 0; i < 30; i++) { x += -gamma *dFntx(x, y); y += -gamma *dFnty(x, y); z = Fnt(x, y); G.Cross(x, y, Color.White); } }
// draw the score function f(x,y)=ax+by+c static public void DrawScore() { Graphique G = Form1.G; int pas = 3; for (int yecran = 0; yecran < G.GetH(); yecran += pas) { for (int xecran = 0; xecran < G.GetL(); xecran += pas) { float x = G.XPixToVal(xecran); float y = G.YPixToVal(yecran); // on calcule le score pour chaque pixel de l'ecran float[] sco = compute_score(x, y); int first = 0; int second = 1; // look for biggest and second biggest score if ((sco[0] >= sco[1]) && (sco[1] >= sco[2])) { first = 0; second = 1; } if ((sco[0] >= sco[2]) && (sco[2] >= sco[1])) { first = 0; second = 2; } if ((sco[1] >= sco[0]) && (sco[0] >= sco[2])) { first = 1; second = 0; } if ((sco[1] >= sco[2]) && (sco[2] >= sco[0])) { first = 1; second = 2; } if ((sco[2] >= sco[0]) && (sco[0] >= sco[1])) { first = 2; second = 0; } if ((sco[2] >= sco[1]) && (sco[1] >= sco[0])) { first = 2; second = 1; } int cat = first; float h = sco[first] - sco[second]; h *= 4; Utils.Inside(0f, 1f, ref h); int[,] Legende = { { 150, 150, 255 }, { 150, 255, 150 }, { 255, 150, 150 } }; int RR = (int)(Legende[cat, 0] * h); int GG = (int)(Legende[cat, 1] * h); int BB = (int)(Legende[cat, 2] * h); Color c = Color.FromArgb(RR, GG, BB); for (int xx = 0; xx < pas; xx++) { for (int yy = 0; yy < pas; yy++) { G.SetPixel(xecran + xx, yecran + yy, c); } } } } }
// draw the score function f(x,y)=ax+by+c static public void DrawScore(float[,] W) { Graphique G = Form1.G; for (int yecran = 0; yecran < G.GetH(); yecran++) { for (int xecran = 0; xecran < G.GetL(); xecran++) { float x = G.XPixToVal(xecran); float y = G.YPixToVal(yecran); float[] sco = new float[3]; for (int i = 0; i < 3; i++) { sco[i] = W[i, 0] * x + W[i, 1] * y + W[i, 2]; } int first = 0; int second = 1; // look for biggest and second biggest score if ((sco[0] >= sco[1]) && (sco[1] >= sco[2])) { first = 0; second = 1; } if ((sco[0] >= sco[2]) && (sco[2] >= sco[1])) { first = 0; second = 2; } if ((sco[1] >= sco[0]) && (sco[0] >= sco[2])) { first = 1; second = 0; } if ((sco[1] >= sco[2]) && (sco[2] >= sco[0])) { first = 1; second = 2; } if ((sco[2] >= sco[0]) && (sco[0] >= sco[1])) { first = 2; second = 0; } if ((sco[2] >= sco[1]) && (sco[1] >= sco[0])) { first = 2; second = 1; } int cat = first; float h = sco[first] - sco[second]; h *= 4; Utils.Inside(0, 1, ref h); int[,] Legende = { { 150, 150, 255 }, { 150, 255, 150 }, { 255, 150, 150 } }; int RR = (int)(Legende[cat, 0] * h); int GG = (int)(Legende[cat, 1] * h); int BB = (int)(Legende[cat, 2] * h); Color c = Color.FromArgb(RR, GG, BB); G.SetPixel(xecran, yecran, c); } } }