示例#1
0
        /// <summary>
        /// Q-R Decomposition
        /// </summary>
        /// <param name="A">Source matrix</param>
        /// <returns>{Q,R} in array form</returns>
        public static RealMatrix[] QR(RealMatrix A)
        {
            int m = A.Height;
            int n = A.Width;

            var Q = RealMatrix.I(m);
            var R = new RealMatrix(A);

            for (int k = 0; k < n; k++)
            {
                var x = new RVector(R.Submatrix(k, k, 0, 1));
                var e = RVector.Zeros(x.Length);
                e[0] = 1;
                var u = -Math.Sign(x[0]) * x.Norm2 * e + x;
                u = u / u.Norm2;
                var qn = RealMatrix.I(u.Length) - 2 * u.Outer(u);

                //var r = R.Submatrix(k, k) - 2 * u.Outer(u) * R.Submatrix(k, k);
                var q = RealMatrix.I(Q.Height);
                q.Update(k, k, qn);
                Q = Q * q;
                R = Q.Transpose * A;
            }
            return(new[] { Q, R });
        }
示例#2
0
文件: RBM.cs 项目: ThomasCX/DBN-KNN
        /// <summary>
        /// Day dream - Reconstruct a randrom matrix (An interesting way of seeing strong features the machine has learnt).
        ///
        /// Randomly initialize the visible units once, and start running alternating Gibbs sampling steps
        /// (where each step consists of updating all the hidden units, and then updating all of the visible units),
        /// taking a sample of the visible units at each step.
        ///
        /// Note that we only initialize the network "once", so these samples are correlated.
        /// ---------
        /// samples: A matrix, where each row is a sample of the visible units produced while the network was daydreaming。
        /// </summary>
        /// <param name="numberOfSamples">How many images/dreams</param>
        /// <returns>Array of Reconstructed dreams</returns>
        public double[][] DayDream(int numberOfSamples)
        {
            //Create a matrix, where each row is to be a sample of of the visible units
            //(with an extra bias unit), initialized to all ones.
            var data = RealMatrix.Ones(numberOfSamples, m_numVisibleElements + 1);

            //Take the first sample from a uniform distribution.
            data.Update(0, 1, Distributions.UniformRandromMatrixBool(1, m_numVisibleElements), 1);

            //Start the alternating Gibbs sampling.
            //Note that we keep the hidden units binary states, but leave the
            //visible units as real probabilities.
            //See section 3 of Hinton's "A Practical Guide to Training Restricted Boltzmann Machines" for more on why.
            for (int i = 0; i < numberOfSamples; i++)
            {
                var visible = data.Submatrix(i, 0, 1).ToVector();
                //Calculate the activations of the hidden units.
                var hidden_activations = (visible * m_weights).ToVector();
                //Calculate the probabilities of turning the hidden units on.
                var hidden_probs = ActivationFunctions.Logistic(hidden_activations);
                //Turn the hidden units on with their specified probabilities.
                var hidden_states = hidden_probs > RVector.Random(m_numHiddenElements + 1);
                //Always fix the bias unit to 1.
                hidden_states[0] = 1;

                //Recalculate the probabilities that the visible units are on.
                var visible_activations = (hidden_states * m_weights.Transpose).ToVector();
                var visible_probs       = ActivationFunctions.Logistic(visible_activations);
                var visible_states      = visible_probs > RVector.Random(m_numVisibleElements + 1);
                data.Update(visible_states, 0, false, i, 0);
            }

            return(data.Submatrix(0, 1).ToArray());
        }
示例#3
0
        public static RVector operator *(double scalar, RVector v)
        {
            var newVector = new RVector(v.Length);

            Parallel.For(0, v.Length, i =>
            {
                newVector[i] = v[i] * scalar;
            });
            return(newVector);
        }
示例#4
0
        public static double Dot(this RVector v1, RVector v2)
        {
            double r = 0;

            Parallel.For(0, v1.Length, i =>
            {
                r += v1[i] * v2[i];
            });
            return(r);
        }
示例#5
0
        public static RVector Ones(int size)
        {
            var v = new RVector(size);

            for (int i = 0; i < size; i++)
            {
                v[i] = 1;
            }
            return(v);
        }
示例#6
0
        public static RVector operator >(RVector c1, RVector c2)
        {
            var result = new RVector(c1.Length);

            Parallel.For(0, c1.Length, i =>
            {
                result[i] = Convert.ToDouble(c1[i] > c2[i]);
            });
            return(result);
        }
示例#7
0
        public static RVector operator -(RVector u, RVector v)
        {
            var x = new RVector(u.Length);

            Parallel.For(0, u.Length, i =>
            {
                x[i] = u[i] - v[i];
            });
            return(x);
        }
示例#8
0
        /// <summary>
        /// Uniform Random Vector
        /// </summary>
        /// <param name="numElements"></param>
        /// <returns></returns>
        public static RVector UniformRandromVector(int numElements)
        {
            var vector = new RVector(numElements);

            Parallel.For(0, numElements, i =>
            {
                vector[i] = GetRandomDouble();
            });
            return(vector);
        }
示例#9
0
        public static RVector Logistic(RVector vector)
        {
            var result = new RVector(vector.Length);

            for (int i = 0; i < vector.Length; i++)
            {
                result[i] = Logistic(vector[i]);
            }
            return(result);
        }
示例#10
0
        public static RealMatrix Outer(this RVector u, RVector v)
        {
            var A = new RealMatrix(u.Length, v.Length);

            Parallel.For(0, u.Length, i =>
                         Parallel.For(0, v.Length, j =>
            {
                A[i, j] = u[i] * v[j];
            }));
            return(A);
        }
示例#11
0
 /// <summary>
 /// Update a selection with a source vector
 /// </summary>
 /// <param name="matrix"></param>
 /// <param name="src"></param>
 /// <param name="length"></param>
 /// <param name="isVertical"></param>
 /// <param name="mPos"></param>
 /// <param name="nPos"></param>
 public static void Update(this RealMatrix matrix, RVector src, int length = 0, bool isVertical = true,
                           int mPos = 0, int nPos = 0)
 {
     length = length == 0 ? src.Length : length;
     for (int i = 0; i < length; i++)
     {
         if (isVertical)
         {
             matrix[mPos + i, nPos] = src[i];
         }
         else
         {
             matrix[mPos, nPos + i] = src[i];
         }
     }
 }