static void ScoreMelModel(Set sourceFiles, Matrix Model) { if (Model == null) { Console.WriteLine("Model not loaded."); return; } string[] Shuffle = ((IEnumerable <string>)sourceFiles).ToArray(); foreach (string file in Shuffle) { Matrix Data = System.Ai.Model.LoadFromFile(file, SIZE, out string fmt, out CBOW.DIMS); Debug.Assert(fmt == "MIDI"); var wo = Model["a"]; if (wo == null) { continue; } foreach (var it in Data) { double dot = 0.0, score; for (int j = 0; j < it.Axis.Length; j++) { dot += it.Axis[j].Re * wo.Axis[j].Im; } score = Sigmoid.f(dot); it.Score.Im = score; it.Axis = null; } SaveMidi(Data.GetBuffer(), fmt, Path.ChangeExtension(file, ".score")); } }
public static Word[] Predict(IEnumerable <Word> Model, float[] Re, int max) { Word[] best = new Word[max]; foreach (Word c in Model) { int b = 0; for (int j = 0; j < best.Length; j++) { if (best[j] == null) { b = j; break; } if (best[j].Re < best[b].Re) { b = j; } } float dot = 0, score; for (int j = 0; j < Re.Length; j++) { dot += c.Elements[j].Im * Re[j]; } score = (float)Sigmoid.f(dot); if (best[b] == null || best[b].Re < score) { best[b] = new Word(c.Id, c.HashCode) { Re = score }; } } return(best); }
public static double binaryLogistic(double[] X, double[] W) { Debug.Assert(X.Length == W.Length); int len = X.Length; double Dot = 0.0; for (int j = 0; j < len; j++) { Dot += X[j] * W[j]; } var y = Sigmoid.f(Dot); return(y); }
public static double binaryLogistic(double[] wi, double[] grads, Complex[] wo, double label, double lr) { if (wo == null) { return(0); } Debug.Assert(label >= -1 && label <= 1); Debug.Assert(lr >= 0 && lr <= 1); int len = wi.Length; double dot = 0.0; for (int j = 0; j < len; j++) { dot += wo[j].Im * wi[j]; } var y = Sigmoid.f(dot); double diff = lr * (label - y); if (double.IsNaN(diff) || double.IsInfinity(diff)) { Console.WriteLine("NaN or Infinity detected..."); return(diff); } if (grads != null) { Debug.Assert(grads.Length == len); for (int j = 0; j < len; j++) { grads[j] += wo[j].Im * diff; } } for (int j = 0; j < len; j++) { wo[j].Im += (float)(wi[j] * diff); } return(y); }