Exemple #1
0
        /// <summary>
        /// Insert a new row at a first row in a matrix
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static RealMatrix InsertRow(this RealMatrix matrix, int value)
        {
            var mat = new RealMatrix(matrix.Height + 1, matrix.Width);


            Parallel.For(0, matrix.Height, i =>
                         Parallel.For(0, matrix.Width, j =>
            {
                mat[i + 1, j] = matrix[i, j];
            }));


            Parallel.For(0, mat.Width, i =>
            {
                mat[0, i] = value;
            });

            return(mat);
        }
Exemple #2
0
        /// <summary>
        /// Update a certain axis in a matrix with a specific value
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="index">Offset point</param>
        /// <param name="value"></param>
        /// <param name="axis">0 -> left to right, 1 -> Up and down</param>
        /// <returns></returns>
        public static RealMatrix Update(this RealMatrix matrix, int index, int value, int axis)
        {
            var mat = new RealMatrix(matrix);

            if (axis == 0)
            {
                Parallel.For(0, matrix.Width, i =>
                {
                    mat[index, i] = value;
                });
            }
            else
            {
                Parallel.For(0, matrix.Height, i =>
                {
                    mat[i, index] = value;
                });
            }

            return(mat);
        }
Exemple #3
0
        /// <summary>
        /// Day dream - Reconstruct a randrom matrix (An interesting way of seeing strong features the machine has learnt)
        /// </summary>
        /// <param name="numberOfSamples">How many images/dreams</param>
        /// <returns>Array of Reconstructed dreams</returns>
        public double[][] DayDream(int numberOfSamples)
        {
            var data = RealMatrix.Ones(numberOfSamples, m_numVisibleElements + 1);

            data.Update(0, 1, Distributions.UniformRandromMatrixBool(1, m_numVisibleElements), 1);

            for (int i = 0; i < numberOfSamples; i++)
            {
                var visible            = data.Submatrix(i, 0, 1).ToVector();
                var hidden_activations = (visible * m_weights).ToVector();
                var hidden_probs       = ActivationFunctions.Logistic(hidden_activations);
                var hidden_states      = hidden_probs > RVector.Random(m_numHiddenElements + 1);
                hidden_states[0] = 1;

                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());
        }
Exemple #4
0
        public void Train(double[][] dataArray, int maxEpochs, out double error)
        {
            error = 0;

            var numExamples = dataArray.Length;
            var data        = new RealMatrix(dataArray);

            data = data.InsertCol(1);
            Stopwatch sw = new Stopwatch();

            for (int i = 0; i < maxEpochs; i++)
            {
                sw.Start();
                var posHiddenActivations = data * m_weights;
                var posHiddenProbs       = ActivationFunctions.Logistic(posHiddenActivations);
                var posHiddenStates      = posHiddenProbs > Distributions.UniformRandromMatrix(numExamples, m_numHiddenElements + 1);
                var posAssociations      = data.Transpose * posHiddenProbs;

                var negVisibleActivations = posHiddenStates * m_weights.Transpose;
                var negVisibleProbs       = ActivationFunctions.Logistic(negVisibleActivations);

                negVisibleProbs = negVisibleProbs.Update(0, 1, 1);
                var negHiddenActivations = negVisibleProbs * m_weights;
                var negHiddenProbs       = ActivationFunctions.Logistic(negHiddenActivations);

                var negAssociations = negVisibleProbs.Transpose * negHiddenProbs;

                m_weights = m_weights + (m_learningRate * ((posAssociations - negAssociations) / numExamples));

                sw.Stop();
                error = ((data - negVisibleProbs) ^ 2).Sum();
                RaiseEpochEnd(i, error);
                Console.WriteLine("Epoch {0}: error is {1}, computation time (ms): {2}", i, error, sw.ElapsedMilliseconds);
                sw.Reset();
            }

            RaiseTrainEnd(maxEpochs, error);
        }
Exemple #5
0
        /// <summary>
        /// Get the visible layer from a hidden layer
        /// </summary>
        /// <param name="dataArray"></param>
        /// <returns></returns>
        public double[][] GetVisibleLayer(double[][] dataArray)
        {
            var numExamples = dataArray.Length;

            // Create a matrix, where each row is to be the visible units (plus a bias unit)
            // sampled from a training example.

            var data = new RealMatrix(dataArray);

            // Insert bias units of 1 into the first column of data.
            data = data.InsertCol(1);

            // Calculate the activations of the visible units.
            var visibleActivations = data * m_weights.Transpose;
            // Calculate the probabilities of turning the visible units on.
            var visibleProbs = ActivationFunctions.Logistic(visibleActivations);
            // Turn the visible units on with their specified probabilities.
            var visibleStates = visibleProbs > Distributions.UniformRandromMatrix(numExamples, m_numVisibleElements + 1);

            // Ignore the bias units.
            visibleStates = visibleStates.RemoveFirstCol(); //visible_states[:,1:]
            return(visibleStates);
        }
Exemple #6
0
        public static RealMatrix Product(RealMatrix matrixA, RealMatrix matrixB)
        {
            int aRows = matrixA.Height;
            int aCols = matrixA.Width;
            int bRows = matrixB.Height;
            int bCols = matrixB.Width;

            if (aCols != bRows)
            {
                throw new Exception("xxxx");
            }

            var result = new RealMatrix(aRows, bCols);

            Parallel.For(0, aRows, i =>
            {
                for (int j = 0;
                     j < bCols;
                     ++j)
                {
                    // each col of B
                    for (int k = 0;
                         k < aCols;
                         ++k)
                    // could use k < bRows
                    {
                        result[i, j] +=
                            matrixA[i, k] *
                            matrixB[k, j];
                    }
                }
            }

                         );

            return(result);
        }
Exemple #7
0
 public RealMatrix(RealMatrix matrix) : base(matrix)
 {
 }
Exemple #8
0
 public RVector(RealMatrix mat)
     : base(mat)
 {
 }