def test_jacobian_identity(self, dim): """Tests that the jacobian is computed correctly compared to a general calculation using an identity matrix of feature vectors""" k = dim - 2 features = np.eye(dim) expf = embed.ExpFeatures(features) exp = embed.Exp(dim) assert np.allclose(expf.jacobian(ps[k]), exp.jacobian(ps[k]))
def test_grad_threshold(self, k): """Tests the calculation of the gradient against an explicit computation from hard-coded values of trainable parameters and mean photon numbers from data and model.""" m = k + 2 embedding = embed.Exp(m) vgbs = param.VGBS(A_eye[:m, :m], mean_photon_number_th, embedding, threshold=True) kl = cost.KL(test_data_th[k], vgbs) gamma = [-(n_means_data_th[i] - n_means_gbs_th[k][i]) / weights[i] for i in range(m)] assert np.allclose(kl.grad(params_fixed[:m]), gamma @ test_jacobian[:m, :m])
def test_mean_data_threshold(self, k): """Tests the mean photon number per mode from hard-coded values stored in the array ``n_means_data``. The test iterates over different numbers of modes in the data.""" m = k + 2 embedding = embed.Exp(m) vgbs = param.VGBS(A_eye[:m, :m], mean_photon_number_th, embedding, threshold=True) kl = cost.KL(test_data_th[k], vgbs) assert np.allclose(kl.mean_n_data, n_means_data_th[:m]) assert len(kl.mean_n_data) == m
def test_cost(self, k): """Tests the calculation of the Kullback-Liebler cost function against an explicit computation from hard-coded values of trainable parameters and mean photon numbers from data and model.""" m = k + 2 embedding = embed.Exp(m) vgbs = param.VGBS(A_eye[:m, :m], mean_photon_number, embedding, threshold=False) kl = cost.KL(test_data[k], vgbs) assert np.allclose(kl(params_fixed[:m]), test_sum_log_probs[k] / 6)
def test_jacobian_predefined(self, dim): """Tests that the jacobian is computed correctly for pre-defined features and parameters""" k = dim - 2 exp = embed.Exp(dim) g = exp.jacobian(ps[k]) assert np.allclose(g, jacobian[k])
def test_jacobian_zero_params(self, dim): """Tests that the jacobian is equal to negative identity when parameters are zero""" params = np.zeros(dim) exp = embed.Exp(dim) g = exp.jacobian(params) assert np.allclose(-1 * np.eye(dim), g)
def test_predefined(self, dim): """Tests that weights are computed correctly for pre-defined features and parameters""" k = dim - 2 exp = embed.Exp(dim) assert np.allclose(exp(ps[k]), weights[k])
def test_zero_params(self, dim): """Tests that weights are equal to one when parameters are zero""" exp = embed.Exp(dim) params = np.zeros(dim) assert np.allclose(exp(params), np.ones(dim))