예제 #1
0
        public override KeyValuePair <double, double[, , ]> WriteG(int pos, double[,,] input, double[,,] t, double k)
        {
            List <IOPair> pairs;
            double        err;

            double[][,,] errors = GetBackwardErrors(input, t, out err, out pairs);

            layers[0].WriteG(pos, errors[0], input, k);

            for (int i = 1; i < layers.Count; i++)
            {
                layers[i].WriteG(pos, errors[i], pairs[i - 1].Key, k);
            }

            int depth  = memory_layer.output_size[0];
            int height = memory_layer.output_size[1];
            int width  = memory_layer.output_size[2];

            #region  асчет ошибки для входа
            double[,,] diff = doubleArrayExtensions.CreateArray(layers[0].input_size);
            diff.ForEach((val) => 1);
            var true_diff = memory_layer.GetOutputAndDiff(cached_last_layer).Value;
            for (int z = 0; z < depth; z++)
            {
                int z2 = z + z_offset;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        diff[z2, y, x] = true_diff[z, y, x];
                    }
                }
            }
            var error = layers[0].CreateInpuut();
            layers[0].GetError(errors[0], diff, input, error);
            #endregion

            #region Запись градиента для рекурсивного слоя
            double[,,] new_err = doubleArrayExtensions.CreateArray(memory_layer.output_size);

            for (int z = 0; z < depth; z++)
            {
                int z2 = z + z_offset;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        new_err[z, y, x] = error[z2, y, x];
                    }
                }
            }
            memory_layer.WriteG(pos, new_err, cached_last_layer, k);
            #endregion

            return(new KeyValuePair <double, double[, , ]>(Math.Sqrt(err / t.Length), pairs.Last().Key));
        }