def testBatchMultivariateFullDynamic(self):
        with self.test_session() as sess:
            x = array_ops.placeholder(dtypes.float32, name="x")
            mu = array_ops.placeholder(dtypes.float32, name="mu")
            scale_diag = array_ops.placeholder(dtypes.float32,
                                               name="scale_diag")
            event_ndims = array_ops.placeholder(dtypes.int32,
                                                name="event_ndims")

            x_value = np.array([[[1., 1]]], dtype=np.float32)
            mu_value = np.array([[1., -1]], dtype=np.float32)
            scale_diag_value = np.array([[2., 2]], dtype=np.float32)
            event_ndims_value = 1

            feed_dict = {
                x: x_value,
                mu: mu_value,
                scale_diag: scale_diag_value,
                event_ndims: event_ndims_value
            }

            bijector = Affine(shift=mu,
                              scale_diag=scale_diag,
                              event_ndims=event_ndims)
            self.assertEqual(1, sess.run(bijector.event_ndims, feed_dict))
            self.assertAllClose([[[3., 1]]],
                                sess.run(bijector.forward(x), feed_dict))
            self.assertAllClose([[[0., 1]]],
                                sess.run(bijector.inverse(x), feed_dict))
            self.assertAllClose([-np.log(4)],
                                sess.run(bijector.inverse_log_det_jacobian(x),
                                         feed_dict))
Esempio n. 2
0
  def testBatchMultivariateFullDynamic(self):
    with self.test_session() as sess:
      x = array_ops.placeholder(dtypes.float32, name="x")
      mu = array_ops.placeholder(dtypes.float32, name="mu")
      scale_diag = array_ops.placeholder(dtypes.float32, name="scale_diag")
      event_ndims = array_ops.placeholder(dtypes.int32, name="event_ndims")

      x_value = np.array([[[1., 1]]], dtype=np.float32)
      mu_value = np.array([[1., -1]], dtype=np.float32)
      scale_diag_value = np.array([[2., 2]], dtype=np.float32)
      event_ndims_value = 1

      feed_dict = {
          x: x_value,
          mu: mu_value,
          scale_diag: scale_diag_value,
          event_ndims: event_ndims_value
      }

      bijector = Affine(
          shift=mu, scale_diag=scale_diag, event_ndims=event_ndims)
      self.assertEqual(1, sess.run(bijector.event_ndims, feed_dict))
      self.assertAllClose([[[3., 1]]], sess.run(bijector.forward(x), feed_dict))
      self.assertAllClose([[[0., 1]]], sess.run(bijector.inverse(x), feed_dict))
      self.assertAllClose([-np.log(4)],
                          sess.run(
                              bijector.inverse_log_det_jacobian(x), feed_dict))
Esempio n. 3
0
    def testNoBatchMultivariateFullDynamic(self):
        with self.cached_session() as sess:
            x = array_ops.placeholder(dtypes.float32, name="x")
            mu = array_ops.placeholder(dtypes.float32, name="mu")
            scale_diag = array_ops.placeholder(dtypes.float32,
                                               name="scale_diag")

            x_value = np.array([[1., 1]], dtype=np.float32)
            mu_value = np.array([1., -1], dtype=np.float32)
            scale_diag_value = np.array([2., 2], dtype=np.float32)
            feed_dict = {
                x: x_value,
                mu: mu_value,
                scale_diag: scale_diag_value,
            }

            bijector = Affine(shift=mu, scale_diag=scale_diag)
            self.assertAllClose([[3., 1]],
                                sess.run(bijector.forward(x), feed_dict))
            self.assertAllClose([[0., 1]],
                                sess.run(bijector.inverse(x), feed_dict))
            self.assertAllClose(
                -np.log(4),
                sess.run(bijector.inverse_log_det_jacobian(x, event_ndims=1),
                         feed_dict))
Esempio n. 4
0
    def _testLegalInputs(self, shift=None, scale_params=None, x=None):
        def _powerset(x):
            s = list(x)
            return itertools.chain.from_iterable(
                itertools.combinations(s, r) for r in range(len(s) + 1))

        for args in _powerset(scale_params.items()):
            with self.test_session():
                args = dict(args)

                scale_args = dict({"x": x}, **args)
                scale = self._makeScale(**scale_args)

                # We haven't specified enough information for the scale.
                if scale is None:
                    with self.assertRaisesRegexp(ValueError,
                                                 ("must be specified.")):
                        bijector = Affine(shift=shift, **args)
                else:
                    bijector = Affine(shift=shift, **args)
                    np_x = x
                    # For the case a vector is passed in, we need to make the shape
                    # match the matrix for matmul to work.
                    if x.ndim == scale.ndim - 1:
                        np_x = np.expand_dims(x, axis=-1)

                    forward = np.matmul(scale, np_x) + shift
                    if x.ndim == scale.ndim - 1:
                        forward = np.squeeze(forward, axis=-1)
                    self.assertAllClose(forward, bijector.forward(x).eval())

                    backward = np.linalg.solve(scale, np_x - shift)
                    if x.ndim == scale.ndim - 1:
                        backward = np.squeeze(backward, axis=-1)
                    self.assertAllClose(backward, bijector.inverse(x).eval())

                    scale *= np.ones(shape=x.shape[:-1], dtype=scale.dtype)
                    ildj = -np.log(np.abs(np.linalg.det(scale)))
                    # TODO (jvdillon): We need to make it so the scale_identity_multiplier id:1102
                    # https://github.com/imdone/tensorflow/issues/1103
                    # case does not deviate in expected shape. Fixing this will get rid of
                    # these special cases.
                    if (ildj.ndim > 0 and
                        (len(scale_args) == 1 or
                         (len(scale_args) == 2 and scale_args.get(
                             "scale_identity_multiplier", None) is not None))):
                        ildj = np.squeeze(ildj[0])
                    elif ildj.ndim < scale.ndim - 2:
                        ildj = np.reshape(ildj, scale.shape[0:-2])
                    self.assertAllClose(
                        ildj,
                        bijector.inverse_log_det_jacobian(
                            x, event_ndims=1).eval())
Esempio n. 5
0
  def _testLegalInputs(self, shift=None, scale_params=None, x=None):

    def _powerset(x):
      s = list(x)
      return itertools.chain.from_iterable(
          itertools.combinations(s, r) for r in range(len(s) + 1))

    for args in _powerset(scale_params.items()):
      with self.test_session():
        args = dict(args)

        scale_args = dict({"x": x}, **args)
        scale = self._makeScale(**scale_args)

        bijector_args = dict({"event_ndims": 1}, **args)

        # We haven't specified enough information for the scale.
        if scale is None:
          with self.assertRaisesRegexp(ValueError, ("must be specified.")):
            bijector = Affine(shift=shift, **bijector_args)
        else:
          bijector = Affine(shift=shift, **bijector_args)
          np_x = x
          # For the case a vector is passed in, we need to make the shape
          # match the matrix for matmul to work.
          if x.ndim == scale.ndim - 1:
            np_x = np.expand_dims(x, axis=-1)

          forward = np.matmul(scale, np_x) + shift
          if x.ndim == scale.ndim - 1:
            forward = np.squeeze(forward, axis=-1)
          self.assertAllClose(forward, bijector.forward(x).eval())

          backward = np.linalg.solve(scale, np_x - shift)
          if x.ndim == scale.ndim - 1:
            backward = np.squeeze(backward, axis=-1)
          self.assertAllClose(backward, bijector.inverse(x).eval())

          ildj = -np.log(np.abs(np.linalg.det(scale)))
          # TODO(jvdillon): We need to make it so the scale_identity_multiplier
          # case does not deviate in expected shape. Fixing this will get rid of
          # these special cases.
          if (ildj.ndim > 0 and (len(scale_args) == 1 or (
              len(scale_args) == 2 and
              scale_args.get("scale_identity_multiplier", None) is not None))):
            ildj = np.squeeze(ildj[0])
          elif ildj.ndim < scale.ndim - 2:
            ildj = np.reshape(ildj, scale.shape[0:-2])
          self.assertAllClose(ildj, bijector.inverse_log_det_jacobian(x).eval())
Esempio n. 6
0
  def testNoBatchMultivariateFullDynamic(self):
    with self.test_session() as sess:
      x = array_ops.placeholder(dtypes.float32, name="x")
      mu = array_ops.placeholder(dtypes.float32, name="mu")
      scale_diag = array_ops.placeholder(dtypes.float32, name="scale_diag")

      x_value = np.array([[1., 1]], dtype=np.float32)
      mu_value = np.array([1., -1], dtype=np.float32)
      scale_diag_value = np.array([2., 2], dtype=np.float32)
      feed_dict = {
          x: x_value,
          mu: mu_value,
          scale_diag: scale_diag_value,
      }

      bijector = Affine(shift=mu, scale_diag=scale_diag)
      self.assertAllClose([[3., 1]], sess.run(bijector.forward(x), feed_dict))
      self.assertAllClose([[0., 1]], sess.run(bijector.inverse(x), feed_dict))
      self.assertAllClose(
          -np.log(4),
          sess.run(bijector.inverse_log_det_jacobian(x), feed_dict))