Пример #1
0
        /// <summary>
        /// Evaluates the specified feed dictionary.
        /// </summary>
        /// <param name="feedDict">The feed dictionary.</param>
        /// <remarks>
        /// # 'x' is [[1, 1, 1]
        /// #         [1, 1, 1]]
        /// tf.reduce_sum(x) ==> 6
        /// tf.reduce_sum(x, 0) ==> [2, 2, 2]
        /// tf.reduce_sum(x, 1) ==> [3, 3]
        /// tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
        /// tf.reduce_sum(x, [0, 1]) ==> 6</remarks>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException">
        /// </exception>
        public override Matrix <float> Evaluate(Dictionary <string, Matrix <float> > feedDict)
        {
            Matrix <float> input = InputTensor.Evaluate(feedDict);
            Matrix <float> sum   = Matrix <float> .Build.Dense(1, 1, 0);

            if (Axis == ReduceSumAxis.None)
            {
                for (int i = 0; i < input.RowCount; i++)
                {
                    for (int j = 0; j < input.ColumnCount; j++)
                    {
                        sum += input[i, j];
                    }
                }
            }
            else if (Axis == ReduceSumAxis.X)
            {
                if (!KeepDimensions)
                {
                    int endCount = input.RowCount;
                    sum = Matrix <float> .Build.Dense(1, endCount, 0);

                    for (int i = 0; i < endCount; i++)
                    {
                        float rowSum = 0f;
                        for (int j = 0; j < input.ColumnCount; j++)
                        {
                            rowSum += input[i, j];
                        }
                        sum[0, i] = rowSum;
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else if (Axis == ReduceSumAxis.Y)
            {
                if (!KeepDimensions)
                {
                    int endCount = input.ColumnCount;
                    sum = Matrix <float> .Build.Dense(1, endCount, 0);

                    for (int j = 0; j < endCount; j++)
                    {
                        float colSum = 0f;
                        for (int i = 0; i < input.RowCount; i++)
                        {
                            colSum += input[i, j];
                        }
                        sum[0, j] = colSum;
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            return(sum);
        }