variable.py 3.5 KB
Newer Older
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14 15 16
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle.distribution import constraint

17 18
import paddle

19

20
class Variable:
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    """Random variable of probability distribution.

    Args:
        is_discrete (bool): Is the variable discrete or continuous.
        event_rank (int): The rank of event dimensions.
    """

    def __init__(self, is_discrete=False, event_rank=0, constraint=None):
        self._is_discrete = is_discrete
        self._event_rank = event_rank
        self._constraint = constraint

    @property
    def is_discrete(self):
        return self._is_discrete

    @property
    def event_rank(self):
        return self._event_rank

    def constraint(self, value):
42
        """Check whether the 'value' meet the constraint conditions of this
43 44 45 46 47 48
        random variable."""
        return self._constraint(value)


class Real(Variable):
    def __init__(self, event_rank=0):
49
        super().__init__(False, event_rank, constraint.real)
50 51 52 53


class Positive(Variable):
    def __init__(self, event_rank=0):
54
        super().__init__(False, event_rank, constraint.positive)
55 56 57 58 59 60 61


class Independent(Variable):
    """Reinterprets some of the batch axes of variable as event axes.

    Args:
        base (Variable): Base variable.
62 63
        reinterpreted_batch_rank (int): The rightmost batch rank to be
            reinterpreted.
64 65 66 67 68
    """

    def __init__(self, base, reinterpreted_batch_rank):
        self._base = base
        self._reinterpreted_batch_rank = reinterpreted_batch_rank
69
        super().__init__(
70 71
            base.is_discrete, base.event_rank + reinterpreted_batch_rank
        )
72 73 74 75 76 77

    def constraint(self, value):
        ret = self._base.constraint(value)
        if ret.dim() < self._reinterpreted_batch_rank:
            raise ValueError(
                "Input dimensions must be equal or grater than  {}".format(
78 79 80 81 82 83
                    self._reinterpreted_batch_rank
                )
            )
        return ret.reshape(
            ret.shape[: ret.dim() - self.reinterpreted_batch_rank] + (-1,)
        ).all(-1)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105


class Stack(Variable):
    def __init__(self, vars, axis=0):
        self._vars = vars
        self._axis = axis

    @property
    def is_discrete(self):
        return any(var.is_discrete for var in self._vars)

    @property
    def event_rank(self):
        rank = max(var.event_rank for var in self._vars)
        if self._axis + rank < 0:
            rank += 1
        return rank

    def constraint(self, value):
        if not (-value.dim() <= self._axis < value.dim()):
            raise ValueError(
                f'Input dimensions {value.dim()} should be grater than stack '
106 107 108 109 110 111 112 113 114 115 116 117
                f'constraint axis {self._axis}.'
            )

        return paddle.stack(
            [
                var.check(value)
                for var, value in zip(
                    self._vars, paddle.unstack(value, self._axis)
                )
            ],
            self._axis,
        )
118 119 120 121


real = Real()
positive = Positive()