Example #1
0
        public override void CalculateGradients(Tensor nextLayerGradients)
        {
            for (int x = 0; x < In.Size.X; x++)
            {
                for (int y = 0; y < In.Size.Y; y++)
                {
                    Range range = MapToOutput(x, y);
                    for (int z = 0; z < In.Size.Z; z++)
                    {
                        float sum_error = 0;
                        for (int i = range.MinX; i <= range.MaxX; i++)
                        {
                            int minx = i * Stride;
                            for (int j = range.MinY; j <= range.MaxY; j++)
                            {
                                int miny = j * Stride;

                                int is_max = (In.Get(x, y, z) == Out.Get(i, j, z)) ? 1 : 0;
                                sum_error += is_max * nextLayerGradients.Get(i, j, z);
                            }
                        }
                        GradsIn.Set(x, y, z, sum_error);
                    }
                }
            }
        }
Example #2
0
        public override void CalculateGradients(Tensor nextLayerGradients)
        {
            for (int x = 0; x < GradsIn.Size.X; x++)
            {
                for (int y = 0; y < GradsIn.Size.Y; y++)
                {
                    for (int z = 0; z < GradsIn.Size.Z; z++)
                    {
                        GradsIn.Set(x, y, z, 0f);
                    }
                }
            }

            for (int n = 0; n < Out.Size.X; n++)
            {
                Gradients[n] = nextLayerGradients.Get(n, 0, 0) * (float)Utilities.SigmoidDerivative(Input[n]);
                for (int i = 0; i < In.Size.X; i++)
                {
                    for (int j = 0; j < In.Size.Y; j++)
                    {
                        for (int z = 0; z < In.Size.Z; z++)
                        {
                            int m         = Map(i, j, z);
                            var oldGradIn = GradsIn.Get(i, j, z);
                            GradsIn.Set(i, j, z, oldGradIn + Gradients[n] * Weights.Get(m, n, 0));
                        }
                    }
                }
            }
        }
Example #3
0
 public override void CalculateGradients(Tensor nextLayerGradients)
 {
     for (int i = 0; i < In.Size.X; i++)
     {
         for (int j = 0; j < In.Size.Y; j++)
         {
             for (int z = 0; z < In.Size.Z; z++)
             {
                 float value = (In.Get(i, j, z) < 0) ? 0 : nextLayerGradients.Get(i, j, z);
                 GradsIn.Set(i, j, z, value);
             }
         }
     }
 }
        public override void CalculateGradients(Tensor nextLayerGradients)
        {
            for (int k = 0; k < Gradients.Length; k++)
            {
                for (int i = 0; i < ExtendFilter; i++)
                {
                    for (int j = 0; j < ExtendFilter; j++)
                    {
                        for (int z = 0; z < In.Size.Z; z++)
                        {
                            Gradients[k].Set(i, j, z, 0);
                        }
                    }
                }
            }

            for (int x = 0; x < In.Size.X; x++)
            {
                for (int y = 0; y < In.Size.Y; y++)
                {
                    Range range = MapToOutput(x, y);
                    for (int z = 0; z < In.Size.Z; z++)
                    {
                        float sumError = 0;
                        for (int i = range.MinX; i <= range.MaxX; i++)
                        {
                            int minX = i * Stride;
                            for (int j = range.MinY; j <= range.MaxY; j++)
                            {
                                int minY = j * Stride;
                                for (int k = range.MinZ; k <= range.MaxZ; k++)
                                {
                                    int wApplied = (int)Filters[k].Get(x - minX, y - minY, z); //TODO eigentlich int
                                    sumError += wApplied * nextLayerGradients.Get(i, j, k);
                                    var oldValue = Gradients[k].Get(x - minX, y - minY, z);
                                    Gradients[k].Set(x - minX, y - minY, z, oldValue + In.Get(x, y, z) * nextLayerGradients.Get(i, j, k));
                                }
                            }
                        }
                        GradsIn.Set(x, y, z, sumError);
                    }
                }
            }
        }