def entropy(self): """Entropy of Dirichlet distribution. Returns: Entropy of distribution. """ concentration0 = self.concentration.sum(-1) k = self.concentration.shape[-1] return (paddle.lgamma(self.concentration).sum(-1) - paddle.lgamma(concentration0) - (k - concentration0) * paddle.digamma(concentration0) - ((self.concentration - 1.0) * paddle.digamma(self.concentration)).sum(-1))
def test_dtype_error(self): # in static mode with self.assertRaises(TypeError): with static.program_guard(static.Program()): x = static.data(name="x", shape=self._shape, dtype="int32") out = paddle.digamma(x, name="digamma_res") # in dynamic mode with self.assertRaises(RuntimeError): with fluid.dygraph.guard(): input = np.random.random(self._shape).astype("int32") input_t = paddle.to_tensor(input) res = paddle.digamma(input_t)
def test_in_dynamic_mode(self): for dtype in self.dtypes: input = np.random.random(self._shape).astype(dtype) sc_res = psi(input) for place in self.places: # it is more convenient to use `guard` than `enable/disable_**` here with fluid.dygraph.guard(place): input_t = paddle.to_tensor(input) res = paddle.digamma(input_t).numpy() self.assertEqual(np.allclose(res, sc_res, rtol=1e-05), True)
def test_in_static_mode(self): def init_input_output(dtype): input = np.random.random(self._shape).astype(dtype) return {'x': input}, psi(input) for dtype in self.dtypes: input_dict, sc_res = init_input_output(dtype) for place in self.places: with static.program_guard(static.Program()): x = static.data(name="x", shape=self._shape, dtype=dtype) out = paddle.digamma(x) exe = static.Executor(place) out_value = exe.run(feed=input_dict, fetch_list=[out.name]) self.assertEqual( np.allclose(out_value[0], sc_res, rtol=1e-5), True)
def test_name_argument(self): with static.program_guard(static.Program()): x = static.data(name="x", shape=self._shape, dtype=self.dtypes[0]) out = paddle.digamma(x, name="digamma_res") self.assertTrue("digamma_res" in out.name)