def testSparseProblem_alwaysZero(self): zero_prob = 1.0 problem = pg.SparseProblem(problem_spec.Spec(pg.Quadratic, (5, ), {}), zero_prob) self.assertEqual(zero_prob, problem.zero_prob) parameters = problem.init_tensors(seed=1234) objective = problem.objective(parameters) gradients = problem.gradients(objective, parameters) self.assertLen(gradients, 1) with self.test_session() as sess: self.assertFalse(any(sess.run(gradients[0])))
def testSparseProblem_someProbability(self): tf.set_random_seed(1234) zero_prob = 0.5 problem = pg.SparseProblem(problem_spec.Spec(pg.Quadratic, (5, ), {}), zero_prob) self.assertEqual(zero_prob, problem.zero_prob) parameters = problem.init_tensors(seed=1234) objective = problem.objective(parameters) gradients = problem.gradients(objective, parameters) self.assertLen(gradients, 1) with self.test_session() as sess: self.assertTrue(any(sess.run(gradients[0]))) self.assertFalse(all(sess.run(gradients[0])))