public void TrainNetwork(SomNetwork network, Vector input)
        {
            var bmu = BestMatchingUnit(network, input);

            UpdateNetwork(network, bmu, input);
            network.Time++;
        }
        public Bmu DrawInput(SomNetwork network, DataPoint data)
        {
            var bmu   = BestMatchingUnit(network, data.Input);
            var color = new Color();

            switch (data.Output.AbsoluteMaximumIndex())
            {
            case 0:
                color = Color.Blue;
                break;

            case 1:
                color = Color.BlueViolet;
                break;

            case 2:
                color = Color.Turquoise;
                break;

            case 3:
                color = Color.Green;
                break;

            case 4:
                color = Color.DeepSkyBlue;
                break;

            case 5:
                color = Color.Yellow;
                break;

            case 6:
                color = Color.Orange;
                break;

            case 7:
                color = Color.DeepPink;
                break;

            case 8:
                color = Color.Red;
                break;

            case 9:
                color = Color.SaddleBrown;
                break;

            default:
                color = Color.White;
                break;
            }
            bmu.Color = color;
            return(bmu);
        }
        public void UpdateNetwork(SomNetwork network, Bmu bmu, Vector input)
        {
            var radius = network.Radius * SomUtilities.Shrink(network.Time, network.T0);

            foreach (SomNode node in network.Weights)
            {
                if (Distance.Euclidean(bmu.Position, node.Position) < radius)
                {
                    Vector delta = (Vector)(
                        SomUtilities.Shrink(network.Time, network.T0) *
                        SomUtilities.Shrink(Distance.Euclidean(bmu.Position, node.Position), network.L0) *
                        network.LearningRate * (input - node.Weight));
                    node.Weight = (Vector)(node.Weight + delta);
                }
            }
        }
        public Bmu BestMatchingUnit(SomNetwork network, Vector input)
        {
            //Inelegant.
            var bmu = new Bmu()
            {
                Distance = int.MaxValue
            };

            foreach (SomNode node in network.Weights)
            {
                if (Distance.Euclidean(node.Weight, input) < bmu.Distance)
                {
                    bmu.Position = node.Position;
                    bmu.Distance = Distance.Euclidean(node.Weight, input);
                }
            }
            return(bmu);
        }
Exemple #5
0
        public Bitmap Illustrate(SomNetwork network)
        {
            //var lengths = (from somNode1 in network.Weights from somNode2 in network.Weights where network.Weights.IndexOf(somNode2) < network.Weights.IndexOf(somNode1) select Distance.Euclidean(somNode2.Weight, somNode1.Weight)).ToList();

            var lengths =
                network.Weights.Select(
                    parentNode => network.Neighbors(parentNode, 1.5).Average(childNode => Distance.Euclidean(childNode.Weight, parentNode.Weight))).ToList();

            //var lengths = network.Weights.Select(node => node.Weight.L2Norm()).ToList();
            var mean = lengths.Average();
            var quan = lengths.Quantile(0.02);

            var standardDeviation = lengths.StandardDeviation();
            var min = lengths.Min();

            var bitmap = new Bitmap(network.Width, network.Height);

            Graphics g = Graphics.FromImage(bitmap);

            foreach (var node in network.Weights)
            {
                int i = Convert.ToInt32(node.Position[0]);
                int j = Convert.ToInt32(node.Position[1]);

                var neighbors = network.Neighbors(node, 1.5);
                var dist      = 35 * (neighbors.Average(n => Distance.Euclidean(n.Weight, node.Weight)) - quan) / standardDeviation;

                var strength = Convert.ToInt32(dist);
                if (strength > 255)
                {
                    strength = 255;
                }
                if (strength < 0)
                {
                    strength = 0;
                }
                var color = Color.FromArgb(strength, Color.Black);
                bitmap.SetPixel(j, i, color);
            }

            return(bitmap);
        }