I am learning about the Boltzmann machine. So far, I have successfully written a code that can learn the coefficients of the energy function of a Restricted Boltzmann Machine. Now, since my model is generative (if I have understood things correctly so far) and I know for sure that RBMs can be used for inpainting in binary images at least, I want to know how I can generate a sample from my probabilistic distribution given by the Boltzmann machine. That is how a new binary image based on the training dataset is generated by my model.

However, even though mathematically the idea seems clear, I do not know how I can program a computer to sample from a distribution.


For a RBM, you run the stochastic network - forward and back from the "input" and hidden layers - multiple times. After a few steps it will converge into sampling from the data population it has learned from. If you have learned from a whole picture and want to fill in a patch, then hold the input values that are not in the patch (don't allow them to change randomly).

Other generative models may have different approaches. GANs and VAEs typically have a simpler approach where you generate a random input vector and run the generator part of network forward from that input.

  • $\begingroup$ Thanks. I do not understand your first paragraph though. I mean, I understand the idea, but I have no clue how to implement it. Assuming that I have already found the coefficients of the energy function, what kind of algorithm should I implement to "run the stochastic network"? Can you elaborate please? $\endgroup$ – stressed out Jul 30 '18 at 7:23
  • $\begingroup$ @stressedout: Exactly the same process as training, generate the probabilities using the weights between hidden and input layers, and sample them by setting to 1 with given probability or 0 otherwise. In both directions, alternating between generating hidden state and the visible "input" (which is now technically the output). $\endgroup$ – Neil Slater Jul 30 '18 at 8:03
  • $\begingroup$ Then this is probably something easy and obvious but I'm overcomplicating it. I think I need to review the definitions and the algorithm once more. I'll check your answer later. Thanks. $\endgroup$ – stressed out Aug 1 '18 at 12:24
  • 1
    $\begingroup$ @stressedout Yes that makes sense. In this case you sample a 0 or a 1 for each neuron. The probability of a 1 is the value that you calculate based on sigmoid function of sum of weights plus bias. You can think of it just like a regular neural network, but instead of the output bring e.g. 0.7, instead you output 1 with probability 0.7 or 0 otherwise. Do same for all neurons at each step. $\endgroup$ – Neil Slater Aug 1 '18 at 17:01
  • 1
    $\begingroup$ To implement, you can generate a vector of random floating point numbers between 0 and 1 same size as the layer that you are calculating, and use an element-wise less than operation. You may need to recast the output back to floating point, depending on which library you are using. $\endgroup$ – Neil Slater Aug 1 '18 at 17:06

Your Answer

