class Test_GaussianBinaryVarianceRBM(unittest.TestCase): gbrbmData = numx.array([[0.57459607, 0.57689834], [-0.60788602, -0.57941004]]) gbrbmw = numx.array([[0.12179488, 2.95950177], [ 0.20318085, -28.62372894, ]]) gbrbmbv = numx.array([[-19.24399659, -13.26258696]]) gbrbmbh = numx.array([[-0.11155958, 57.02097584]]) gbrbm = Model.GaussianBinaryVarianceRBM(2, 2, gbrbmData, gbrbmw, gbrbmbv, gbrbmbh, 1.0, 0.0, 0.0) gbrbmTruelogZ = 59.6749019726 gbrbmTrueLL = -1.7328699078 gbrbmBestLLPossible = -1.732867951 epsilon = 0.00001 def test___init__(self): sys.stdout.write( 'GaussianBinaryVarianceRBM -> Performing init test ...') sys.stdout.flush() print(' successfully passed!') sys.stdout.flush() pass def test__calculate_sigma_gradient(self): sys.stdout.write( 'GaussianBinaryVarianceRBM -> Performing calculate_sigma_gradient test ...' ) sys.stdout.flush() deltaSigma = self.gbrbm._calculate_sigma_gradient( numx.array([[0.98, -0.56], [-0.3, 0.8]]), numx.array([[0, 1], [1, 1]])) target = numx.array([[763.9331994, 372.52636802]]) assert numx.all(numx.abs(target - deltaSigma) < self.epsilon) print(' successfully passed!') sys.stdout.flush() def test_get_parameters(self): sys.stdout.write( 'GaussianBinaryVarianceRBM -> Performing get_parameters test ...') sys.stdout.flush() assert len(self.gbrbm.get_parameters()) == 4 print(' successfully passed!') sys.stdout.flush() def test_calculate_gradients(self): sys.stdout.write( 'GaussianBinaryVarianceRBM -> Performing calculate_gradients test ...' ) sys.stdout.flush() assert len( self.gbrbm.calculate_gradients( numx.array([[0.98, -0.56], [-0.3, 0.8]]), numx.array([[0, 1], [1, 1]]))) == 4 print(' successfully passed!') sys.stdout.flush()
# Set restriction factor, learning rate, batch size and maximal number of epochs restrict = 0.01 * numx.max(numxext.get_norms(train_data, axis=1)) eps = 0.1 batch_size = 100 max_epochs = 200 # Create model, initial weights=Glorot init., initial sigma=1.0, initial bias=0, # no centering (Usually pass the data=training_data for a automatic init. that is # set the bias and sigma to the data mean and data std. respectively, for # whitened data centering is not an advantage) rbm = model.GaussianBinaryVarianceRBM(number_visibles=v1 * v2, number_hiddens=h1 * h2, initial_weights='AUTO', initial_visible_bias=0, initial_hidden_bias=0, initial_sigma=1.0, initial_visible_offsets=0.0, initial_hidden_offsets=0.0, dtype=numx.float64) # Set the hidden bias such that the scaling factor is 0.01 rbm.bh = -(numxext.get_norms(rbm.w + rbm.bv.T, axis=0) - numxext.get_norms(rbm.bv, axis=None)) / 2.0 + numx.log(0.01) rbm.bh = rbm.bh.reshape(1, h1 * h2) # Training with CD-1 k = 1 trainer_cd = trainer.CD(rbm) # Train model, status every 10th epoch