public static void Predict(Matrix Model, int dims, string cliString) { var Re = new double[dims]; for (var j = 0; j < Re.Length; j++) { if (j >= cliString.Length) { break; } Re[j] = char.ToUpper(cliString[j]) / (double)128; } Console.WriteLine(); foreach (Vector it in Model) { int len = it.Axis.Length; double dot = 0.0, y; for (int j = 0; j < len; j++) { dot += Re[j] * it.Axis[j].Im; } y = SigF.f(dot); Console.WriteLine($"p({it.Id}|{cliString}) ~ {y}"); } }
private static void DrawVector(Graphics g, RectangleF r, Complex[] F, Color color) { float linf(float val, float from, float to) { return(val * (to / from)); } var PixelOffsetMode = g.PixelOffsetMode; if (F != null) { g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; var dots = new List <PointF>(); var pen = new Pen(color, 2f); for (int i = 0; i < F.Length; i++) { var ampl = SigF.f((F[i].Im + F[i].Re) * 0.5) - 0.5; // var ampl = ((F[i].Re + F[i].Im) / 2f); //var ampl = Tanh.f((F[i].Re + F[i].Im) / 2f); if (ampl < -1) { ampl = -1; } if (ampl > +1) { ampl = +1; } if (ampl < -1 || ampl > +1) { throw new IndexOutOfRangeException(); } float m = r.Height / 2f; float y = linf(-(float)ampl, 1f, m) + m; float x = linf(i, F.Length - 1, r.Width); dots.Add(new PointF(x, y)); } PointF[] pts = dots.ToArray(); if (pts.Length > 1) { g.DrawCurve( pen, pts); } foreach (PointF p in pts) { float m = r.Height / 2f; g.FillEllipse(pen.Brush, p.X - 3, p.Y - 3, 7, 7); } pen.Dispose(); } g.PixelOffsetMode = PixelOffsetMode; }
public static float BinaryLogistic(Complex[] y, float[] X) { Debug.Assert(X != null); Debug.Assert(y != null); Debug.Assert(X.Length == y.Length); float σ = 0.0f; for (int j = 0; j < X.Length; j++) { σ += y[j].Re * X[j]; } σ = (float)SigF.f(σ); return(σ); }
static double binaryLogistic(double[] X, double[] grads, Complex[] Y, double label, double lr) { if (Y == null) { return(0); } Debug.Assert(label >= -1 && label <= 1); Debug.Assert(lr >= 0 && lr <= 1); int len = X.Length; double dot = 0.0; for (int j = 0; j < len; j++) { dot += Y[j].Im * X[j]; } var y = SigF.f(dot); double diff = lr * (label - y); if (double.IsNaN(diff) || double.IsInfinity(diff)) { Console.WriteLine("NaN detected..."); return(diff); } if (grads != null) { Debug.Assert(grads.Length == len); for (int j = 0; j < len; j++) { grads[j] += Y[j].Im * diff; } } for (int j = 0; j < len; j++) { Y[j].Im += X[j] * diff; } return(y); }
public static void onDrawMic(Surface2D Canvas, float phase, ISource wav) { int hz = wav?.Hz ?? 0; var fft = wav?.Peek(); int samples = wav != null ? fft.Length : 0; double h = samples != 0 ? hz / (double)samples : 0; var duration = hz != 0 ? Math.Round((double)samples / hz, 4) : 0; Canvas.TopLeft = $"{samples} @ {hz}Hz = {duration}s, {h}Hz"; Canvas.TopRight = $"{phase:N4}s"; Canvas.Fill(Color.Black); if (fft == null) { return; } var X = Complex.InverseFFT(fft); Canvas.Line((x, width) => Color.Green, (x, width) => { int i = Surface2D.linear(x, width, X.Length); return(SigF.f(X[i]) + 0.07); }); Canvas.Line((x, width) => Color.FromArgb(173, 216, 230), (x, width) => { int i = Surface2D.linear(x, width, samples / 7); return(+(SigF.f(2 * fft[i].Magnitude) - 0.5)); }, true); for (int s = 0; s < samples; s++) { fft[s].Scale(0.5f); } X = Complex.InverseFFT(fft); Canvas.Line((x, width) => Color.White, (x, width) => { int i = Surface2D.linear(x, width, samples / 7); return(-(SigF.f(2 * fft[i].Magnitude) - 0.5)); }, true); Canvas.Line((x, width) => Color.OrangeRed, (x, width) => { int i = Surface2D.linear(x, width, X.Length); return(SigF.f(X[i]) + 0.07); }); Canvas.Line((x, width) => Color.DarkOrange, (x, width) => { int i = Surface2D.linear(x, width, X.Length); return(SigF.f(X[i]) - 1.07); }); }