def test_mean_variance_2d(): # create some random data num = 10000 dim2 = 10 num_steps = 10 stepsize = num // num_steps s = be.rand((num,dim2)) # reference result ref_mean = be.mean(s, axis=0) ref_var = be.var(s, axis=0) # do the online calculation mv = math_utils.MeanVarianceArrayCalculator() for i in range(num_steps): mv.update(s[i*stepsize:(i+1)*stepsize]) assert be.allclose(ref_mean, mv.mean) assert be.allclose(ref_var, mv.var, rtol=1e-3, atol=1e-5)
def test_mean_variance_serialization(): # create some random data num = 100 dim2 = 10 num_steps = 10 stepsize = num // num_steps s = be.rand((num, dim2)) # do the online calculation mv = math_utils.MeanVarianceArrayCalculator() for i in range(num_steps): mv.update(s[i * stepsize:(i + 1) * stepsize]) df = mv.to_dataframe() mv_serial = math_utils.MeanVarianceArrayCalculator.from_dataframe(df) assert be.allclose(mv_serial.mean, mv.mean) assert be.allclose(mv_serial.var, mv.var) assert be.allclose(mv_serial.square, mv.square) assert mv_serial.num == mv.num
def test_conditional_sampling(): """ Test sampling from one layer conditioned on the state of another layer. Note: This test compares values estimated by *sampling* to values computed analytically. It can fail for small batch_size, or strict tolerances, even if everything is working propery. """ num_visible_units = 20 num_hidden_units = 10 steps = 1000 mean_tol = 0.1 # set a seed for the random number generator be.set_seed() layer_types = [ layers.BernoulliLayer, layers.GaussianLayer] for layer_type in layer_types: # set up some layer and model objects vis_layer = layer_type(num_visible_units) hid_layer = layer_type(num_hidden_units) rbm = BoltzmannMachine([vis_layer, hid_layer]) # randomly set the intrinsic model parameters a = be.rand((num_visible_units,)) b = be.rand((num_hidden_units,)) W = 10 * be.rand((num_visible_units, num_hidden_units)) rbm.layers[0].params.loc[:] = a rbm.layers[1].params.loc[:] = b rbm.connections[0].weights.params.matrix[:] = W if layer_type == layers.GaussianLayer: log_var_a = be.randn((num_visible_units,)) log_var_b = be.randn((num_hidden_units,)) rbm.layers[0].params.log_var[:] = log_var_a rbm.layers[1].params.log_var[:] = log_var_b # initialize a state state = State.from_model(1, rbm) # set up a calculator for the moments moments = mu.MeanVarianceArrayCalculator() for _ in range(steps): moments.update(rbm.layers[0].conditional_sample( rbm._connected_rescaled_units(0, state), rbm._connected_weights(0))) model_mean = rbm.layers[0].conditional_mean( rbm._connected_rescaled_units(0, state), rbm._connected_weights(0)) ave = moments.mean close = be.allclose(ave, model_mean[0], rtol=mean_tol, atol=mean_tol) assert close, "{} conditional mean".format(layer_type) if layer_type == layers.GaussianLayer: model_mean, model_var = rbm.layers[0].conditional_params( rbm._connected_rescaled_units(0, state), rbm._connected_weights(0)) close = be.allclose(be.sqrt(moments.var), be.sqrt(model_var[0]), rtol=mean_tol, atol=mean_tol) assert close, "{} conditional standard deviation".format(layer_type)