Ejemplo n.º 1
0
        public static Tensor Concat(this Session session, IList <Tensor> xs, int axis)
        {
            const string ActionName = "concat";

            if (axis < 0)
            {
                throw new ArgumentException(Properties.Resources.E_NegativeAxisIndex, nameof(axis));
            }

            return(session.RunOperation(
                       ActionName,
                       () =>
            {
                bool calculateGradient = session.CalculateGradients && xs.Any(x => x.CalculateGradient);

                Tensor y = session.AllocateTensor(ActionName, Shape.Concat(xs.Select(x => x.Shape).ToArray(), axis), calculateGradient);

                ArrayOperations.Concat(xs, axis, y, false);

#if !NOLEARNING
                if (calculateGradient)
                {
                    session.Push(ActionName, () => ArrayOperations.Split(y, axis, xs, true));
                }
#endif

                return y;
            }));
        }
Ejemplo n.º 2
0
        public static Tensor[] Split(this Session session, Tensor x, int axis, int[] sizes)
        {
            const string ActionName = "split";

            if (axis < 0)
            {
                throw new ArgumentException(Properties.Resources.E_NegativeAxisIndex, nameof(axis));
            }

            return(session.RunOperation(
                       ActionName,
                       () =>
            {
                bool calculateGradient = session.CalculateGradients && x.CalculateGradient;

                // check sizes
                int ydim = sizes.Length;
                int sum = 0;
                for (int i = 0; i < ydim; i++)
                {
                    sum += sizes[i];
                }

                if (sum != x.Shape.Axes[axis])
                {
                    throw new ArgumentException("The sub tensors sizes must be provided.");
                }

                // allocate destination
                Tensor[] ys = new Tensor[ydim];
                for (int i = 0; i < ydim; i++)
                {
                    ys[i] = session.AllocateTensor(ActionName, x.Shape.Reshape(axis, sizes[i]), calculateGradient);
                }

                ArrayOperations.Split(x, axis, ys, false);

#if !NOLEARNING
                if (calculateGradient)
                {
                    session.Push(ActionName, () => ArrayOperations.Concat(ys, axis, x, true));

                    // return copy of the array; calling method can replace its content; our closure keeps the array, not its items
                    return ys.ToArray();
                }
#endif

                return ys;
            }));
        }
Ejemplo n.º 3
0
        public static Tensor[] Split(this Session session, Tensor x, int axis, int numberOfSplits)
        {
            const string ActionName = "split";

            if (axis < 0)
            {
                throw new ArgumentException(Properties.Resources.E_NegativeAxisIndex, nameof(axis));
            }

            // check number of splits
            if ((x.Axes[axis] % numberOfSplits) != 0)
            {
                throw new ArgumentException("The number of tensors to split into must evenly divide the tensor split dimension.", nameof(numberOfSplits));
            }

            return(session.RunOperation(
                       ActionName,
                       () =>
            {
                bool calculateGradient = session.CalculateGradients && x.CalculateGradient;

                // allocate destination
                Tensor[] ys = session.AllocateTensors(
                    ActionName,
                    numberOfSplits,
                    x.Shape.Reshape(axis, x.Axes[axis] / numberOfSplits),
                    calculateGradient);

                ArrayOperations.Split(x, axis, ys, false);

#if !NOLEARNING
                if (calculateGradient)
                {
                    session.Push(ActionName, () => ArrayOperations.Concat(ys, axis, x, true));

                    // return copy of the array; calling method can replace its content; our closure keeps the array, not its items
                    return ys.ToArray();
                }
#endif

                return ys;
            }));
        }