Restricted Boltzmann Machine (RBM)
| 28 | |
| 29 | # start-snippet-1 |
| 30 | class RBM(object): |
| 31 | """Restricted Boltzmann Machine (RBM) """ |
| 32 | def __init__( |
| 33 | self, |
| 34 | input=None, |
| 35 | n_visible=784, |
| 36 | n_hidden=500, |
| 37 | W=None, |
| 38 | hbias=None, |
| 39 | vbias=None, |
| 40 | numpy_rng=None, |
| 41 | theano_rng=None |
| 42 | ): |
| 43 | """ |
| 44 | RBM constructor. Defines the parameters of the model along with |
| 45 | basic operations for inferring hidden from visible (and vice-versa), |
| 46 | as well as for performing CD updates. |
| 47 | |
| 48 | :param input: None for standalone RBMs or symbolic variable if RBM is |
| 49 | part of a larger graph. |
| 50 | |
| 51 | :param n_visible: number of visible units |
| 52 | |
| 53 | :param n_hidden: number of hidden units |
| 54 | |
| 55 | :param W: None for standalone RBMs or symbolic variable pointing to a |
| 56 | shared weight matrix in case RBM is part of a DBN network; in a DBN, |
| 57 | the weights are shared between RBMs and layers of a MLP |
| 58 | |
| 59 | :param hbias: None for standalone RBMs or symbolic variable pointing |
| 60 | to a shared hidden units bias vector in case RBM is part of a |
| 61 | different network |
| 62 | |
| 63 | :param vbias: None for standalone RBMs or a symbolic variable |
| 64 | pointing to a shared visible units bias |
| 65 | """ |
| 66 | |
| 67 | self.n_visible = n_visible |
| 68 | self.n_hidden = n_hidden |
| 69 | |
| 70 | if numpy_rng is None: |
| 71 | # create a number generator |
| 72 | numpy_rng = numpy.random.RandomState(1234) |
| 73 | |
| 74 | if theano_rng is None: |
| 75 | theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) |
| 76 | |
| 77 | if W is None: |
| 78 | # W is initialized with `initial_W` which is uniformely |
| 79 | # sampled from -4*sqrt(6./(n_visible+n_hidden)) and |
| 80 | # 4*sqrt(6./(n_hidden+n_visible)) the output of uniform if |
| 81 | # converted using asarray to dtype theano.config.floatX so |
| 82 | # that the code is runable on GPU |
| 83 | initial_W = numpy.asarray( |
| 84 | numpy_rng.uniform( |
| 85 | low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)), |
| 86 | high=4 * numpy.sqrt(6. / (n_hidden + n_visible)), |
| 87 | size=(n_visible, n_hidden) |