Пример #1
0
        public Matrix Uniform(float a)
        {
            float a2 = 2 * a;
            float an = -a;

            ThreadSafeFastRandom.NextFloats(Data.AsSpan());
            SIMD.Multiply(Data.AsSpan(), a2);
            SIMD.Add(Data.AsSpan(), an);

            return(this);
        }
Пример #2
0
        public Matrix Uniform(float a)
        {
            float a2 = 2 * a;
            float an = -a;

            Parallel.For(0, Rows, (i) =>
            {
                ThreadSafeFastRandom.NextFloats(Data[i]);
                SIMD.Multiply(ref Data[i], a2);
                SIMD.Add(ref Data[i], an);
            });
            return(this);
        }
Пример #3
0
        public void ResizeAndFillRows(int newRows, float a)
        {
            float a2 = 2 * a;
            float an = -a;

            Array.Resize(ref Data, newRows * Columns);
            var toFill = Data.AsSpan().Slice(Rows * Columns);

            ThreadSafeFastRandom.NextFloats(toFill);
            SIMD.Multiply(toFill, a2);
            SIMD.Add(toFill, an);

            Rows = newRows;
        }
Пример #4
0
        public IList <DetectedLanguage> DetectAll(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(Array.Empty <DetectedLanguage>());
            }

            var ngrams = ExtractNGrams(NormalizeText(text));

            try
            {
                if (ngrams.Count == 0)
                {
                    return(Array.Empty <DetectedLanguage>());
                }

                Span <double> languageProbabilities = Data.Languages.Count < 256 ? stackalloc double[Data.Languages.Count] : new double[Data.Languages.Count];

                for (int t = 0; t < Trials; t++)
                {
                    var probs = InitializeProbabilities();

                    double alpha = Alpha + ThreadSafeFastRandom.NextDouble() * AlphaWidth;

                    for (int i = 0; ; i++)
                    {
                        int r = ThreadSafeFastRandom.Next(ngrams.Count);
                        UpdateProbabilities(probs, ngrams[r], alpha);
                        if (i % 5 == 0 && (NormalizeProbabilities(probs) > ConvergenceThreshold || i >= MaxIterations))
                        {
                            break;
                        }
                    }

                    for (int j = 0; j < languageProbabilities.Length; j++)
                    {
                        languageProbabilities[j] += probs[j] / Trials;
                    }
                }

                return(SortProbabilities(languageProbabilities));
            }
            finally
            {
                _listPool.Return(ngrams);
            }
        }