Exemple #1
0
 public IVector FindDistances(IReadOnlyList <IVector> data, DistanceMetric distance)
 {
     Debug.Assert(IsValid && data.All(v => v.IsValid));
     if (distance == DistanceMetric.Cosine)
     {
         var norm     = DotProduct(this);
         var dataNorm = data.Select(d => d.DotProduct(d)).ToList();
         var ret      = new float[data.Count];
         for (var i = 0; i < data.Count; i++)
         {
             ret[i] = Convert.ToSingle(1d - DotProduct(data[i]) / Math.Sqrt(norm * dataNorm[i]));
         }
         return(_cuda.CreateVector(data.Count, i => ret[i]));
     }
     else if (distance == DistanceMetric.Euclidean || distance == DistanceMetric.Manhattan)
     {
         var ret = _cuda.CalculateDistances(new[] { this }, data, distance);
         return(ret.ReshapeAsVector());
     }
     else
     {
         var distanceFunc = _GetDistanceFunc(distance);
         var ret          = new float[data.Count];
         for (var i = 0; i < data.Count; i++)
         {
             ret[i] = distanceFunc(data[i]);
         }
         return(_cuda.CreateVector(data.Count, i => ret[i]));
     }
 }
Exemple #2
0
        public IVector ColumnL2Norm()
        {
            Debug.Assert(IsValid);
            var norm = new List <float>();

            for (var i = 0; i < _columns; i++)
            {
                using (var col = Column(i))
                    norm.Add(col.L2Norm());
            }
            return(_cuda.CreateVector(norm.Count, x => norm[x]));
        }