示例#1
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);
        }
示例#2
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);
        }
示例#3
0
        public WeightMatrix mergeSY(List <WeightMatrix> vols)
        {
            WeightMatrix merged = new WeightMatrix(vols.Count, vols[0].Columns, 0);

            for (int j = 0; j < vols.Count; j++)
            {
                var item = vols[j];
                int n    = item.Columns;
                for (int i = 0; i < n; i++)
                {
                    merged.Set(j, i, item.Weight[i]);
                    //merged.Set_Grad(j, i, 0, item.DW[i]);
                }
            }
            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++)
                        {
                            item.Gradient[i] += merged.Get_Grad(j, i);
                        }
                    }
                };
                this.backprop.Add(backward);
            }
            return(merged);
        }
示例#4
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);
        }
示例#5
0
        public WeightMatrix concatRows(List <WeightMatrix> m1)
        {
            var res = new WeightMatrix(m1.Count, m1[0].Columns, 0);

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

            if (this.needs_backprop)
            {
                Action backward = () =>
                {
                    for (var i = 0; i < m1.Count; i++)
                    {
                        for (int j = 0; j < m1[i].Columns; j++)
                        {
                            var el = res.Get_Grad(i, j);
                            m1[i].Add_Grad(0, j, el);
                        }
                    }
                };
                this.backprop.Add(backward);
            }
            return(res);
        }
示例#6
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);
        }