def __init__(self, model, data=None):
        """ The constructor initializes the CD trainer with a given model and data.

        :param model: The model to sample from.
        :type model: Valid model class.

        :param data: Data for initialization, only has effect if the centered gradient is used.
        :type data: numpy array [num. samples x input dim]
        """
        # Store model variable
        self.model = model

        # Create the Gibbs-sampler
        self.sampler = sampler.GibbsSampler(model)

        # Count the number of parameters
        parameters = self.model.get_parameters()
        self.num_parameters = len(parameters)

        self.hidden_offsets = 0.5 * numx.ones((1, self.model.output_dim))
        if data is not None:
            if self.model.input_dim != data.shape[1]:
                raise ex.ValueError("Data dimension and model input dimension have to be equal!")
            self.visible_offsets = data.mean(axis=0).reshape(1, data.shape[1])
        else:
            self.visible_offsets = 0.5 * numx.ones((1, self.model.input_dim))
        # Storage variables for the gradients
        self.parameter_updates = []
        for i in range(self.num_parameters):
            self.parameter_updates.append(numx.zeros((
                parameters[i].shape[0],
                parameters[i].shape[1]),
                dtype=model.dtype))
 def test_Gibbs_sampler(self):
     sys.stdout.write('RBM Sampler -> Performing GibbsSampler test ... ')
     sys.stdout.flush()
     numx.random.seed(42)
     sampler = Sampler.GibbsSampler(self.bbrbm)
     probCD1, probCD2, probCS1, probCS2, probCS3, probCS4, sumProbs = self.execute_sampler(sampler, self.num_samples)
     assert numx.all(numx.abs(1.0 / 4.0 - probCD1) < self.epsilon)
     assert numx.all(numx.abs(1.0 / 4.0 - probCD2) < self.epsilon)
     assert numx.all(numx.abs(1.0 / 8.0 - probCS1) < self.epsilon)
     assert numx.all(numx.abs(1.0 / 8.0 - probCS2) < self.epsilon)
     assert numx.all(numx.abs(1.0 / 8.0 - probCS3) < self.epsilon)
     assert numx.all(numx.abs(1.0 / 8.0 - probCS4) < self.epsilon)
     assert numx.all(numx.abs(1.0 - sumProbs) < self.epsilon)
     print('successfully passed!')
     sys.stdout.flush()
Beispiel #3
0
    def __init__(self, model, batch_size):

        # Set batch size
        self.batch_size = batch_size

        # Store model
        self.model = model

        self.rbm = RBM_MODEL.BinaryBinaryRBM(
            number_visibles=model.input_dim + model.hidden2_dim,
            number_hiddens=model.hidden1_dim,
            data=None,
            initial_weights=numx.vstack((model.W1, model.W2.T)),
            initial_visible_bias=numx.hstack((model.b1, model.b3)),
            initial_hidden_bias=model.b2,
            initial_visible_offsets=numx.hstack((model.o1, model.o3)),
            initial_hidden_offsets=model.o2)

        self.sampler = RBM_SAMPLER.GibbsSampler(self.rbm)