def test_softmax_normal(self): b = Softmax() d = np.random.random((5, 3)) pred = b.predict(d) predsums = np.sum(pred, axis=1) self.assertTrue(np.allclose(predsums, np.ones_like(predsums))) self.assertEqual(d.shape, pred.shape)
def test_softmax_normal_masked(self): b = Softmax() d = np.random.random((5, 3)) m = np.ones_like(d) m[:, 2] = 0 pred = b.predict(d, m) self.assertTrue(np.allclose(np.zeros_like(pred[:, 2]), pred[:, 2])) self.assertEqual(d.shape, pred.shape) predsums = np.sum(pred, axis=-1) self.assertTrue(np.allclose(predsums, np.ones_like(predsums)))
def test_masked_3D_gumbel_softmax(self): d = np.random.random((5, 4, 3)) m = np.ones_like(d) m[:, :, 2] = 0 sm = Softmax() gsm = GumbelSoftmax(temperature=0.3) smpred = sm.predict(d) gsmpred = gsm.predict(smpred, m) np.set_printoptions(precision=5, suppress=True) print gsmpred predsums = np.sum(gsmpred, axis=-1) self.assertTrue(np.allclose(predsums, np.ones_like(predsums))) self.assertEqual(d.shape, gsmpred.shape) self.assertTrue( np.allclose(np.zeros_like(gsmpred[:, :, 2]), gsmpred[:, :, 2]))
def test_gumbel_softmax(self): d = np.random.random((5, 3)) d[0, 0] = 2. sm = Softmax() gsm = GumbelSoftmax(temperature=1e-10, _alwaysrandom=True) smpred = sm.predict(d) samples = [] gsmpredf = gsm.predict for i in range(100000): gsmpred = gsmpredf(smpred) samples.append(gsmpred[0, :]) samples = np.concatenate([sample[:, np.newaxis] for sample in samples], axis=1) sampleavg = np.average(samples, axis=1) print sampleavg np.set_printoptions(precision=5, suppress=True) print smpred print gsmpred predsums = np.sum(gsmpred, axis=-1) self.assertTrue(np.allclose(predsums, np.ones_like(predsums))) self.assertEqual(d.shape, gsmpred.shape) self.assertTrue(np.allclose(smpred[0, :], sampleavg, rtol=1e-1))
def test_softmax_normal_with_temperature(self): b = Softmax(temperature=1e-6) d = np.random.random((5, 3)) pred = b.predict(d) print pred