Ejemplo n.º 1
0
        public static Slice[] SlicesWithoutBias(this Array <Real> a)
        {
            var slices = a.Slices();

            slices[slices.Length - 1] = Slicer.Upto(-1);
            return(slices);
        }
Ejemplo n.º 2
0
        public static Array <Real> CombineWithBias(this Array <Real> t, Array <Real> x, Array <Real> y,
                                                   Array <Real> result = null, Real beta = 0)
        {
            if (t.Shape.Length != 3 && x.Shape.Length != 1 && y.Shape.Length != 1)
            {
                throw new ArgumentException();
            }
            if (t.Shape[2] != x.Shape[0] + 1 && t.Shape[1] != y.Shape[0] + 1)
            {
                throw new ArgumentException();
            }

            result = t[_, Slicer.Upto(-1), Slicer.Upto(-1)].Combine21(x, y, result: result);

            // TODO check this mess
            //var biasY = t[_, Slicer.Until(-1), -1].Dot(y);
            t[_, Slicer.Upto(-1), -1].Dot(y, result: result, beta: 1); //Doesn't work actually
            //int offY = y.offset[0];
            //int offT = t.offset[0] + t.offset[1] + t.offset[2] + (t.Shape[2] - 1) * t.Stride[2];
            //for (int j = 0; j < y.Shape[0]; ++j)
            //{
            //    Blas.axpy(t.Shape[0], y.Values[offY], t.Values, offT, t.Stride[0], result.Values, result.offset[0], result.Stride[0]);
            //    offY += y.Stride[0];
            //    offT += t.Stride[1];
            //}

            //var biasX = t[_, -1, Slicer.Until(-1)].Dot(x);
            t[_, -1, Slicer.Upto(-1)].Dot(x, result: result, beta: 1);

            //var biasXY = t[_, -1, -1];
            result.Acc(t[_, -1, -1]);

            //result = result + biasX + biasY + biasXY;
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Fills the result array using the value from a, and the indexes from selected.
        /// </summary>
        /// <typeparam name="T">The type of a content</typeparam>
        /// <param name="thiz"></param>
        /// <param name="selected">the result of a ArgMax/ArgMin operation</param>
        /// <param name="axis"></param>
        /// <param name="axisSize"></param>
        /// <param name="keepDims"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static Array <T> UnArgmax <T>(this Array <T> thiz, Array <int> selected, int axis, int axisSize, bool keepDims = false, Array <T> result = null)
        {
            thiz.AssertOfShape(selected);
            var dim   = thiz.NDim + (keepDims ? 0 : 1);
            var shape = new int[dim];

            if (keepDims)
            {
                System.Array.Copy(thiz.Shape, shape, dim);
            }
            else
            {
                System.Array.Copy(thiz.Shape, 0, shape, 0, axis);
                System.Array.Copy(thiz.Shape, axis, shape, axis + 1, thiz.NDim - axis);
            }
            shape[axis] = axisSize;
            result      = result ?? NN.Zeros <T>(shape);
            result.AssertOfShape(shape);
            var resultInc = result.Stride[axis];

            // HACK
            // as result have one more shape than thiz and selected, we have to lie about the number of shapes
            var resultSlices = new Slice[dim];

            for (int i = 0; i < dim; ++i)
            {
                resultSlices[i] = Slicer._;
            }
            if (!keepDims)
            {
                resultSlices[axis] = 0;
            }
            else
            {
                resultSlices[axis] = Slicer.Upto(1);
            }
            var res = result[resultSlices];

            Array_.ElementwiseOp(thiz, selected, res,
                                 (n, x, offsetx, incx, s, offsetS, incS, r, offsetR, incR) =>
            {
                for (int i = 0; i < n; ++i)
                {
                    r[offsetR + resultInc * s[offsetS]] = x[offsetx];
                    offsetR += incR;
                    offsetS += incS;
                    offsetx += incx;
                }
            });

            return(result);
        }
Ejemplo n.º 4
0
        public static Array <T> Concat <T>(int axis, Array <T>[] inputs, Array <T> result)
        {
            var slices = result.Slices();

            slices[axis] = Slicer.Upto(inputs[0].Shape[axis]);
            var view = result[slices];

            Copy(inputs[0], view);
            var stride = view.Stride[axis];

            for (int i = 1; i < inputs.Length; ++i)
            {
                view.Offset     += stride * inputs[i - 1].Shape[axis];
                view.Shape[axis] = inputs[i].Shape[axis];
                Copy(inputs[i], view);
            }
            return(result);
        }