示例#1
0
 def testBijectorUnknownShape(self):
     with self.test_session():
         softmax = SoftmaxCentered()
         self.assertEqual("softmax_centered", softmax.name)
         x = array_ops.placeholder(shape=[2, None], dtype=dtypes.float32)
         real_x = np.log([[2., 3, 4], [4., 8, 12]])
         y = array_ops.placeholder(shape=[2, None], dtype=dtypes.float32)
         real_y = [[0.2, 0.3, 0.4, 0.1], [0.16, 0.32, 0.48, 0.04]]
         self.assertAllClose(real_y,
                             softmax.forward(x).eval(feed_dict={x: real_x}))
         self.assertAllClose(real_x,
                             softmax.inverse(y).eval(feed_dict={y: real_y}))
         self.assertAllClose(-np.sum(np.log(real_y), axis=1),
                             softmax.inverse_log_det_jacobian(
                                 y,
                                 event_ndims=1).eval(feed_dict={y: real_y}),
                             atol=0.,
                             rtol=1e-7)
         self.assertAllClose(-softmax.inverse_log_det_jacobian(
             y, event_ndims=1).eval(feed_dict={y: real_y}),
                             softmax.forward_log_det_jacobian(
                                 x,
                                 event_ndims=1).eval(feed_dict={x: real_x}),
                             atol=0.,
                             rtol=1e-7)
 def testBijectorUnknownShape(self):
   with self.test_session():
     softmax = SoftmaxCentered()
     self.assertEqual("softmax_centered", softmax.name)
     x = array_ops.placeholder(shape=[2, None], dtype=dtypes.float32)
     real_x = np.log([[2., 3, 4], [4., 8, 12]])
     y = array_ops.placeholder(shape=[2, None], dtype=dtypes.float32)
     real_y = [[0.2, 0.3, 0.4, 0.1], [0.16, 0.32, 0.48, 0.04]]
     self.assertAllClose(real_y, softmax.forward(x).eval(
         feed_dict={x: real_x}))
     self.assertAllClose(real_x, softmax.inverse(y).eval(
         feed_dict={y: real_y}))
     self.assertAllClose(
         -np.sum(np.log(real_y), axis=1),
         softmax.inverse_log_det_jacobian(y).eval(
             feed_dict={y: real_y}),
         atol=0.,
         rtol=1e-7)
     self.assertAllClose(
         -softmax.inverse_log_det_jacobian(y).eval(
             feed_dict={y: real_y}),
         softmax.forward_log_det_jacobian(x).eval(
             feed_dict={x: real_x}),
         atol=0.,
         rtol=1e-7)
示例#3
0
 def testBijectorVector(self):
     with self.test_session():
         softmax = SoftmaxCentered(event_ndims=1)
         self.assertEqual("softmax_centered", softmax.name)
         x = np.log([[2., 3, 4], [4., 8, 12]])
         y = [[0.2, 0.3, 0.4, 0.1], [0.16, 0.32, 0.48, 0.04]]
         self.assertAllClose(y, softmax.forward(x).eval())
         self.assertAllClose(x, softmax.inverse(y).eval())
         self.assertAllClose(-np.sum(np.log(y), axis=1),
                             softmax.inverse_log_det_jacobian(y).eval(),
                             atol=0.,
                             rtol=1e-7)
         self.assertAllClose(-softmax.inverse_log_det_jacobian(y).eval(),
                             softmax.forward_log_det_jacobian(x).eval(),
                             atol=0.,
                             rtol=1e-7)
示例#4
0
 def testBijectorScalar(self):
     with self.test_session():
         softmax = SoftmaxCentered()  # scalar by default
         self.assertEqual("softmax_centered", softmax.name)
         x = np.log([[2., 3, 4], [4., 8, 12]])
         y = [[[2. / 3, 1. / 3], [3. / 4, 1. / 4], [4. / 5, 1. / 5]],
              [[4. / 5, 1. / 5], [8. / 9, 1. / 9], [12. / 13, 1. / 13]]]
         self.assertAllClose(y, softmax.forward(x).eval())
         self.assertAllClose(x, softmax.inverse(y).eval())
         self.assertAllClose(-np.sum(np.log(y), axis=2),
                             softmax.inverse_log_det_jacobian(y).eval(),
                             atol=0.,
                             rtol=1e-7)
         self.assertAllClose(-softmax.inverse_log_det_jacobian(y).eval(),
                             softmax.forward_log_det_jacobian(x).eval(),
                             atol=0.,
                             rtol=1e-7)
 def testBijectorVector(self):
   with self.test_session():
     softmax = SoftmaxCentered(event_ndims=1)
     self.assertEqual("softmax_centered", softmax.name)
     x = np.log([[2., 3, 4], [4., 8, 12]])
     y = [[0.2, 0.3, 0.4, 0.1], [0.16, 0.32, 0.48, 0.04]]
     self.assertAllClose(y, softmax.forward(x).eval())
     self.assertAllClose(x, softmax.inverse(y).eval())
     self.assertAllClose(
         -np.sum(np.log(y), axis=1),
         softmax.inverse_log_det_jacobian(y).eval(),
         atol=0.,
         rtol=1e-7)
     self.assertAllClose(
         -softmax.inverse_log_det_jacobian(y).eval(),
         softmax.forward_log_det_jacobian(x).eval(),
         atol=0.,
         rtol=1e-7)
 def testBijectorScalar(self):
   with self.test_session():
     softmax = SoftmaxCentered()  # scalar by default
     self.assertEqual("softmax_centered", softmax.name)
     x = np.log([[2., 3, 4],
                 [4., 8, 12]])
     y = [[[2. / 3, 1. / 3],
           [3. / 4, 1. / 4],
           [4. / 5, 1. / 5]],
          [[4. / 5, 1. / 5],
           [8. / 9, 1. / 9],
           [12. / 13, 1. / 13]]]
     self.assertAllClose(y, softmax.forward(x).eval())
     self.assertAllClose(x, softmax.inverse(y).eval())
     self.assertAllClose(
         -np.sum(np.log(y), axis=2),
         softmax.inverse_log_det_jacobian(y).eval(),
         atol=0.,
         rtol=1e-7)
     self.assertAllClose(
         -softmax.inverse_log_det_jacobian(y).eval(),
         softmax.forward_log_det_jacobian(x).eval(),
         atol=0.,
         rtol=1e-7)