コード例 #1
0
        /// <inheritdoc />
        public float Loss(Tensor y, Tensor expected, bool calculateGradient)
        {
            if (y == null)
            {
                throw new ArgumentNullException(nameof(y));
            }

            if (expected == null)
            {
                throw new ArgumentNullException(nameof(expected));
            }

            if (expected.Length != y.Length)
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentCulture,
                                                "The number of expected labels: {0} does not match the tensor length: {1}.",
                                                expected.Length,
                                                y.Length));
            }

            float[] yw = y.Weights;
            float[] ew = expected.Weights;

            if (calculateGradient)
            {
                Vectors.Copy(expected.Length, ew, 0, y.Gradient, 0);
            }

            if (y.Shape.Rank == 1)
            {
                return(Calculate(expected.Length, 0, 0));
            }
            else
            {
                int mb     = y.Shape.Axes[0];         // number of items in a mini-batch
                int mbsize = y.Shape.Strides[0];      // item size

                float loss = 0.0f;
                for (int i = 0, yi = 0, ei = 0; i < mb; i++, yi += mbsize, ei += mbsize)
                {
                    loss += Calculate(mbsize, yi, ei);
                }

                return(loss / mb);
            }

            float Calculate(int length, int offy, int offe)
            {
                return(Vectors.EuclideanDistance(length, yw, offy, ew, offe) / length);
            }
        }
コード例 #2
0
ファイル: EuclideanDistance.cs プロジェクト: arnavdas88/dnn
 public double Distance(double[] x, double[] y)
 {
     return(Vectors.EuclideanDistance(x.Length, x, 0, y, 0));
 }
コード例 #3
0
ファイル: EuclideanDistance.cs プロジェクト: arnavdas88/dnn
 public float Distance(float[] x, float[] y)
 {
     return(Vectors.EuclideanDistance(x.Length, x, 0, y, 0));
 }
コード例 #4
0
 public float EuclideanDistance(float[] y, int offy) => Vectors.EuclideanDistance(this.length, this.x, this.offset, y, offy);
コード例 #5
0
ファイル: DenseVectorF.cs プロジェクト: arnavdas88/dnn
 public float EuclideanDistance(float[] y, int offy) => Vectors.EuclideanDistance(this.x.Length, this.x, 0, y, offy);