Exemple #1
0
        public double[,,] FastFunc(double[,,] val)
        {
            double max = double.MinValue;

            val.ForEach(x => { max = Math.Max(max, x); });
            val.ForEach(x => x / max);
            return(val);
        }
        public double[,,] FastFunc(double[,,] val)
        {
            double sum = 0;

            val.ForEach(x => { sum += x * x; });
            val.ForEach(x => x * x / sum);
            return(val);
        }
Exemple #3
0
        public void SyncDiff(double[,,] val, double[,,] result)
        {
            double max = double.MinValue;

            val.ForEach(x => { max = Math.Max(max, Math.Abs(x)); });
            result.ForEach(val, (y, x) => x == max ? 0 : 1);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                pretrain.SetupPretrain(listBox1.SelectedIndex);

                double[,,] res = pretrain.ProcessBackground(base_arr);
                double max = -1;
                res.ForEach((x) => { max = Math.Max(max, x); });
                res.ForEach((x) => x / max);
                //pictureBox1.Image = ImageDataConverter.GetImage(res.ConvertToRGD());

                RunPretrain();
                timer1.Start();
            }
        }
Exemple #5
0
        public override Value PassForward(Value value)
        {
            _featureMaps.ForEach((k, i, j) =>
                                 _featureMaps[k, i, j] = _activator.CalculateValue(value.Multi[k, i, j]));

            return(new MultiValue(_featureMaps));
        }
Exemple #6
0
        public double[,,] Func(double[,,] val)
        {
            double max = double.MinValue;

            val.ForEach(x => { max = Math.Max(max, Math.Abs(x)); });
            return(val.NewEach(x => x / max));
        }
Exemple #7
0
        public void RandomizeWeights(IWeightInitializer weightInitializer)
        {
            int    inputs    = Weights.GetLength(0) * Weights.GetLength(1) * Weights.GetLength(2);
            double magnitude = Math.Sqrt((double)1 / inputs);

            Weights.ForEach((i, j, k) => Weights[i, j, k] = weightInitializer.GenerateRandom(magnitude));
        }
Exemple #8
0
        public double[,,] Diff(double[,,] val)
        {
            double max = double.MinValue;

            val.ForEach(x => { max = Math.Max(max, Math.Abs(x)); });
            return(val.NewEach(x => x == max ? 0 : 1));
        }
        double[][,,] GetBackwardErrors(double[,,] input, double[,,] t, out double err, out List <KeyValuePair <double[, , ], double[, , ]> > getOutput)
        {
            double[][,,] errors = new double[layers.Count][, , ];
            var out_pairs = GetOutAndDiff(input);

            double[,,] output = out_pairs.Last().Key;

            //Console.WriteLine("{0} {1}", (float)output[0, 0, 0], (float)output[0, 0, 1]);

            int depth2  = output.GetLength(0);
            int height2 = output.GetLength(1);
            int width2  = output.GetLength(2);

            double val  = 0;
            double temp = 0;

            double[,,] out_err = (double[, , ])t.Clone();

            double[,,] diff = out_pairs.Last().Value;

            for (int z2 = 0; z2 < depth2; z2++)
            {
                for (int y2 = 0; y2 < height2; y2++)
                {
                    for (int x2 = 0; x2 < width2; x2++)
                    {
                        temp = t[z2, y2, x2] - output[z2, y2, x2];
                        out_err[z2, y2, x2] = -temp * diff[z2, y2, x2];
                        val += temp * temp;
                    }
                }
            }

            errors[errors.Length - 1] = out_err;

            for (int i = layers.Count - 1; i >= 0; i--)
            {
                Layer prev = null;
                if (i != 0)
                {
                    prev = layers[i - 1];
                    if (prev != null)
                    {
                        errors[i - 1] = layers[i].GetError(errors[i], out_pairs[i - 1].Value, out_pairs[i - 1].Key);
                    }
                    else
                    {
                        double[,,] diff2 = (double[, , ])input.Clone();
                        diff2.ForEach(x => 1);
                        errors[i - 1] = layers[i].GetError(errors[i], diff2, input);
                    }
                }
            }

            err       = val;
            getOutput = out_pairs;

            return(errors);
        }
        public void SyncDiff(double[,,] val, double[,,] result)
        {
            double sum = 0;

            val.ForEach(x => { sum += x * x; });
            sum += c;
            result.ForEach(x => x * (1 - x / sum) / sum);
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (pretrain_task != null)
            {
                if (pretrain_task.IsCompleted)
                {
                    pretrain.UpdateWeights(ref NetworkData.network);
                    label1.Text    = "Error: " + (float)pretrain_task.Result;
                    double[,,] res = pretrain.ProcessAutoEncoder(base_arr);
                    ContinuePretrain();

                    double max = -1;
                    res.ForEach((x) => { max = Math.Max(max, x); });
                    res.ForEach((x) => x / max);
                    //pictureBox2.Image = ImageDataConverter.GetImage(res.ConvertToRGD());
                }
            }
        }
Exemple #12
0
        public static double[,,] Flip(double[,,] input)
        {
            int w      = input.GetLength(1);
            int h      = input.GetLength(2);
            var output = new double[input.GetLength(0), w, h];

            input.ForEach((q, k, i, j) => output[k, w - i - 1, h - j - 1] = q);
            return(output);
        }
Exemple #13
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));
        }
        public double[,,] Func(double[,,] val)
        {
            double sum = 0;

            val.ForEach(x => { sum += x * x; });
            sum += c;
            double[,,] result = (double[, , ])val.Clone();
            result.ForEach(x => x * x / sum);
            return(result);
        }
        KeyValuePair <double, double[, , ]> SetBackwardErrors(double[,,] input, double[,,] t)
        {
            double[,,] output = GetOutput(input);

            //Console.WriteLine("{0} {1}", (float)output[0, 0, 0], (float)output[0, 0, 1]);

            int depth2  = output.GetLength(0);
            int height2 = output.GetLength(1);
            int width2  = output.GetLength(2);

            double val  = 0;
            double temp = 0;

            double[,,] out_err = errors.Last();

            double[,,] diff = layers.Last().GetCachedDiff();

            for (int z2 = 0; z2 < depth2; z2++)
            {
                for (int y2 = 0; y2 < height2; y2++)
                {
                    for (int x2 = 0; x2 < width2; x2++)
                    {
                        temp = t[z2, y2, x2] - output[z2, y2, x2];
                        out_err[z2, y2, x2] = -temp * diff[z2, y2, x2];
                        val += temp * temp;
                    }
                }
            }

            errors[errors.Count - 1] = out_err;

            for (int i = layers.Count - 1; i >= 0; i--)
            {
                Layer prev = null;
                if (i != 0)
                {
                    prev = layers[i - 1];
                    if (prev != null)
                    {
                        errors[i - 1] = layers[i].GetError(errors[i], prev.GetCachedDiff(), prev.GetCachedOutpur());
                    }
                    else
                    {
                        double[,,] diff2 = (double[, , ])input.Clone();
                        diff2.ForEach(x => 1);
                        errors[i - 1] = layers[i].GetError(errors[i], diff2, input);
                    }
                }
            }

            return(new KeyValuePair <double, double[, , ]>(val, output));
        }
Exemple #16
0
        public static double[,,] Pad(double[,,] input, int padding)
        {
            int d = input.GetLength(0);
            int w = input.GetLength(1) + padding * 2;
            int h = input.GetLength(2) + padding * 2;

            var output = new double[d, w, h];

            input.ForEach((q, k, i, j) => output[k, i + padding, j + padding] = q);

            return(output);
        }
        public KeyValuePair <double, double> GetErrorPair(double[,,] input, double[,,] t)
        {
            double[,,] output = GetOutput(input);

            int depth2  = output.GetLength(0);
            int height2 = output.GetLength(1);
            int width2  = output.GetLength(2);

            double err  = 0;
            double temp = 0;

            for (int z2 = 0; z2 < depth2; z2++)
            {
                for (int y2 = 0; y2 < height2; y2++)
                {
                    for (int x2 = 0; x2 < width2; x2++)
                    {
                        temp = output[z2, y2, x2] - t[z2, y2, x2];
                        err += temp * temp;
                    }
                }
            }

            double max     = -1;
            int    max_arg = -1;

            t.ForEach((val, z, y, x) =>
            {
                if (val > max)
                {
                    max_arg = x;
                    max     = val;
                }
            });

            max = -1;
            int max_arg2 = -2;

            output.ForEach((val, z, y, x) =>
            {
                if (val > max)
                {
                    max_arg2 = x;
                    max      = val;
                }
            });

            return(new KeyValuePair <double, double>(err, max_arg == max_arg2 ? 1 : 0));
        }
        public override void GetError(double[,,] error_out, double[,,] diff, double[,,] indata, double[,,] input)
        {
            int dh = input.GetLength(1) - height_in + 1;
            int dw = input.GetLength(2) - width_in + 1;

            input.ForEach((x) => 0);

            double val = 0;

            for (int c1 = 0; c1 < ch_out; c1++)
            {
                for (int c2 = 0; c2 < ch_in; c2++)
                {
                    for (int y0 = 0; y0 < dh; y0++)
                    {
                        for (int x0 = 0; x0 < dw; x0++)
                        {
                            for (int y1 = 0; y1 < height_out; y1++)
                            {
                                for (int x1 = 0; x1 < width_out; x1++)
                                {
                                    for (int y2 = 0; y2 < height_in; y2++)
                                    {
                                        for (int x2 = 0; x2 < width_in; x2++)
                                        {
                                            input[c2, y0 + y2, x0 + x2] += weights[c1, y1, x1, c2, y2, x2] * error_out[c1, y0 + y1, x0 + x1];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            for (int c = 0; c < ch_in; c++)
            {
                for (int y = 0; y < input_size[1]; y++)
                {
                    for (int x = 0; x < input_size[2]; x++)
                    {
                        input[c, y, x] *= diff[c, y, x];
                    }
                }
            }
        }
Exemple #19
0
        public override void GetError(double[,,] error_out, double[,,] diff, double[,,] indata, double[,,] input)
        {
            int depth   = input_size[0];
            int height2 = input_size[1];
            int width2  = input_size[2];

            int dh = height2 - height + 1;
            int dw = width2 - width + 1;

            input.ForEach((x) => 0);

            for (int c = 0; c < count; c++)
            {
                for (int y0 = 0; y0 < dh; y0++)
                {
                    for (int x0 = 0; x0 < dw; x0++)
                    {
                        double val = 0;
                        for (int ch = 0; ch < depth; ch++)
                        {
                            for (int y = 0; y < height; y++)
                            {
                                for (int x = 0; x < width; x++)
                                {
                                    input[ch, y0 + y, x0 + x] += weights[c, ch, y, x] * error_out[c, y0, x0];
                                }
                            }
                        }
                    }
                }
            }

            for (int z = 0; z < depth; z++)
            {
                for (int y = 0; y < height2; y++)
                {
                    for (int x = 0; x < width2; x++)
                    {
                        input[z, y, x] *= diff[z, y, x];
                    }
                }
            }
        }
Exemple #20
0
        public SingleValue ToSingle()
        {
            if (Multi.Length == 0)
            {
                throw new Exception(Consts.CanNotConvertMultiValueToSingleValue);
            }

            double[] output = new double[Multi.Length];

            Multi.ForEach((i, j, k) =>
            {
                int depth  = Multi.GetLength(0);
                int width  = Multi.GetLength(1);
                int height = Multi.GetLength(2);

                output[i * width * height + j * width + k] = Multi[i, j, k];
            });

            return(new SingleValue(output));
        }
        public override void GetOutputAndDiff(double[,,] input, double[,,] output, double[,,] diff)
        {
            int dh = input.GetLength(1) - height_in + 1;
            int dw = input.GetLength(2) - width_in + 1;

            output.ForEach((x) => 0);

            double val = 0;

            for (int c1 = 0; c1 < ch_out; c1++)
            {
                for (int c2 = 0; c2 < ch_in; c2++)
                {
                    for (int y0 = 0; y0 < dh; y0++)
                    {
                        for (int x0 = 0; x0 < dw; x0++)
                        {
                            for (int y1 = 0; y1 < height_out; y1++)
                            {
                                for (int x1 = 0; x1 < width_out; x1++)
                                {
                                    val = 0;
                                    for (int y2 = 0; y2 < height_in; y2++)
                                    {
                                        for (int x2 = 0; x2 < width_in; x2++)
                                        {
                                            val += weights[c1, y1, x1, c2, y2, x2] * input[c2, y0 + y2, x0 + x2];
                                        }
                                    }

                                    output[c1, y0 + y1, x0 + x1] += val;
                                }
                            }
                        }
                    }
                }
            }

            func.SyncDiff(output, diff);
            func.FastFunc(output);
        }
        /// <summary>
        /// Возражаешь координаты положения максимального числа в массиве
        /// </summary>
        /// <param name="array"></param>
        /// <param name="c"></param>
        /// <param name="y"></param>
        /// <param name="x"></param>
        public static void ArgMax(this double[,,] array, out int c_res, out int y_res, out int x_res)
        {
            double max       = double.MinValue;
            int    max_arg_x = -1;
            int    max_arg_y = -1;
            int    max_arg_c = -1;

            array.ForEach((val, c, y, x) =>
            {
                if (val > max)
                {
                    max_arg_x = x;
                    max_arg_y = y;
                    max_arg_c = c;
                    max       = val;
                }
            });
            c_res = max_arg_c;
            y_res = max_arg_y;
            x_res = max_arg_x;
        }
        /// <summary>
        /// Возращает ошибку с процентами угаданных максимумов
        /// </summary>
        /// <param name="input"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual KeyValuePair <double, double> GetErrorPair(double[,,] input, double[,,] t)
        {
            double[,,] output = GetOutput(input);

            int depth2  = output.GetLength(0);
            int height2 = output.GetLength(1);
            int width2  = output.GetLength(2);

            var loss_err = loss.Metric(output, t);


            double max     = -1;
            int    max_arg = -1;

            t.ForEach((val, z, y, x) =>
            {
                if (val > max)
                {
                    max_arg = x;
                    max     = val;
                }
            });

            max = -1;
            int max_arg2 = -2;

            output.ForEach((val, z, y, x) =>
            {
                if (val > max)
                {
                    max_arg2 = x;
                    max      = val;
                }
            });

            return(new KeyValuePair <double, double>(loss_err, max_arg == max_arg2 ? 1 : 0));
        }
        protected virtual double[][,,] GetBackwardErrors(double[,,] input, double[,,] t, out double err, int pos)
        {
            GetOutAndDiff(input, pos);

            double[,,] output = cached_outputs[pos].Last();

            //Console.WriteLine("{0} {1}", (float)output[0, 0, 0], (float)output[0, 0, 1]);

            int depth2  = output.GetLength(0);
            int height2 = output.GetLength(1);
            int width2  = output.GetLength(2);


            double[,,] out_err = (double[, , ])t.Clone();
            loss.Error(output, t, out_err);

            double[,,] diff = cached_diffs[pos].Last();

            #region  асчет ошибки конечного слоя
            for (int z2 = 0; z2 < depth2; z2++)
            {
                for (int y2 = 0; y2 < height2; y2++)
                {
                    for (int x2 = 0; x2 < width2; x2++)
                    {
                        out_err[z2, y2, x2] *= -diff[z2, y2, x2];
                    }
                }
            }
            #endregion


            #region Обратное распостронение ошибки
            cached_errors[pos][cached_errors[pos].Length - 1] = out_err;

            for (int i = layers.Count - 1; i >= 0; i--)
            {
                Layer prev = null;
                if (i != 0)
                {
                    prev = layers[i - 1];
                    if (prev != null)
                    {
                        layers[i].GetError(cached_errors[pos][i], cached_diffs[pos][i - 1], cached_outputs[pos][i - 1], cached_errors[pos][i - 1]);
                        //cached_errors[pos][i - 1].MaxNorm();
                    }
                    else
                    {
                        double[,,] diff2 = (double[, , ])input.Clone();
                        diff2.ForEach(x => 1);
                        layers[i].GetError(errors[i], diff2, input, cached_errors[pos][i - 1]);
                        //cached_errors[pos][i - 1].MaxNorm();
                    }
                }
            }
            #endregion

            err = loss.Metric(output, t);

            return(cached_errors[pos]);
        }
Exemple #25
0
 public override void ClearBuffers()
 {
     cached_last_layer.ForEach((val) => 0);
     cached_output.ForEach((val) => 0);
 }
 public void SyncDiff(double[,,] val, double[,,] result)
 {
     result.ForEach((x) => 1);
 }
Exemple #27
0
 public void SyncDiff(double[,,] val, double[,,] result)
 {
     result.ForEach(val, (y, x) => x >= 0 ? 1 : 0);
 }
Exemple #28
0
 public double[,,] FastFunc(double[,,] val)
 {
     val.ForEach(x => x > 0 ? x : 0);
     return(val);
 }
Exemple #29
0
 public void UpdateWeights()
 {
     Gradient.ForEach((q, i, j, k) => Weights[i, j, k] += _learningRateAnnealers[i, j, k].Compute(q));
 }
 public void Error(double[,,] prediction, double[,,] output, double[,,] result)
 {
     output.ArgMax(out int cout, out int yout, out int xout);
     result.ForEach((v) => - 1);
     result[cout, yout, xout] = 1;
 }