Exemplo n.º 1
0
        /// <summary>
        /// Get the hidden layer features from a visible layer
        /// ---------
        /// Assuming the RBM has been trained (so that weights for the network have been learned),
        /// run the network on a set of hidden units, to get a sample of the visible units.
        ///
        /// Parameters
        /// ---------
        /// data: A matrix where each row consists of the states of the hidden units.
        ///
        /// Returns
        /// ---------
        /// visible_states: A matrix where each row consists of the visible units activated from the hidden
        /// units in the data matrix passed in.
        /// </summary>
        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();
            return(hidden_states);
        }
Exemplo n.º 2
0
        /// <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());
        }