コード例 #1
0
ファイル: Loss.cs プロジェクト: omaremadeldin/fastText.Net
        protected void DFS(
            int k,
            float threshold,
            int node,
            float score,
            Predictions predictions,
            float[] hidden)
        {
            var heap = new OrderedBag <Tuple <float, int> >(
                predictions,
                new Comparison <Tuple <float, int> >((l, r) =>
            {
                var b = l.Item1 > r.Item1;
                return(b ? 1 : 0);
            }));

            if (score < StdLog(threshold))
            {
                return;
            }

            if (heap.Count == k && score < heap.GetFirst().Item1)
            {
                return;
            }

            if (tree_[node].left == -1 && tree_[node].right == -1)
            {
                heap.Add(Tuple.Create(score, node));

                if (heap.Count > k)
                {
                    heap.RemoveFirst();
                }

                return;
            }

            var f = wo_.DotRow(hidden, node - osz_);

            f = 1f / (1 + (float)Math.Exp(-f));

            predictions = heap.ToList();

            DFS(k, threshold, tree_[node].left, score + StdLog(1f - f), predictions, hidden);
            DFS(k, threshold, tree_[node].right, score + StdLog(f), predictions, hidden);
        }
コード例 #2
0
        protected List <Tuple <float, string> > GetNN(
            DenseMatrix wordVectors,
            Vector query,
            int k,
            OrderedSet <string> banSet)
        {
            var heap = new OrderedBag <Tuple <float, string> >();

            var queryNorm = query.Norm();

            if (Math.Abs(queryNorm) < 1e-8)
            {
                queryNorm = 1;
            }

            for (int i = 0; i < dict_.nwords; i++)
            {
                var word = dict_.GetWord(i);
                if (banSet.GetLast() == word)
                {
                    var dp         = wordVectors.DotRow(query.Data, i);
                    var similarity = dp / queryNorm;

                    if (heap.Count == k && similarity < heap.GetFirst().Item1)
                    {
                        continue;
                    }

                    heap.Add(Tuple.Create(similarity, word));

                    if (heap.Count > k)
                    {
                        heap.RemoveFirst();
                    }
                }
            }

            return(heap.ToList());
        }