Exemple #1
0
    static void TrainMelModel(Set sourceFiles,
                              Matrix Model, Action <double> SetLoss, Func <bool> HasCtrlBreak)
    {
        if (Model == null)
        {
            Console.WriteLine("Model not loaded.");
            return;
        }
        Thread[] threads         = new Thread[Environment.ProcessorCount * 2];
        int      numberOfThreads = 0,
                 verbOut = 0;

        for (var t = 0; t < threads.Length; t++)
        {
            threads[t] = new Thread(() => {
                string[] Shuffle = ((IEnumerable <string>)sourceFiles).ToArray();
                Random.Shuffle(
                    Shuffle,
                    Shuffle.Length);
                Interlocked.Increment(ref numberOfThreads);
                try {
                    for (int iter = 0; iter < GENS; iter++)
                    {
                        if (HasCtrlBreak != null && HasCtrlBreak())
                        {
                            break;
                        }
                        foreach (string file in Shuffle)
                        {
                            if (HasCtrlBreak != null && HasCtrlBreak())
                            {
                                return;
                            }
                            try {
                                Matrix Data = null;
                                Data        = System.Ai.Model.LoadFromFile(file, SIZE, out string fmt, out CBOW.DIMS);
                                Debug.Assert(fmt == "MIDI");
                                double loss = 0,
                                cc          = 0;
                                var wo      = Model["a"];
                                if (wo == null)
                                {
                                    Interlocked.Increment(ref verbOut);
                                    continue;
                                }
                                foreach (var it in Data)
                                {
                                    double score = 0,
                                    err          = 0;
                                    double[] X
                                        = new double[it.Axis.Length];
                                    for (int j = 0; j < X.Length; j++)
                                    {
                                        X[j] = it.Axis[j].Re;
                                    }
                                    bool label = it.Id == wo.Id;
                                    if (!label)
                                    {
                                        for (int j = 0; j < X.Length; j++)
                                        {
                                            X[j] = ((Random.Next() & 0xFFFF) / (65536f));
                                            if (X[j] < 0 || X[j] > 0.7)
                                            {
                                                X[j] = 0;
                                            }
                                        }
                                        if (((Random.Next() & 0xFFFF) / (65536f)) > 0.7)
                                        {
                                            for (int j = 0; j < X.Length; j++)
                                            {
                                                X[j] = 0;
                                            }
                                        }
                                    }
                                    score = CBOW.binaryLogistic(
                                        X,
                                        null,
                                        wo.Axis,
                                        label ? 1.0 : 0.0,
                                        CBOW.lr);
                                    if (label)
                                    {
                                        err = -System.Math.Log(score);
                                    }
                                    else
                                    {
                                        err = -System.Math.Log(1.0 - score);
                                    }
                                    if (double.IsNaN(err) || double.IsInfinity(err))
                                    {
                                        Console.WriteLine("NaN detected...");
                                    }
                                    else
                                    {
                                        loss += err;
                                        cc++;
                                        if (err == 0 || (verbOut >= 0 && ((verbOut % CBOW.VERBOSITY) == 0)))
                                        {
                                            Console.Write($"[{Thread.CurrentThread.ManagedThreadId}.{verbOut}] " +
                                                          $" Cost: {loss / cc}\r\n");
                                        }
                                        Interlocked.Increment(ref verbOut);
                                    }
                                }
                                Thread.Sleep(3000 + Random.Next(3000));
                            } finally {
                            }
                        }
                    }
                } finally {
                    Interlocked.Decrement(ref numberOfThreads);
                }
                Console.Write($"[{Thread.CurrentThread.ManagedThreadId}] stopped...\r\n");
            });
        }
        foreach (var t in threads)
        {
            t.Start();
        }
        foreach (var t in threads)
        {
            t.Join();
        }
        Debug.Assert(numberOfThreads == 0);
    }
Exemple #2
0
    public static Vector[] RunFullCosineSort(IOrthography lex, Matrix Model, string Q, int max)
    {
        if (Model == null || string.IsNullOrWhiteSpace(Q))
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Model not loaded.\r\n");
            Console.ResetColor();
            Console.WriteLine("See '--load' command for more info...\r\n");
            return(null);
        }
        double[] Re   = new double[CBOW.DIMS];
        double   norm = 0;
        var      sign = +1;

        foreach (var tok in PlainText.ForEach(Q, 0, Q.Length, 0))
        {
            string wi = lex.GetKey(tok.TextFragment.Substring(tok.StartIndex, tok.Length));
            if (wi == "+")
            {
                sign = +1;
            }
            else if (wi == "-")
            {
                sign = -1;
            }
            else
            {
                var vec = Model[wi];
                if (vec != null)
                {
                    Debug.Assert(vec.Axis.Length == Re.Length);
                    for (var j = 0; j < Re.Length; j++)
                    {
                        Re[j] += sign * vec.Axis[j].Re;
                    }
                    norm++;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine($"'{wi}' not found.");
                    Console.ResetColor();
                }
            }
        }
        if (norm > 0)
        {
            for (var j = 0; j < Re.Length; j++)
            {
                Re[j] /= (double)norm;
            }
        }
        Vector[] output = CBOW.Predict(Model, Re, max);
        Array.Sort(output,
                   (a, b) => Scalar.CompareTo(a, b));
        Console.WriteLine();
        Console.WriteLine(" [" + string.Join(",", Re.Select(re => Math.Round(re, 4)).Take(7)) + "...]");
        Console.WriteLine();
        int len = 0;

        for (int i = output.Length - 1; i >= 0; i--)
        {
            Vector n = output[i];
            if (n != null)
            {
                string str = n.Id;
                var    it  = Model[n.Id];
                if (it != null)
                {
                    // if (it.Count > 0) {
                    //     var best = it.ArgMax();
                    //     if (best != null) {
                    //         str = best.Id;
                    //     }
                    // }
                }
                if (len + str.Length > 37 /* break like if does not fit */)
                {
                    Console.WriteLine(
                        output.Length <= 31
                            ? $" {str} : {n.Score}"
                            : $" {str}");
                    len = 0;
                }
                else
                {
                    Console.Write(
                        output.Length <= 31
                            ? $" {str} : {n.Score}"
                            : $" {str}");
                    len += str.Length;
                }
            }
        }
        Console.WriteLine();
        return(output);
    }