예제 #1
0
  def testProbScalarBaseDistributionNonScalarTransformDynamic(self):
    # Scalar batch_shape.
    df = np.asarray(2., dtype=np.float32)
    # Non-scalar batch_shape.
    loc = np.asarray([[0., 0, 0],
                      [1, 2, 3],
                      [1, 0, 1]],
                     dtype=np.float32)
    scale_diag = np.asarray([[1., 2, 3],
                             [2, 3, 4],
                             [4, 5, 6]],
                            dtype=np.float32)
    scale_tril = np.concatenate([[np.diag(scale_diag[i])]
                                 for i in range(len(scale_diag))])
    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=np.tile(df, reps=len(scale_diag)),
        loc=loc,
        scale_tril=scale_tril)

    with self.cached_session():
      df_pl = array_ops.placeholder(dtypes.float32, name="df")
      loc_pl = array_ops.placeholder(dtypes.float32, name="loc")
      scale_diag_pl = array_ops.placeholder(dtypes.float32, name="scale_diag")
      feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag}
      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
예제 #2
0
  def testProbStatic(self):
    # Non-scalar batch_shape.
    df = np.asarray([1., 2, 3], dtype=np.float32)
    # Non-scalar batch_shape.
    loc = np.asarray([[0., 0, 0],
                      [1, 2, 3],
                      [1, 0, 1]],
                     dtype=np.float32)
    scale_diag = np.asarray([[1., 2, 3],
                             [2, 3, 4],
                             [4, 5, 6]],
                            dtype=np.float32)
    scale_tril = np.concatenate([[np.diag(scale_diag[i])]
                                 for i in range(len(scale_diag))])
    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=df, loc=loc, scale_tril=scale_tril)

    with self.cached_session():
      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(),
                          rtol=0., atol=1e-5)
예제 #3
0
  def testProbNonScalarBaseDistributionScalarTransformDynamic(self):
    # Non-scalar batch_shape.
    df = np.asarray([1., 2., 3.], dtype=np.float32)
    # Scalar batch_shape.
    loc = np.asarray([1, 2, 3], dtype=np.float32)
    scale_diag = np.asarray([2, 3, 4], dtype=np.float32)
    scale_tril = np.diag(scale_diag)

    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=df,
        loc=np.tile(loc[array_ops.newaxis, :], reps=[len(df), 1]),
        scale_tril=np.tile(scale_tril[array_ops.newaxis, :, :],
                           reps=[len(df), 1, 1]))

    with self.cached_session():
      df_pl = array_ops.placeholder(dtypes.float32, name="df")
      loc_pl = array_ops.placeholder(dtypes.float32, name="loc")
      scale_diag_pl = array_ops.placeholder(dtypes.float32, name="scale_diag")
      feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag}
      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
예제 #4
0
  def testProbNonScalarBaseDistributionScalarTransformDynamic(self):
    # Non-scalar batch_shape.
    df = np.asarray([1., 2., 3.], dtype=np.float32)
    # Scalar batch_shape.
    loc = np.asarray([1, 2, 3], dtype=np.float32)
    scale_diag = np.asarray([2, 3, 4], dtype=np.float32)
    scale_tril = np.diag(scale_diag)

    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=df,
        loc=np.tile(loc[None, :], [len(df), 1]),
        scale_tril=np.tile(scale_tril[None, :, :], [len(df), 1, 1]))

    with self.test_session():
      df_pl = array_ops.placeholder(dtypes.float32, name="df")
      loc_pl = array_ops.placeholder(dtypes.float32, name="loc")
      scale_diag_pl = array_ops.placeholder(dtypes.float32, name="scale_diag")
      feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag}
      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
예제 #5
0
  def testProbStatic(self):
    # Non-scalar batch_shape.
    df = np.asarray([1., 2, 3], dtype=np.float32)
    # Non-scalar batch_shape.
    loc = np.asarray([[0., 0, 0],
                      [1, 2, 3],
                      [1, 0, 1]],
                     dtype=np.float32)
    scale_diag = np.asarray([[1., 2, 3],
                             [2, 3, 4],
                             [4, 5, 6]],
                            dtype=np.float32)
    scale_tril = np.concatenate([[np.diag(scale_diag[i])]
                                 for i in range(len(scale_diag))])
    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=df, loc=loc, scale_tril=scale_tril)

    with self.test_session():
      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(),
                          rtol=0., atol=1e-5)
예제 #6
0
  def testProbScalarBaseDistributionNonScalarTransformDynamic(self):
    # Scalar batch_shape.
    df = np.asarray(2., dtype=np.float32)
    # Non-scalar batch_shape.
    loc = np.asarray([[0., 0, 0],
                      [1, 2, 3],
                      [1, 0, 1]],
                     dtype=np.float32)
    scale_diag = np.asarray([[1., 2, 3],
                             [2, 3, 4],
                             [4, 5, 6]],
                            dtype=np.float32)
    scale_tril = np.concatenate([[np.diag(scale_diag[i])]
                                 for i in range(len(scale_diag))])
    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=np.tile(df, len(scale_diag)),
        loc=loc,
        scale_tril=scale_tril)

    with self.test_session():
      df_pl = array_ops.placeholder(dtypes.float32, name="df")
      loc_pl = array_ops.placeholder(dtypes.float32, name="loc")
      scale_diag_pl = array_ops.placeholder(dtypes.float32, name="scale_diag")
      feed_dict = {df_pl: df, loc_pl: loc, scale_diag_pl: scale_diag}
      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
    def testProbStaticScalar(self):
        with self.test_session():
            # Scalar batch_shape.
            df = np.asarray(3., dtype=np.float32)
            # Scalar batch_shape.
            loc = np.asarray([1], dtype=np.float32)
            scale_diag = np.asarray([2.], dtype=np.float32)
            scale_tril = np.diag(scale_diag)

            expected_mst = _FakeVectorStudentT(df=df,
                                               loc=loc,
                                               scale_tril=scale_tril)

            actual_mst = _VectorStudentT(df=df,
                                         loc=loc,
                                         scale_diag=scale_diag,
                                         validate_args=True)
            x = 2. * self._rng.rand(4, 1).astype(np.float32) - 1.

            self.assertAllClose(expected_mst.log_prob(x),
                                actual_mst.log_prob(x).eval(),
                                rtol=0.,
                                atol=1e-5)
            self.assertAllClose(expected_mst.prob(x),
                                actual_mst.prob(x).eval(),
                                rtol=0.,
                                atol=1e-5)
    def testProbNonScalarBaseDistributionScalarTransform(self):
        # Non-scalar batch_shape.
        df = np.asarray([1., 2., 3.], dtype=np.float32)
        # Scalar batch_shape.
        loc = np.asarray([1, 2, 3], dtype=np.float32)
        scale_diag = np.asarray([2, 3, 4], dtype=np.float32)
        scale_tril = np.diag(scale_diag)
        x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

        expected_mst = _FakeVectorStudentT(
            df=df,
            loc=np.tile(loc[array_ops.newaxis, :], reps=[len(df), 1]),
            scale_tril=np.tile(scale_tril[array_ops.newaxis, :, :],
                               reps=[len(df), 1, 1]))

        with self.test_session():
            actual_mst = _VectorStudentT(df=df,
                                         loc=loc,
                                         scale_diag=scale_diag,
                                         validate_args=True)
            self.assertAllClose(expected_mst.log_prob(x),
                                actual_mst.log_prob(x).eval(),
                                rtol=0.,
                                atol=1e-5)
            self.assertAllClose(expected_mst.prob(x),
                                actual_mst.prob(x).eval(),
                                rtol=0.,
                                atol=1e-5)
  def testProbDynamic(self):
    # Non-scalar batch_shape.
    df = np.asarray([1., 2, 3], dtype=np.float32)
    # Non-scalar batch_shape.
    shift = np.asarray([[0., 0, 0],
                        [1, 2, 3],
                        [1, 0, 1]],
                       dtype=np.float32)
    scale_diag = np.asarray([[1., 2, 3],
                             [2, 3, 4],
                             [4, 5, 6]],
                            dtype=np.float32)
    scale_tril = np.concatenate([[np.diag(scale_diag[i])]
                                 for i in range(len(scale_diag))])
    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=df, shift=shift, scale_tril=scale_tril)

    with self.test_session():
      df_pl = array_ops.placeholder(dtypes.float32, name="df")
      shift_pl = array_ops.placeholder(dtypes.float32, name="shift")
      scale_diag_pl = array_ops.placeholder(dtypes.float32, name="scale_diag")
      feed_dict = {df_pl: df, shift_pl: shift, scale_diag_pl: scale_diag}
      actual_mst = _VectorStudentT(df=df, shift=shift, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(feed_dict=feed_dict),
                          rtol=0., atol=1e-5)
예제 #10
0
  def testProbStaticScalar(self):
    with self.cached_session():
      # Scalar batch_shape.
      df = np.asarray(3., dtype=np.float32)
      # Scalar batch_shape.
      loc = np.asarray([1], dtype=np.float32)
      scale_diag = np.asarray([2.], dtype=np.float32)
      scale_tril = np.diag(scale_diag)

      expected_mst = _FakeVectorStudentT(
          df=df, loc=loc, scale_tril=scale_tril)

      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      x = 2. * self._rng.rand(4, 1).astype(np.float32) - 1.

      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(),
                          rtol=0., atol=1e-5)
  def testProbNonScalarBaseDistributionScalarTransform(self):
    # Non-scalar batch_shape.
    df = np.asarray([1., 2., 3.], dtype=np.float32)
    # Scalar batch_shape.
    loc = np.asarray([1, 2, 3], dtype=np.float32)
    scale_diag = np.asarray([2, 3, 4], dtype=np.float32)
    scale_tril = np.diag(scale_diag)
    x = 2. * self._rng.rand(4, 3, 3).astype(np.float32) - 1.

    expected_mst = _FakeVectorStudentT(
        df=df,
        loc=np.tile(loc[None, :], [len(df), 1]),
        scale_tril=np.tile(scale_tril[None, :, :], [len(df), 1, 1]))

    with self.test_session():
      actual_mst = _VectorStudentT(df=df, loc=loc, scale_diag=scale_diag,
                                   validate_args=True)
      self.assertAllClose(expected_mst.log_prob(x),
                          actual_mst.log_prob(x).eval(),
                          rtol=0., atol=1e-5)
      self.assertAllClose(expected_mst.prob(x),
                          actual_mst.prob(x).eval(),
                          rtol=0., atol=1e-5)