Exemple #1
0
        protected NdArray ForwardCpu(NdArray x)
        {
            int[] inputShape  = x.Shape;
            int[] outputShape = this.Weight.Shape;

            List <int> shapeList = new List <int>();

            for (int i = 0; i < this.Axis; i++)
            {
                shapeList.Add(1);
            }

            shapeList.AddRange(outputShape);

            for (int i = 0; i < inputShape.Length - this.Axis - outputShape.Length; i++)
            {
                shapeList.Add(1);
            }

            int[] preShape = shapeList.ToArray();

            NdArray y1 = new Reshape(preShape).Forward(this.Weight)[0];
            NdArray y2 = new Broadcast(inputShape).Forward(y1)[0];

            if (BiasTerm)
            {
                NdArray b1 = new Reshape(preShape).Forward(this.Bias)[0];
                NdArray b2 = new Broadcast(inputShape).Forward(b1)[0];

                return(x * y2 + b2);
            }
            else
            {
                return(x * y2);
            }
        }
Exemple #2
0
        public NdArray <T> MultiplyScaleForward(NdArray <T> x)
        {
            int[] inputShape  = x.Shape;
            int[] outputShape = this.Weight.Shape;

            List <int> shapeList = new List <int>();

            for (int i = 0; i < this.Axis; i++)
            {
                shapeList.Add(1);
            }

            shapeList.AddRange(outputShape);

            for (int i = 0; i < inputShape.Length - this.Axis - outputShape.Length; i++)
            {
                shapeList.Add(1);
            }

            int[] preShape = shapeList.ToArray();

            NdArray <T> y1 = new Reshape <T>(preShape).Forward(this.Weight)[0];
            NdArray <T> y2 = new Broadcast <T>(inputShape).Forward(y1)[0];

            if (BiasTerm)
            {
                NdArray <T> b1 = new Reshape <T>(preShape).Forward(this.Bias)[0];
                NdArray <T> b2 = new Broadcast <T>(inputShape).Forward(b1)[0];

                return(x * y2 + b2);
            }
            else
            {
                return(x * y2);
            }
        }