Example #1
0
        public FPA NeighborhoodRatio(FPA t)          //Sigma(t)
        {
            FPA exponent = -t / m_Theta;
            FPA Sigmat   = m_SigmaInitial * PA.Pow2(PA.Log2(LogBase) * exponent);

            return(Sigmat);
        }
Example #2
0
        private void run()
        {
            FPA t1 = PA.Add(PA.InnerProduct(dinput, diwt), theta);             // summation and theta

            FPA ohidden = PA.Reciprocal(PA.Add(PA.Pow2(PA.Negate(t1)), 1.0f)); //applying sigmoid function

            FPA t2 = PA.Add(PA.InnerProduct(ohidden, dowt), tau);              // summation and tau

            FPA ooutput = PA.Reciprocal(PA.Add(PA.Pow2(PA.Negate(t2)), 1.0f));

            FPA oerror = PA.Subtract(doutput, ooutput);                                                                     //numpat no

            FPA herror = PA.Transpose(PA.InnerProduct(dowt, PA.Transpose(oerror, new int[] { 1, 0 })), new int[] { 1, 0 }); // doubtful transpose

            herror = PA.Multiply(PA.Multiply(PA.Subtract(1.0f, t1), t1), herror);

            FPA _owt = PA.Add(dowt, PA.Multiply(PA.InnerProduct(PA.Transpose(t1, new int[] { 1, 0 }), oerror), betao)); // orig no transpose


            FPA _iwt = PA.Multiply(PA.InnerProduct(PA.Transpose(dinput, new int[] { 1, 0 }), herror), betah); //original dinput herror and no transpose

            dtau = PA.Add(PA.Multiply(betao, oerror), dtau);

            dtheta = PA.Add(PA.Multiply(betah, herror), dtheta); //orig oerror

            PA.ToArray(_owt, out owt);
            PA.ToArray(_iwt, out iwt);

            cleanup();
            diwt = new DFPA(iwt);
            dowt = new DFPA(owt);
        }
Example #3
0
        private void run()
        {
            FPA t1 = PA.Add(PA.InnerProduct(dinput, diwt), theta);

            FPA ohidden = PA.Reciprocal(PA.Add(PA.Pow2(PA.Negate(t1)), 1.0f));

            FPA t2 = PA.Add(PA.InnerProduct(ohidden, dowt), tau);

            FPA ooutput = PA.Reciprocal(PA.Add(PA.Pow2(PA.Negate(t2)), 1.0f));

            FPA oerror = PA.Subtract(doutput, ooutput);



            FPA herror = PA.InnerProduct(dowt, PA.Transpose(oerror, new int[] { 1, 0 }));

            herror = PA.InnerProduct(PA.Multiply(PA.Subtract(1.0f, t1), t1), herror);

            FPA _owt = PA.Add(dowt, PA.Multiply(PA.InnerProduct(t1, oerror), betao));

            FPA _iwt = PA.Multiply(PA.InnerProduct(herror, dinput), betah); //original dinput herror

            dtau = PA.Add(PA.Multiply(betao, oerror), dtau);

            dtheta = PA.Add(PA.Multiply(betah, herror), dtheta); //orig herror

            PA.ToArray(_owt, out owt);
            PA.ToArray(_iwt, out iwt);

            diwt = new DFPA(owt);
            dowt = new DFPA(iwt);
        }
Example #4
0
        public FPA LearningRate(FPA t)              //Epsilon(t)
        {
            FPA exponent = -t / m_Theta;
            FPA Epsilont = m_EpsilonInitial * PA.Pow2(PA.Log2(LogBase) * exponent);

            return(Epsilont);
        }
Example #5
0
        /*
         * Function which performs all the GPU operations
         */
        private void run()
        {
            /* Note : Inner product --- Matrix multiplication
             *        Multiply -- Element by element multiplication */

            FPA t1 = PA.Add(PA.InnerProduct(dinput, diwt), dtheta);

            /* ohidden is the output of hidden layer
             * Only Sigmoid function is used for timebeing */
            FPA ohidden = PA.Reciprocal(PA.Add(PA.Pow(new FPA(2.71828f, new int[] { numpat, nh }), PA.Negate(t1)), 1.0f));

            FPA t2 = PA.Add(PA.InnerProduct(ohidden, dowt), dtau);

            /* ooutput is the "actual" output of hidden layer
             * Only Sigmoid function is used for timebeing */
            FPA ooutput = PA.Reciprocal(PA.Add(PA.Pow(new FPA(2.71828f, new int[] { numpat, no }), PA.Negate(t2)), 1.0f));

            /* Error between expected and actual */
            FPA oerror = PA.Subtract(doutput, ooutput);

            /* Checking if error has fallen below 1% if so terminatinf further cycles */
            BoolParallelArray b = PA.All(PA.CompareGreater(derror, PA.Abs(oerror)), 1);

            b = PA.All(b);
            bool[] bt;
            PA.ToArray(b, out bt);
            if (bt[0] == true)
            {
                traincycles = 0;
            }

            /* herror is the error in the hidden layer */
            FPA herror = PA.Transpose(PA.InnerProduct(dowt, PA.Transpose(oerror, new int[] { 1, 0 })), new int[] { 1, 0 });

            herror = PA.Multiply(PA.Multiply(PA.Subtract(1.0f, ohidden), ohidden), herror);

            /* Weights between hidden  and output layer being updated */
            FPA _owt = PA.Add(PA.Multiply(PA.InnerProduct(PA.Transpose(ohidden, new int[] { 1, 0 }), oerror), betao), dowt);

            /* Weights between input  and hidden layer being updated */
            FPA _iwt = PA.Add(PA.Multiply(PA.InnerProduct(PA.Transpose(dinput, new int[] { 1, 0 }), herror), betah), diwt);

            /*Updating threshold for output layer */
            dtau = PA.Add(PA.Multiply(betao, oerror), dtau);

            /*Updating threshold for hidden layer */
            dtheta = PA.Add(PA.Multiply(betah, herror), dtheta);

            /* Casting the Parallel arrays to normal arrays */
            PA.ToArray(_owt, out owt);
            PA.ToArray(_iwt, out iwt);

            /* Rebuilding the disposable arrays from newly formed arrays */
            diwt = new DFPA(iwt);
            dowt = new DFPA(owt);
        }
Example #6
0
        }//Init

        public void FindBMU()
        {
            //Useful locals
            int alen = m_Height * m_Width;

            //Compute the distances from pattern to code vectors
            FPA a    = PA.AddDimension(m_CurrentPatternGPU, 1);
            FPA x    = PA.Stretch(a, 1, alen);
            FPA pol  = PA.Subtract(m_Weights, x);
            FPA pol2 = PA.Multiply(pol, pol);
            FPA pol3 = PA.Sum(pol2, 0);

            m_Distances = PA.Sqrt(pol3);

            //Find the minimal distance
            FPA dist2  = PA.AddDimension(m_Distances, 1);
            FPA minval = PA.MinVal(dist2, 0);

            FPA xxx = PA.Stretch(minval, alen);

            //Prepare trigger array
            BPA trigger = PA.CompareEqual(xxx, m_Distances);

            //BMU Coord ZEROS
            BPA trigger2 = PA.AddDimension(trigger, 0);
            BPA trigger3 = PA.Stretch(trigger2, 2, 1);

            //Extract BMU Coord
            FPA lol = PA.Cond(trigger3, m_Shape, zeros);

            m_BMUCoord = PA.Sum(lol, 1);
            //m_BMUCoord = PA.Evaluate(m_BMUCoord);

            //BMU Code Vector ZEROS
            BPA triggercv2 = PA.AddDimension(trigger, 0);
            BPA triggercv3 = PA.Stretch(triggercv2, m_PatternLength, 1);

            //Extract BMU code vector
            FPA mdr = PA.Cond(triggercv3, m_Weights, zeroscv);

            m_BMUCodeVector = PA.Sum(mdr, 1);
            //m_BMUCodeVector = PA.Evaluate(m_BMUCodeVector);


            //Begin computation ! ^^ AND OUTPUT
            PA.Evaluate(m_BMUCodeVector);
            PA.Evaluate(m_BMUCoord);
        }
Example #7
0
        public float[] Test(float[] iinput)
        {
            float[,] tinput = new float[1, ni];
            for (int i = 0; i < ni; i++)
            {
                tinput[0, i] = iinput[i];
            }

            dinput = new DFPA(tinput);
            diwt   = new DFPA(iwt);
            dowt   = new DFPA(owt);

            dtheta = PA.Section(dtheta, new Slice(0, 1), new Slice(0, nh));
            dtau   = PA.Section(dtau, new Slice(0, 1), new Slice(0, no));

            FPA t1      = PA.Add(PA.InnerProduct(dinput, diwt), dtheta);
            FPA ohidden = PA.Reciprocal(PA.Add(PA.Pow2(PA.Negate(t1)), 1.0f));
            FPA t2      = PA.Add(PA.InnerProduct(ohidden, dowt), dtau);

            FPA ooutput = PA.Reciprocal(PA.Add(PA.Pow2(PA.Negate(t2)), 1.0f));

            float[,] output;
            float[] routput = new float[no];
            PA.ToArray(ooutput, out output);

            for (int i = 0; i < no; i++)
            {
                routput[i] = output[0, i];
            }

            /*Disposable Floating arrays need to be explicitly "disposed" */
            dinput.Dispose();
            diwt.Dispose();
            dowt.Dispose();
            doutput.Dispose();

            /*Releasing all GPU Resources*/
            PA.UnInit();

            return(routput);
        }
Example #8
0
        public void DoEpoch(int nb)
        {
            int alen = m_Width * m_Height;

            for (int i = 0; i < nb; ++i)
            {
                //Neighborhood Function
                FPA sbmuc = PA.AddDimension(m_BMUCoord, 1);
                sbmuc = PA.Stretch(sbmuc, 1, alen);
                sbmuc = sbmuc - m_Shape;
                sbmuc = PA.Pow2(sbmuc);
                FPA sqdist = PA.Sum(sbmuc, 0);
                sqdist = PA.AddDimension(sqdist, 0);
                sqdist = PA.Stretch(sqdist, m_PatternLength, 1);
                //PA.Evaluate(sqdist);
                //

                //Learning Rate
                FPA lrate = new FPA((float)_CPU_LearningRate(m_time_val), m_PatternLength, alen);

                /*FPA sLearningRate = PA.AddDimension(LearningRate(m_Time), 1);
                 * sLearningRate = PA.Stretch(sLearningRate, m_PatternLength, alen);*/

                //Difference between units and current pattern
                FPA a   = PA.AddDimension(m_CurrentPatternGPU, 1);
                FPA x   = PA.Stretch(a, 1, alen);
                FPA pol = x - m_Weights;

                //Calcul des deltas
                FPA deltaW = lrate * pol;

                //Mise à jour des poids
                m_Weights = m_Weights + deltaW;

                //Incrémente le compteur de temps
                //m_Time = PA.Add(m_Time, 1.0f);
                m_time_val += 1;
            }
        }
Example #9
0
        public Network(int ini, int inh, int ino, int inumpat, int itraincycles)
        {
            ni          = ini;
            no          = ino;
            nh          = inh;
            numpat      = inumpat;
            traincycles = itraincycles;

            input  = new float[numpat, ni];
            output = new float[numpat, no];

            iwt = new float[ni, nh];
            owt = new float[nh, no];

            dtheta = new FPA(theta, new int[] { numpat, nh });
            dtau   = new FPA(tau, new int[] { numpat, no });

            rand  = new Random();
            theta = tau = 0.35f;
            betao = 0.2f;
            betah = 0.15f;
        }
Example #10
0
        public void DoEpoch(int nb)
        {
            int alen = m_Width * m_Height;
                for (int i = 0; i < nb; ++i)
                {
                    //Neighborhood Function
                    FPA sbmuc = PA.AddDimension(m_BMUCoord, 1);
                    sbmuc = PA.Stretch(sbmuc, 1, alen);
                    sbmuc = sbmuc - m_Shape;
                    sbmuc = PA.Pow2(sbmuc);
                    FPA sqdist = PA.Sum(sbmuc, 0);
                    sqdist = PA.AddDimension(sqdist, 0);
                    sqdist = PA.Stretch(sqdist, m_PatternLength, 1);
                    //PA.Evaluate(sqdist);
                    //

                    //Learning Rate
                    FPA lrate = new FPA((float)_CPU_LearningRate(m_time_val), m_PatternLength, alen);
                    /*FPA sLearningRate = PA.AddDimension(LearningRate(m_Time), 1);
                    sLearningRate = PA.Stretch(sLearningRate, m_PatternLength, alen);*/

                    //Difference between units and current pattern
                    FPA a = PA.AddDimension(m_CurrentPatternGPU, 1);
                    FPA x = PA.Stretch(a, 1, alen);
                    FPA pol = x - m_Weights;

                    //Calcul des deltas
                    FPA deltaW = lrate * pol;

                    //Mise à jour des poids
                    m_Weights = m_Weights + deltaW;

                    //Incrémente le compteur de temps
                    //m_Time = PA.Add(m_Time, 1.0f);
                    m_time_val += 1;
                }
        }
Example #11
0
 public void SetCurrentPattern(float[] Pattern)
 {
     m_CurrentPattern = Pattern;
     m_CurrentPatternGPU = new DisposableFloatParallelArray(Pattern);
 }
Example #12
0
        private void InitMap()
        {
            // MAP VALUES AND SHAPE
            Random RandomNumber = new Random();
            weight_vals = new float[m_PatternLength,m_Width * m_Height];
            for (int i = 0; i < m_Width * m_Height; ++i)
            {
                for(int k = 0; k < m_PatternLength; ++k )
                weight_vals[k, i] = (float)(RandomNumber.NextDouble()*255.0f);
            }

            shape_vals = new float[2,m_Width*m_Height];
            for( int i = 0; i < m_Height; i++ )
                for (int j = 0; j < m_Width; j++)
                {
                    shape_vals[0,m_Width*i + j] = j+1;
                    shape_vals[1,m_Width*i + j] = i+1;
                }

            m_Weights = new DisposableFloatParallelArray(weight_vals);
            m_Shape = new DFPA(shape_vals);
            //

            //BMU INIT
            m_bmucoord_vals = new float[2];
            m_bmucodevector_vals = new float[m_PatternLength];

            //ZEROS FOR FINDBMU
            zero_vals = new float[2, m_Width * m_Height];
            for (int i = 0; i < m_Width * m_Height; ++i)
            {
                zero_vals[0, i] = 0;
                zero_vals[1, i] = 0;
            }
            zerocv_vals = new float[m_PatternLength, m_Width * m_Height];
            for (int i = 0; i < m_Width * m_Height; ++i)
                for (int k = 0; k < m_PatternLength; ++k)
                {
                    zerocv_vals[k, i] = 0;
                    zerocv_vals[k, i] = 0;
                }

            zeroscv = new DFPA(zerocv_vals);
            zeros = new DFPA(zero_vals);
            //

            //INIT TIME
            //Don't use this
            m_Time = new DFPA(new float[] { 1 });

            //NEIGHBORHOOD VARIABLE INIT
            m_maxtime_val = 100.0f;
            m_theta_val = 100.0f / (float)Math.Log(25);
            m_sigmainitial_val = 25;
            m_epsiloninitial_val = 0.1f;
            m_Theta = new DFPA(new float[] { m_theta_val });
            m_SigmaInitial = new DFPA(new float[] { m_sigmainitial_val });
            m_EpsilonInitial = new DFPA(new float[] { m_epsiloninitial_val });
            m_MaxTime = new DFPA(new float[] { m_maxtime_val });

            //LOG BASE
            LogBase = new DFPA(new float[] { (float)(Math.E) });
        }
Example #13
0
 //Sigma(t)
 public FPA NeighborhoodRatio(FPA t)
 {
     FPA exponent = -t / m_Theta;
     FPA Sigmat = m_SigmaInitial * PA.Pow2(PA.Log2(LogBase) * exponent);
     return Sigmat;
 }
Example #14
0
 //Epsilon(t)
 public FPA LearningRate(FPA t)
 {
     FPA exponent = -t / m_Theta;
     FPA Epsilont = m_EpsilonInitial * PA.Pow2(PA.Log2(LogBase) * exponent);
     return Epsilont;
 }
Example #15
0
        public void FindBMU()
        {
            //Useful locals
            int alen = m_Height * m_Width;

                //Compute the distances from pattern to code vectors
                FPA a = PA.AddDimension(m_CurrentPatternGPU, 1);
                FPA x = PA.Stretch(a, 1, alen);
                FPA pol = PA.Subtract(m_Weights, x);
                FPA pol2 = PA.Multiply(pol, pol);
                FPA pol3 = PA.Sum(pol2, 0);
                m_Distances = PA.Sqrt(pol3);

                //Find the minimal distance
                FPA dist2 = PA.AddDimension(m_Distances, 1);
                FPA minval = PA.MinVal(dist2, 0);

                FPA xxx = PA.Stretch(minval, alen);

                //Prepare trigger array
                BPA trigger = PA.CompareEqual(xxx, m_Distances);

                //BMU Coord ZEROS
                BPA trigger2 = PA.AddDimension(trigger, 0);
                BPA trigger3 = PA.Stretch(trigger2, 2, 1);

                //Extract BMU Coord
                FPA lol = PA.Cond(trigger3, m_Shape, zeros);
                m_BMUCoord = PA.Sum(lol, 1);
                //m_BMUCoord = PA.Evaluate(m_BMUCoord);

                //BMU Code Vector ZEROS
                BPA triggercv2 = PA.AddDimension(trigger, 0);
                BPA triggercv3 = PA.Stretch(triggercv2, m_PatternLength, 1);

                //Extract BMU code vector
                FPA mdr = PA.Cond(triggercv3, m_Weights, zeroscv);
                m_BMUCodeVector = PA.Sum(mdr, 1);
                //m_BMUCodeVector = PA.Evaluate(m_BMUCodeVector);

            //Begin computation ! ^^ AND OUTPUT
            PA.Evaluate(m_BMUCodeVector);
            PA.Evaluate(m_BMUCoord);
        }
Example #16
0
        }//_CPU_FindBMU

        public void SetCurrentPattern(float[] Pattern)
        {
            m_CurrentPattern    = Pattern;
            m_CurrentPatternGPU = new DisposableFloatParallelArray(Pattern);
        }
Example #17
0
        private void InitMap()
        {
            // MAP VALUES AND SHAPE
            Random RandomNumber = new Random();

            weight_vals = new float[m_PatternLength, m_Width *m_Height];
            for (int i = 0; i < m_Width * m_Height; ++i)
            {
                for (int k = 0; k < m_PatternLength; ++k)
                {
                    weight_vals[k, i] = (float)(RandomNumber.NextDouble() * 255.0f);
                }
            }

            shape_vals = new float[2, m_Width *m_Height];
            for (int i = 0; i < m_Height; i++)
            {
                for (int j = 0; j < m_Width; j++)
                {
                    shape_vals[0, m_Width *i + j] = j + 1;
                    shape_vals[1, m_Width *i + j] = i + 1;
                }
            }

            m_Weights = new DisposableFloatParallelArray(weight_vals);
            m_Shape   = new DFPA(shape_vals);
            //

            //BMU INIT
            m_bmucoord_vals      = new float[2];
            m_bmucodevector_vals = new float[m_PatternLength];

            //ZEROS FOR FINDBMU
            zero_vals = new float[2, m_Width *m_Height];
            for (int i = 0; i < m_Width * m_Height; ++i)
            {
                zero_vals[0, i] = 0;
                zero_vals[1, i] = 0;
            }
            zerocv_vals = new float[m_PatternLength, m_Width *m_Height];
            for (int i = 0; i < m_Width * m_Height; ++i)
            {
                for (int k = 0; k < m_PatternLength; ++k)
                {
                    zerocv_vals[k, i] = 0;
                    zerocv_vals[k, i] = 0;
                }
            }

            zeroscv = new DFPA(zerocv_vals);
            zeros   = new DFPA(zero_vals);
            //

            //INIT TIME
            //Don't use this
            m_Time = new DFPA(new float[] { 1 });

            //NEIGHBORHOOD VARIABLE INIT
            m_maxtime_val        = 100.0f;
            m_theta_val          = 100.0f / (float)Math.Log(25);
            m_sigmainitial_val   = 25;
            m_epsiloninitial_val = 0.1f;
            m_Theta          = new DFPA(new float[] { m_theta_val });
            m_SigmaInitial   = new DFPA(new float[] { m_sigmainitial_val });
            m_EpsilonInitial = new DFPA(new float[] { m_epsiloninitial_val });
            m_MaxTime        = new DFPA(new float[] { m_maxtime_val });

            //LOG BASE
            LogBase = new DFPA(new float[] { (float)(Math.E) });
        }//Init