Exemplo n.º 1
0
        public WeightMatrix repeatSX(WeightMatrix m, int p)
        {
            var res = new WeightMatrix(p, m.Columns, 0);

            for (int i = 0; i < p; i++)
            {
                for (int j = 0; j < m.Columns; j++)
                {
                    res.Set(i, j, m.Get(0, j));
                    //res.Set_Grad(i, j, 0, m.Get_Grad(0, j, 0));
                }
            }
            if (this.needs_backprop)
            {
                Action backward = () =>
                {
                    for (int i = 0; i < p; i++)
                    {
                        for (int j = 0; j < m.Columns; j++)
                        {
                            m.Add_Grad(0, j, res.Get_Grad(i, j));
                        }
                    }
                };
                this.backprop.Add(backward);
            }
            return(res);
        }
Exemplo n.º 2
0
        public List <WeightMatrix> splitSY(WeightMatrix merged)
        {
            List <WeightMatrix> vols = new List <WeightMatrix>();

            for (int j = 0; j < merged.Rows; j++)
            {
                var item = new WeightMatrix(1, merged.Columns, 0);
                int n    = merged.Columns;
                for (int i = 0; i < n; i++)
                {
                    item.Weight[i] = merged.Get(j, i);
                    //item.DW[i] = merged.Get_Grad(j, i, 0);
                }
                vols.Add(item);
            }
            if (this.needs_backprop)
            {
                Action backward = () =>
                {
                    for (int j = 0; j < vols.Count; j++)
                    {
                        var item = vols[j];
                        int n    = item.Columns;
                        for (int i = 0; i < n; i++)
                        {
                            merged.Add_Grad(j, i, item.Gradient[i]);
                        }
                    }
                };
                this.backprop.Add(backward);
            }
            return(vols);
        }
Exemplo n.º 3
0
        public WeightMatrix RepeatRows(WeightMatrix m1, int rows)
        {
            var res = new WeightMatrix(rows, m1.Columns, 0);

            for (var i = 0; i < rows; i++)
            {
                for (int j = 0; j < m1.Columns; j++)
                {
                    var el = m1.Get(0, j);
                    res.Set(i, j, el);
                    //  res.Set_Grad(i, j,m1.Get_Grad(i,j));
                }
            }

            if (this.needs_backprop)
            {
                Action backward = () =>
                {
                    for (var i = 0; i < rows; i++)
                    {
                        for (int j = 0; j < m1.Columns; j++)
                        {
                            var el = res.Get_Grad(i, j);
                            m1.Add_Grad(0, j, el);
                        }
                    }
                };
                this.backprop.Add(backward);
            }
            return(res);
        }
Exemplo n.º 4
0
        public WeightMatrix concatColumns(WeightMatrix m1, WeightMatrix m2)
        {
            int sx = 1;
            int sy = 1;

            sy = m1.Columns + m2.Columns;
            sx = m1.Rows;

            var res = new WeightMatrix(sx, sy, 0);
            var n   = m1.Weight.Length;

            for (var i = 0; i < m1.Rows; i++)
            {
                for (int j = 0; j < m1.Columns; j++)
                {
                    var el = m1.Get(i, j);
                    res.Set(i, j, el);
                    //res.Set_Grad(i, j, 0, m1.Get_Grad(i, j, 0));
                }
            }
            for (var i = 0; i < m2.Rows; i++)
            {
                for (int j = m1.Columns; j < m2.Columns + m1.Columns; j++)
                {
                    var el = m2.Get(i, j - m1.Columns);
                    res.Set(i, j, el);
                    //res.Set_Grad(i, j, 0, m2.Get_Grad(i, j - m1.SY, 0));
                }
            }

            if (this.needs_backprop)
            {
                Action backward = () =>
                {
                    for (var i = 0; i < m1.Rows; i++)
                    {
                        for (int j = 0; j < m1.Columns; j++)
                        {
                            var el = res.Get_Grad(i, j);
                            m1.Add_Grad(i, j, el);
                        }
                    }
                    for (var i = 0; i < m2.Rows; i++)
                    {
                        for (int j = m1.Columns; j < m2.Columns + m1.Columns; j++)
                        {
                            var el = res.Get_Grad(i, j);
                            m2.Add_Grad(i, j - m1.Columns, el);
                        }
                    }
                };
                this.backprop.Add(backward);
            }
            return(res);
        }
Exemplo n.º 5
0
        public WeightMatrix sumColumns(WeightMatrix m1)
        {
            var res = new WeightMatrix(1, m1.Columns, 0);

            for (int x = 0; x < m1.Rows; x++)
            {
                for (int y = 0; y < m1.Columns; y++)
                {
                    //var ix = ((m1.Columns * x) + y);
                    //res.Weight[y] += m1.Weight[ix]  ;
                    //  res.Add(x, y, m1.Get(x, y) * weightRow.Weight[x]);

                    res.Add(0, y, m1.Get(x, y));
                }
            }


            if (this.needs_backprop)
            {
                Action backward = () =>
                {
                    for (int x = 0; x < m1.Rows; x++)
                    {
                        for (int y = 0; y < m1.Columns; y++)
                        {
                            //var ix = ((m1.Columns * x) + y);

                            //m1.Gradient[ix] += res.Gradient[y];


                            m1.Add_Grad(x, y, res.Get_Grad(0, y));
                        }
                    }
                    //for (int i = 0, n = m1.Weight.Length; i < n; i++)
                    //{
                    //    m1.Gradient[i] += weightRow.Weight[0] * res.Gradient[i];
                    //    weightRow.Gradient[0] += m1.Weight[i] * res.Gradient[i];

                    //}
                };
                this.backprop.Add(backward);
            }
            return(res);
        }