Example #1
0
        /// <summary>
        /// Setup function for shaping the input and output arrays to broadcast compatible shapes.
        /// If no output array is given, a compatible output array is created
        /// </summary>
        /// <typeparam name="Ta">The type of input data to operate on</typeparam>
        /// <typeparam name="Tb">The type of output data to operate on</typeparam>
        /// <param name="in1">The left-hand-side input argument</param>
        /// <param name="in2">The right-hand-side input argument</param>
        /// <param name="out">The output target</param>
        /// <returns>A tupple with broadcast compatible shapes for the inputs, and an output array</returns>
        private static Tuple <NdArray <Ta>, NdArray <Ta>, NdArray <Tb> > SetupApplyHelper <Ta, Tb>(NdArray <Ta> in1, NdArray <Ta> in2, NdArray <Tb> @out)
        {
            Tuple <Shape, Shape> broadcastshapes = Shape.ToBroadcastShapes(in1.Shape, in2.Shape);

            if (@out == null)
            {
                //We allocate a new array
                @out = new NdArray <Tb>(broadcastshapes.Item1.Plain);
            }
            else
            {
                if (@out.Shape.Dimensions.Length != broadcastshapes.Item1.Dimensions.Length)
                {
                    throw new Exception("Target array does not have the right number of dimensions");
                }

                for (long i = 0; i < @out.Shape.Dimensions.Length; i++)
                {
                    if (@out.Shape.Dimensions[i].Length != broadcastshapes.Item1.Dimensions[i].Length)
                    {
                        throw new Exception("Dimension size of target array is incorrect");
                    }
                }
            }

            var op1 = in1.Reshape(broadcastshapes.Item1);
            var op2 = in2.Reshape(broadcastshapes.Item2);

            return(new Tuple <NdArray <Ta>, NdArray <Ta>, NdArray <Tb> >(op1, op2, @out));
        }
Example #2
0
        NdArray ForwardCpu([NotNull] NdArray val)
        {
            NdArray result = val.Clone();

            result.ParentFunc = this;
            result.Reshape(false, Shape);

            return(result);
        }
Example #3
0
        NdArray ForwardCpu(NdArray val)
        {
            NdArray result = val.Clone();

            result.ParentFunc = this;
            result.Reshape(this.Shape);

            return(result);
        }
        /// <summary>
        /// Pads the NdArray from the left with size-one dimensions until it has at least the specified number of
        /// dimensions.
        /// </summary>
        /// <param name="minNumDim">The minimum number of dimensions.</param>
        /// <param name="source">The NdArray to operate on.</param>
        /// <returns>A NdArray with at least <paramref name="minNumDim"/> dimensions.</returns>
        public static NdArray <T> AtLeastNd(int minNumDim, NdArray <T> source)
        {
            if (source.NumDimensions >= minNumDim)
            {
                return(source);
            }

            var newShape = Enumerable.Repeat(1, minNumDim - source.NumDimensions).Concat(source.Shape).ToArray();

            return(source.Reshape(newShape));
        }
Example #5
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Initializes a new instance of the KelpNet.Functions.Connections.Convolution2D class.
        /// </summary>
        ///
        /// <param name="linear">   The linear. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public Convolution2D(bool verbose, [NotNull] Linear linear)
            : base(verbose, FUNCTION_NAME, linear.Activator, new[] { new KeyValuePair <string, string>(PARAM_NAME, PARAM_VALUE) }, linear.Name, linear.InputNames, linear.OutputNames, linear.GpuEnable)
        {
            _kWidth    = 1;
            _kHeight   = 1;
            _strideX   = 1;
            _strideY   = 1;
            _padX      = 0;
            _padY      = 0;
            Parameters = linear.Parameters;
            Weight     = linear.Weight;
            Weight.Reshape(verbose, OutputCount, InputCount, _kHeight, _kWidth);
            Bias   = linear.Bias;
            NoBias = linear.NoBias;
        }
Example #6
0
        /// <summary>
        /// Repeats the NdArray along an axis.
        /// </summary>
        /// <param name="axis">The axis to repeat along.</param>
        /// <param name="repeats">The number of repetitions.</param>
        /// <param name="source">The NdArray to repeat.</param>
        /// <returns>The repeated NdArray.</returns>
        public static NdArray <T> Replicate(int axis, int repeats, NdArray <T> source)
        {
            NdArray <T> .CheckAxis(axis, source);

            if (repeats < 0)
            {
                throw new ArgumentException("Number of repetitions cannot be negative.", "repeats");
            }

            // 1. insert axis of size one left to repetition axis
            // 2. broadcast along the new axis to number of repetitions
            // 3. reshape to result shape
            var step1 = source.Reshape(List.Insert(axis, 1, source.Shape));
            var step2 = NdArray <T> .BraodcastDim(axis, repeats, step1);

            var step3 = step2.Reshape(List.Set(axis, repeats * source.Shape[axis], source.Shape));

            return(step3);
        }
 /// <summary>
 /// Flattens the NdArray into a (one-dimensional) vector.
 /// </summary>
 /// <param name="source">The NdArray to operate on.</param>
 /// <returns>A vector.</returns>
 public static NdArray <T> Flatten(NdArray <T> source)
 {
     return(source.Reshape(new[] { SpecialIdx.Remainder }));
 }
Example #8
0
 private static void FillDiagonal(NdArray a, DATA val)
 {
     long d  = a.Shape.Dimensions[0].Length;
     a.Reshape(new NumCIL.Shape(new long[] { d }, 0, new long[] { d+1 })).Set(val);
 }
Example #9
0
        private static void FillDiagonal(NdArray a, DATA val)
        {
            long d = a.Shape.Dimensions[0].Length;

            a.Reshape(new NumCIL.Shape(new long[] { d }, 0, new long[] { d + 1 })).Set(val);
        }