Esempio n. 1
0
        private static NdArray <Real> LocalMin(NdArray <Real> input, int axis)
        {
            int[] resultShape = new int[input.Shape.Length - 1];

            for (int i = 0, j = 0; i < input.Shape.Length; i++)
            {
                if (i != axis)
                {
                    resultShape[j++] = input.Shape[i];
                }
            }

            NdArray <Real> result = new NdArray <Real>(resultShape, input.BatchCount);

            result.Fill(input.Data.Min());
            if (input.Grad != null)
            {
                result.FillGrad(input.Grad.Min());
            }

            for (int i = 0; i < input.Length; i++)
            {
                List <int> index = new List <int>(input.GetDimensionsIndex(i));
                index.RemoveAt(axis);
                int localIndex = result.GetLocalIndex(0, index.ToArray());

                for (int batchCount = 0; batchCount < input.BatchCount; batchCount++)
                {
                    if (result.Data[batchCount * result.Length + localIndex] > input.Data[batchCount * input.Length + i])
                    {
                        result.Data[batchCount * result.Length + localIndex] = input.Data[batchCount * input.Length + i];
                    }
                    if (input.Grad != null)
                    {
                        if (result.Grad[batchCount * result.Length + localIndex] > input.Grad[batchCount * input.Length + i])
                        {
                            result.Grad[batchCount * result.Length + localIndex] = input.Grad[batchCount * input.Length + i];
                        }
                    }
                }
            }

            return(result);
        }