Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)))
Ejemplo n.º 3
0
    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]))
Ejemplo n.º 4
0
 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))
Ejemplo n.º 5
0
 def apply(self, x):
     a = self.lin1(x)
     b = Tanh()(a)
     c = self.lin2(b)
     d = Softmax()(c)
     return d
Ejemplo n.º 6
0
 def apply(self, *args, **kwargs):
     vec = self.inner(*args, **kwargs)
     ret = self.outl(vec)
     return Softmax()(ret)
Ejemplo n.º 7
0
 def apply(self, x):
     ret = self.l(x)
     ret = Softmax()(ret)
     return ret
Ejemplo n.º 8
0
 def test_softmax_normal_with_temperature(self):
     b = Softmax(temperature=1e-6)
     d = np.random.random((5, 3))
     pred = b.predict(d)
     print pred