SetWeight() public method

Set the weight between the two specified neurons.
public SetWeight ( int fromLayer, int fromNeuron, int toNeuron, double v ) : void
fromLayer int The from layer.
fromNeuron int The from neuron.
toNeuron int The to neuron.
v double The to value.
return void
        /// <summary>
        /// Randomize one level of a neural network.
        /// </summary>
        ///
        /// <param name="network">The network to randomize</param>
        /// <param name="fromLayer">The from level to randomize.</param>
        public override void Randomize(BasicNetwork network, int fromLayer)
        {
            int fromCount = network.GetLayerTotalNeuronCount(fromLayer);
            int toCount = network.GetLayerNeuronCount(fromLayer + 1);

            for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
            {
                for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                {
                    double v = CalculateValue(toCount);
                    network.SetWeight(fromLayer, fromNeuron, toNeuron, v);
                }
            }
        }
        /// <summary>
        /// Randomize one level of a neural network.
        /// </summary>
        ///
        /// <param name="network">The network to randomize</param>
        /// <param name="fromLayer">The from level to randomize.</param>
        public virtual void Randomize(BasicNetwork network, int fromLayer)
        {
            int fromCount = network.GetLayerTotalNeuronCount(fromLayer);
            int toCount = network.GetLayerNeuronCount(fromLayer + 1);

            for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
            {
                for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
                {
                    double v = network.GetWeight(fromLayer, fromNeuron, toNeuron);
                    v = Randomize(v);
                    network.SetWeight(fromLayer, fromNeuron, toNeuron, v);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Randomize the connections between two layers.
        /// </summary>
        /// <param name="network">The network to randomize.</param>
        /// <param name="fromLayer">The starting layer.</param>
        private void RandomizeSynapse(BasicNetwork network, int fromLayer)
        {
            int toLayer = fromLayer + 1;
            int toCount = network.GetLayerNeuronCount(toLayer);
            int fromCount = network.GetLayerNeuronCount(fromLayer);
            int fromCountTotalCount = network.GetLayerTotalNeuronCount(fromLayer);
            IActivationFunction af = network.GetActivation(toLayer);
            double low = CalculateRange(af, Double.NegativeInfinity);
            double high = CalculateRange(af, Double.PositiveInfinity);

            double b = 0.7d * Math.Pow(toCount, (1d / fromCount)) / (high - low);

            for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
            {
                if (fromCount != fromCountTotalCount)
                {
                    double w = RangeRandomizer.Randomize(-b, b);
                    network.SetWeight(fromLayer, fromCount, toNeuron, w);
                }
                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    double w = RangeRandomizer.Randomize(0, b);
                    network.SetWeight(fromLayer, fromNeuron, toNeuron, w);
                }
            }
        }
        /// <summary>
        /// Randomize one level of a neural network.
        /// </summary>
        ///
        /// <param name="network">The network to randomize</param>
        /// <param name="fromLayer">The from level to randomize.</param>
        public override void Randomize(BasicNetwork network, int fromLayer)
        {
            int fromCount = network.GetLayerTotalNeuronCount(fromLayer);
            int toCount = network.GetLayerNeuronCount(fromLayer + 1);

            for (int toNeuron = 0; toNeuron < toCount; toNeuron++)
            {
                double n = 0.0;
                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    double w = network.GetWeight(fromLayer, fromNeuron, toNeuron);
                    n += w * w;
                }
                n = Math.Sqrt(n);


                for (int fromNeuron = 0; fromNeuron < fromCount; fromNeuron++)
                {
                    double w = network.GetWeight(fromLayer, fromNeuron, toNeuron);
                    w = _beta * w / n;
                    network.SetWeight(fromLayer, fromNeuron, toNeuron, w);
                }
            }
        }
Esempio n. 5
0
 public override void Randomize(BasicNetwork network, int fromLayer)
 {
     int num2;
     int num3;
     double num4;
     int num5;
     double num6;
     int num7;
     double num8;
     int layerTotalNeuronCount = network.GetLayerTotalNeuronCount(fromLayer);
     goto Label_00DF;
     Label_0011:
     if (num3 < num2)
     {
         num4 = 0.0;
         num5 = 0;
     }
     else if ((((uint) num8) - ((uint) layerTotalNeuronCount)) >= 0)
     {
         return;
     }
     while (true)
     {
         if (num5 >= layerTotalNeuronCount)
         {
             num4 = Math.Sqrt(num4);
             num7 = 0;
             if ((((uint) num4) + ((uint) num2)) < 0)
             {
                 break;
             }
             goto Label_0065;
         }
         num6 = network.GetWeight(fromLayer, num5, num3);
         num4 += num6 * num6;
         num5++;
     }
     Label_0044:
     if ((((uint) fromLayer) + ((uint) num6)) > uint.MaxValue)
     {
         goto Label_00DF;
     }
     num7++;
     Label_0065:
     if (num7 < layerTotalNeuronCount)
     {
         num8 = network.GetWeight(fromLayer, num7, num3);
     }
     else
     {
         num3++;
         goto Label_0011;
     }
     Label_009C:
     num8 = (this._xd7d571ecee49d1e4 * num8) / num4;
     network.SetWeight(fromLayer, num7, num3, num8);
     goto Label_0044;
     Label_00DF:
     num2 = network.GetLayerNeuronCount(fromLayer + 1);
     if (((uint) num8) > uint.MaxValue)
     {
         goto Label_009C;
     }
     num3 = 0;
     goto Label_0011;
 }
Esempio n. 6
0
 public override void Randomize(BasicNetwork network, int fromLayer)
 {
     int num4;
     double num5;
     int layerTotalNeuronCount = network.GetLayerTotalNeuronCount(fromLayer);
     int layerNeuronCount = network.GetLayerNeuronCount(fromLayer + 1);
     int fromNeuron = 0;
     if (((uint) fromLayer) < 0)
     {
         goto Label_0012;
     }
     Label_000E:
     if (fromNeuron < layerTotalNeuronCount)
     {
         num4 = 0;
         goto Label_0054;
     }
     Label_0012:
     if (((uint) num5) > uint.MaxValue)
     {
         goto Label_0054;
     }
     if ((((uint) fromNeuron) + ((uint) fromNeuron)) >= 0)
     {
         return;
     }
     Label_003C:
     num5 = this.x7417261f548b2c9b(layerNeuronCount);
     network.SetWeight(fromLayer, fromNeuron, num4, num5);
     num4++;
     Label_0054:
     if (num4 < layerNeuronCount)
     {
         goto Label_003C;
     }
     fromNeuron++;
     goto Label_000E;
 }
Esempio n. 7
0
 public virtual void Randomize(BasicNetwork network, int fromLayer)
 {
     int num4;
     double num5;
     int layerTotalNeuronCount = network.GetLayerTotalNeuronCount(fromLayer);
     int layerNeuronCount = network.GetLayerNeuronCount(fromLayer + 1);
     int fromNeuron = 0;
     goto Label_002C;
     Label_000D:
     fromNeuron++;
     if ((((uint) fromNeuron) + ((uint) fromNeuron)) > uint.MaxValue)
     {
         goto Label_004B;
     }
     if (0 != 0)
     {
         goto Label_003C;
     }
     Label_002C:
     if (fromNeuron < layerTotalNeuronCount)
     {
         goto Label_0067;
     }
     return;
     Label_003C:
     network.SetWeight(fromLayer, fromNeuron, num4, num5);
     num4++;
     Label_004B:
     if (num4 < layerNeuronCount)
     {
         num5 = network.GetWeight(fromLayer, fromNeuron, num4);
         if (((uint) num5) >= 0)
         {
             num5 = this.Randomize(num5);
             goto Label_003C;
         }
         goto Label_000D;
     }
     if ((((uint) fromLayer) + ((uint) fromLayer)) >= 0)
     {
         goto Label_000D;
     }
     Label_0067:
     num4 = 0;
     goto Label_004B;
 }
        public List<double[]> Learn(double[][] data, double[][] ideal)
        {
            double[][] origData = (double[][])data.Clone();
            int n = data.Length;
            int m = data[0].Length;
            double[][] output = new double[n][];
            double[][] sgmNeighbours = new double[n][];
            for (var i = 0; i < n; i++)
            {
                double[] sgmN = new double[SegmentationData.SEGMENT_NEIGHBOURS];
                Array.Copy(data[i], m - SegmentationData.SEGMENT_NEIGHBOURS, sgmN, 0, SegmentationData.SEGMENT_NEIGHBOURS);
                sgmNeighbours[i] = sgmN;
                data[i] = data[i].Take(m - SegmentationData.SEGMENT_NEIGHBOURS).ToArray();
                output[i] = new double[m - SegmentationData.SEGMENT_NEIGHBOURS];
                data[i].CopyTo(output[i], 0);
            }

            IMLDataSet trainingSet = new BasicMLDataSet(data, output);

            int inputLayerSize = layersConfiguration[0] - SegmentationData.SEGMENT_NEIGHBOURS;
            int trainingLayerSize = layersConfiguration[1];
            BasicNetwork oneLayerAutoencoder = new BasicNetwork();
            oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
            oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
            oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
            oneLayerAutoencoder.Structure.FinalizeStructure();
            oneLayerAutoencoder.Reset();

            IMLTrain train = new ResilientPropagation(oneLayerAutoencoder, trainingSet);
            //IMLTrain train = new Backpropagation(oneLayerAutoencoder, trainingSet, LEARNING_RATE, MOMENTUM);

            int epoch = 1;
            List<double[]> errors = new List<double[]>();
            double[] trainError = new double[AUTOENCODER_MAX_ITER];

            do
            {
                train.Iteration();
                ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                trainError[epoch - 1] = train.Error;
                epoch++;
                //errors.Add(train.Error);
            } while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
            errors.Add(trainError);
            train.FinishTraining();

            BasicNetwork encoder = new BasicNetwork();
            encoder.AddLayer(new BasicLayer(null, BIAS, oneLayerAutoencoder.GetLayerNeuronCount(0)));
            encoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, oneLayerAutoencoder.GetLayerNeuronCount(1)));
            encoder.Structure.FinalizeStructure();
            encoder.Reset();

            //przypisanie wag do encodera
            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < encoder.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < encoder.GetLayerNeuronCount(i + 1); t++)
                        encoder.SetWeight(i, f, t, oneLayerAutoencoder.GetWeight(i, f, t));

            //Compare2Networks(oneLayerAutoencoder, encoder);

            for(int l=1; l<layersConfiguration.Count -2; l++)
            {
                inputLayerSize = layersConfiguration[l];
                trainingLayerSize = layersConfiguration[l+1];
                oneLayerAutoencoder = new BasicNetwork();
                oneLayerAutoencoder.AddLayer(new BasicLayer(null, BIAS, inputLayerSize));
                oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, trainingLayerSize));
                oneLayerAutoencoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, inputLayerSize));
                oneLayerAutoencoder.Structure.FinalizeStructure();
                oneLayerAutoencoder.Reset();

                //liczenie outputu z dotychczasowego encodera
                double[][] input = new double[n][];
                double[][] newOutput = new double[n][];
                for(int ni = 0; ni <n; ni++)
                {
                    IMLData res = encoder.Compute(new BasicMLData(data[ni]));
                    double[] resD = new double[res.Count];
                    for(int i=0; i<res.Count; i++)
                        resD[i] = res[i];
                    input[ni] = resD;
                    newOutput[ni] = new double[res.Count];
                    input[ni].CopyTo(newOutput[ni], 0);
                }

                BasicMLDataSet newTrainingSet = new BasicMLDataSet(input, newOutput);
                train = new ResilientPropagation(oneLayerAutoencoder, newTrainingSet);
                //train = new Backpropagation(oneLayerAutoencoder, newTrainingSet, LEARNING_RATE, MOMENTUM);

                epoch = 1;
                trainError = new double[AUTOENCODER_MAX_ITER];
                do
                {
                    train.Iteration();
                    ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                    Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                    trainError[epoch - 1] = train.Error;
                    epoch++;
                } while (train.Error > EPS && epoch < AUTOENCODER_MAX_ITER);
                errors.Add(trainError);
                train.FinishTraining();

                BasicNetwork extendedEncoder = new BasicNetwork();
                extendedEncoder.AddLayer(new BasicLayer(null, BIAS, encoder.GetLayerNeuronCount(0)));
                for (int el = 1; el < encoder.LayerCount; el++ )
                    extendedEncoder.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, encoder.GetLayerNeuronCount(el)));
                extendedEncoder.AddLayer(new BasicLayer(CurrentActivationFunction(), false, oneLayerAutoencoder.GetLayerNeuronCount(1)));
                extendedEncoder.Structure.FinalizeStructure();

                //przypisanie wag do extendedencodera
                for (int i = 0; i < extendedEncoder.LayerCount - 1; i++)
                {
                    if (i < encoder.LayerCount-1)
                    {
                        for (int f = 0; f < extendedEncoder.GetLayerNeuronCount(i); f++)
                            for (int t = 0; t < extendedEncoder.GetLayerNeuronCount(i + 1); t++)
                                extendedEncoder.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
                    }
                    else
                    {
                        for (int f = 0; f < extendedEncoder.GetLayerNeuronCount(i); f++)
                            for (int t = 0; t < extendedEncoder.GetLayerNeuronCount(i + 1); t++)
                                extendedEncoder.SetWeight(i, f, t, oneLayerAutoencoder.GetWeight(0, f, t));
                    }
                }
                encoder = extendedEncoder;

            }

            //tworzenie struktury ostatecznej sieci
            network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, BIAS, encoder.GetLayerNeuronCount(0) + SegmentationData.SEGMENT_NEIGHBOURS));
            for (int el = 1; el < encoder.LayerCount; el++)
                network.AddLayer(new BasicLayer(CurrentActivationFunction(), BIAS, encoder.GetLayerNeuronCount(el) + SegmentationData.SEGMENT_NEIGHBOURS));
            network.AddLayer(new BasicLayer(CurrentActivationFunction(), false, layersConfiguration[layersConfiguration.Count - 1]));
            network.Structure.FinalizeStructure();
            network.Reset();

            /*
            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < encoder.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < encoder.GetLayerNeuronCount(i + 1); t++)
                            network.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
            */
            //dla innych ustawic wagi 0, dla samych sobie 1

            for (int i = 0; i < encoder.LayerCount - 1; i++)
                for (int f = 0; f < network.GetLayerNeuronCount(i); f++)
                    for (int t = 0; t < network.GetLayerNeuronCount(i + 1); t++)
                    {
                        if (f < encoder.GetLayerNeuronCount(i) && t >= encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 0);
                        else if (f >= encoder.GetLayerNeuronCount(i) && t < encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 0);
                        else if (f >= encoder.GetLayerNeuronCount(i) && t >= encoder.GetLayerNeuronCount(i + 1))
                            network.SetWeight(i, f, t, 1);
                        else
                            network.SetWeight(i, f, t, encoder.GetWeight(i, f, t));
                    }

            //uczenie koncowej sieci
            trainingSet = new BasicMLDataSet(origData, ideal);

            train = new ResilientPropagation(network, trainingSet);
            //train = new Backpropagation(network, trainingSet, LEARNING_RATE, MOMENTUM);

            epoch = 1;
            trainError = new double[FINAL_NETWORK_MAX_ITER];
            do
            {
                train.Iteration();
                ActiveForm.Text = @"Epoch #" + epoch + @" Error:" + train.Error;
                Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
                trainError[epoch - 1] = train.Error;
                epoch++;

            } while (train.Error > EPS && epoch < FINAL_NETWORK_MAX_ITER);
            errors.Add(trainError);
            train.FinishTraining();

            try
            {
                string networkFileName = "autoencoder wo cmp 300 125 50 3";
                EncogDirectoryPersistence.SaveObject(new FileInfo(networkFileName), network);
                MessageBox.Show("NETWORK SAVED TO FILE " + networkFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return errors;
        }