Class RBM


  • public final class RBM
    extends java.lang.Object
    Class for training and stacking Restricted Boltzmann Machines (RBMs). Stacked RBMs are called DBN (deep belief net). Usually every layer of a deep belief net is training greedily with the contrastive divergence algorithm implemented in RBMCostFunction. Create new instances with the RBM.RBMBuilder or with the static factory methods.
    Author:
    thomas.jungblut
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  RBM.RBMBuilder  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static RBM deserialize​(java.io.DataInputStream in)
      Deserializes the RBM back from the binary stream input.
      WeightMatrix[] getNeuralNetworkWeights​(int outputLayerSize)
      Creates a weight matrix that can be used for unsupervised weight initialization in the MultilayerPerceptron.
      de.jungblut.math.DoubleMatrix[] getWeights()  
      de.jungblut.math.DoubleVector predict​(de.jungblut.math.DoubleVector input)
      Returns the hidden activations of the last RBM.
      de.jungblut.math.DoubleVector reconstructInput​(de.jungblut.math.DoubleVector hiddenActivations)
      Creates a reconstruction of the input using the given hidden activations.
      static void serialize​(RBM model, java.io.DataOutput out)
      Serializes this RBM model into the given output stream.
      void setSeed​(long seed)
      Sets the internally used rng seed.
      static RBM single​(int numHiddenNodes)  
      static RBM single​(int numHiddenNodes, ActivationFunction func)  
      static RBM singleGPU​(int numHiddenNodes, ActivationFunction func)  
      static RBM stacked​(int... numHiddenNodes)
      Creates a new stacked RBM with sigmoid activation and with the given number of hidden nodes in each stacked layer.
      static RBM stacked​(ActivationFunction func, int... numHiddenNodes)
      Creates a new stacked RBM with sigmoid activation and with the given number of hidden nodes in each stacked layer.
      static RBM stackedGPU​(ActivationFunction func, int... numHiddenNodes)
      Creates a new stacked RBM with sigmoid activation and with the given number of hidden nodes in each stacked layer.
      void train​(de.jungblut.math.DoubleVector[] trainingSet, double alpha, int numIterations)
      Trains the RBM on the given training set.
      void train​(de.jungblut.math.DoubleVector[] trainingSet, Minimizer minimizer, int numIterations)
      Trains the RBM on the given training set.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • train

        public void train​(de.jungblut.math.DoubleVector[] trainingSet,
                          double alpha,
                          int numIterations)
        Trains the RBM on the given training set.
        Parameters:
        trainingSet - the training set to train on.
        alpha - the learning rate for gradient descent.
        numIterations - how many iterations of training have to be done. (if converged before, it will stop training)
      • train

        public void train​(de.jungblut.math.DoubleVector[] trainingSet,
                          Minimizer minimizer,
                          int numIterations)
        Trains the RBM on the given training set.
        Parameters:
        trainingSet - the training set to train on.
        minimizer - the minimizer to use. Note that the costfunction's gradient isn't the real gradient and thus can't be optimized by line searching minimizers like Fmincg.
        numIterations - how many iterations of training have to be done. (if converged before, it will stop training)
      • predict

        public de.jungblut.math.DoubleVector predict​(de.jungblut.math.DoubleVector input)
        Returns the hidden activations of the last RBM.
        Parameters:
        input - the input of the first RBM.
        Returns:
        a vector that contains the values of the hidden activations on the last layer.
      • reconstructInput

        public de.jungblut.math.DoubleVector reconstructInput​(de.jungblut.math.DoubleVector hiddenActivations)
        Creates a reconstruction of the input using the given hidden activations. (That, what is returned by predict(DoubleVector)).
        Parameters:
        hiddenActivations - the activations of the predict method.
        Returns:
        the reconstructed input vector.
      • getWeights

        public de.jungblut.math.DoubleMatrix[] getWeights()
        Returns:
        the weight matrices.
      • getNeuralNetworkWeights

        public WeightMatrix[] getNeuralNetworkWeights​(int outputLayerSize)
        Creates a weight matrix that can be used for unsupervised weight initialization in the MultilayerPerceptron.
        Parameters:
        outputLayerSize - the size of the classification layer on top of this RBM.
        Returns:
        the WeightMatrix that maps layers to the weights.
      • setSeed

        public void setSeed​(long seed)
        Sets the internally used rng seed.
      • serialize

        public static void serialize​(RBM model,
                                     java.io.DataOutput out)
                              throws java.io.IOException
        Serializes this RBM model into the given output stream.
        Throws:
        java.io.IOException
      • deserialize

        public static RBM deserialize​(java.io.DataInputStream in)
                               throws java.io.IOException
        Deserializes the RBM back from the binary stream input.
        Throws:
        java.io.IOException
      • single

        public static RBM single​(int numHiddenNodes,
                                 ActivationFunction func)
        Returns:
        a single RBM which isn't stacked and emits to the given number of hidden nodes.
      • stacked

        public static RBM stacked​(ActivationFunction func,
                                  int... numHiddenNodes)
        Creates a new stacked RBM with sigmoid activation and with the given number of hidden nodes in each stacked layer. For example: 4,3,2 will create the first RBM with 4 hidden nodes, the second layer will operate on the 4 hidden node outputs of the RBM before and emit to 3 hidden nodes. Similarly the last layer will receive three inputs and emit 2 output's, which state you receive in the predict method.
      • single

        public static RBM single​(int numHiddenNodes)
        Returns:
        a single RBM with sigmoid activation which isn't stacked and emits to the given number of hidden nodes.
      • stacked

        public static RBM stacked​(int... numHiddenNodes)
        Creates a new stacked RBM with sigmoid activation and with the given number of hidden nodes in each stacked layer. For example: 4,3,2 will create the first RBM with 4 hidden nodes, the second layer will operate on the 4 hidden node outputs of the RBM before and emit to 3 hidden nodes. Similarly the last layer will receive three inputs and emit 2 output's, which state you receive in the predict method.
      • singleGPU

        public static RBM singleGPU​(int numHiddenNodes,
                                    ActivationFunction func)
        Returns:
        a single RBM which isn't stacked and emits to the given number of hidden nodes.
      • stackedGPU

        public static RBM stackedGPU​(ActivationFunction func,
                                     int... numHiddenNodes)
        Creates a new stacked RBM with sigmoid activation and with the given number of hidden nodes in each stacked layer. For example: 4,3,2 will create the first RBM with 4 hidden nodes, the second layer will operate on the 4 hidden node outputs of the RBM before and emit to 3 hidden nodes. Similarly the last layer will receive three inputs and emit 2 output's, which state you receive in the predict method.