static bool cluster(k_Means trainingData, bool initialStep)
        {
            bool stop = true;

            if (initialStep)
            {
                //loop over training data
                for (int i = 0; i < trainingData.trainingSet.Count; i++)
                {
                    double[] centersED = new double[trainingData.clustersNumber];
                    //cluster
                    for (int j = 0; j < trainingData.clustersNumber; j++)
                    {
                        centersED[j] = ecludeanDistance(trainingData.trainingSet[i].FacialGestureFeatures,
                                                        trainingData.clusters[j].first);
                    }

                    //pick min ED
                    int minInd = minIndex(centersED);

                    //add current sample to cluster with minInd
                    trainingData.clusters[minInd].second.Add(trainingData.trainingSet[i]);

                    stop = false;
                }
            }
            else        //loop over samples at clusters
            {
                for (int i = 0; i < trainingData.clustersNumber; i++)
                {
                    //cluster
                    for (int j = 0; j < trainingData.clusters[i].second.Count; j++)
                    {
                        double[] centersED = new double[trainingData.clustersNumber];
                        //cluster
                        for (int k = 0; k < trainingData.clustersNumber; k++)
                        {
                            centersED[k] = ecludeanDistance(trainingData.clusters[i].second[j].FacialGestureFeatures,
                                                            trainingData.clusters[k].first);
                        }

                        //pick min ED
                        int minInd = minIndex(centersED);

                        if (minInd != i)
                        {
                            //not in same cluster
                            //add current sample to cluster with minInd and remove it from old cluster
                            trainingData.clusters[minInd].second.Add(trainingData.clusters[i].second[j]);
                            trainingData.clusters[i].second.RemoveAt(j);

                            stop = false;
                        }
                    }
                }
            }

            return(stop);
        }
        static double[] updateCenteroids(k_Means trainingData, int clusterIndex)
        {
            //each center is the mean of the cluster
            double[] newClusterCenter = Enumerable.Repeat((double)0, FacialGesture.facialGestureFeaturesPerSample).ToArray <double>();
            for (int i = 0; i < trainingData.clusters[clusterIndex].second.Count; i++)
            {
                for (int j = 0; j < FacialGesture.facialGestureFeaturesPerSample; j++)
                {
                    newClusterCenter[j] += trainingData.clusters[clusterIndex].second[i].FacialGestureFeatures[j];
                }
            }

            for (int j = 0; j < FacialGesture.facialGestureFeaturesPerSample; j++)
            {
                newClusterCenter[j] /= trainingData.clusters[clusterIndex].second.Count;
            }

            return(newClusterCenter);
        }
Exemple #3
0
        //////////////////////////////////////////////////////////

        public NeuralNetwork(int NetworkTotalLayersNumber, int[] LayerNueronsNumber, /*int inputSize, int outputSize,*/
                             int activationType, double learningRate, int learningAlgoritm, bool bias = false, int RBFLayerIndex = 0)
        {
            this.NetworkTotalLayersNumber = NetworkTotalLayersNumber;
            //this.NetworkHiddenLayersNumber = this.NetworkHiddenLayersNumber - 2 /*input and output*/;

            this.LayerNueronsNumber = LayerNueronsNumber;                       //include input layer

            this.inputSize  = LayerNueronsNumber[0 /*input*/];                  //number of neurons of input layer
            this.outputSize = LayerNueronsNumber[NetworkTotalLayersNumber - 1]; //number of neurons of output layer

            this.activationType = activationType;
            this.learningRate   = learningRate;

            //NeuralNetwork construction
            layers = new List <NetworkLayer>(this.NetworkTotalLayersNumber - 1 /*exclude input layer 34an mfeha4 processing*/);
            for (int i = 0; i < this.NetworkTotalLayersNumber - 2 /*for output layer*/; i++)
            {
                //add hidden layer
                //layer input size = size of previous layer
                //layer output size = neurons number
                layers.Add(new NetworkLayer(LayerNueronsNumber[i], LayerNueronsNumber[i + 1], i, activationType,
                                            learningRate, learningAlgoritm, bias));
            }

            //output Layer
            layers.Add(new NetworkLayer(LayerNueronsNumber[NetworkTotalLayersNumber - 2], LayerNueronsNumber[NetworkTotalLayersNumber - 1],
                                        NetworkTotalLayersNumber - 2 /*exclude input layer*/, activationType, learningRate, learningAlgoritm, bias, true /*output layer*/));

            networkErrorSignal = new double[outputSize];

            this.RBFLayerIndex    = RBFLayerIndex;
            this.learningAlgoritm = learningAlgoritm;

            this.bias = bias;

            this.KMean = new k_Means(LayerNueronsNumber[RBFLayerIndex + 1], FacialGestures.trainingFacialGestures);
        }
        public static List <MyPair <double[] /*centroid*/, double /*variance*/> > clusterProcess(k_Means trainingData)
        {
            //apply k-means clustering on trainingSet and return each cluster center and variance
            if (trainingData.clustersNumber <= trainingData.trainingSet.Count)
            {
                bool stop = false, initialeStep = true;
                while (!stop)
                {
                    //cluster process
                    stop = cluster(trainingData, initialeStep);

                    //loop over clusters
                    for (int i = 0; i < trainingData.clusters.Count; i++)
                    {
                        trainingData.clusters[i].first = updateCenteroids(trainingData, i);
                    }

                    initialeStep = false;
                }
            }

            //till now we have centroids, need to calculate variance
            for (int i = 0; i < trainingData.clustersNumber; i++)
            {
                //calculate variance of each cluster
                double variance = 0;
                for (int j = 0; j < trainingData.clusters[i].second.Count; j++)
                {
                    variance += ecludeanDistance(trainingData.clusters[i].second[j].FacialGestureFeatures, trainingData.clusters[i].first);
                }

                trainingData.clusteringResult.Add(new MyPair <double[], double>(trainingData.clusters[i].first, variance));
            }


            return(trainingData.clusteringResult);  //mlk4 clusters 3ndy :'D
        }