Exemplo n.º 1
0
        /// <summary>
        /// Get the hidden layer features from a visible layer
        /// </summary>
        /// <param name="dataArray"></param>
        /// <returns></returns>
        public double[][] GetHiddenLayer(double[][] dataArray)
        {
            var num_examples = dataArray.Length;

            // Create a matrix, where each row is to be the hidden units (plus a bias unit)
            // sampled from a training example.
            var hidden_states = RealMatrix.Ones(num_examples, m_numHiddenElements + 1);

            var data = new RealMatrix(dataArray);

            // Insert bias units of 1 into the first column of data.
            data = data.InsertCol(1); // np.insert(data, 0, 1, axis = 1)

            // Calculate the activations of the hidden units.
            var hiddenActivations = data * m_weights;
            // Calculate the probabilities of turning the hidden units on.
            var hiddenProbs = ActivationFunctions.Logistic(hiddenActivations);

            // Turn the hidden units on with their specified probabilities.
            hidden_states = hiddenProbs > Distributions.UniformRandromMatrix(num_examples, m_numHiddenElements + 1);

            // Ignore the bias units.
            hidden_states = hidden_states.RemoveFirstCol(); //  hidden_states[:,1:]
            return(hidden_states);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }