Example #1
0
def test_ones():
    """Tests ones"""

    # Scalar
    ones = ops.ones([1])
    assert isinstance(ones, tf.Tensor)
    assert ones.ndim == 1
    assert ones.shape[0] == 1
    assert ones.numpy() == 1.0

    # 1D
    ones = ops.ones([5])
    assert isinstance(ones, tf.Tensor)
    assert ones.ndim == 1
    assert ones.shape[0] == 5
    assert all(ones.numpy() == 1.0)

    # 2D
    ones = ops.ones([5, 4])
    assert isinstance(ones, tf.Tensor)
    assert ones.ndim == 2
    assert ones.shape[0] == 5
    assert ones.shape[1] == 4
    assert np.all(ones.numpy() == 1.0)

    # 3D
    ones = ops.ones([5, 4, 3])
    assert isinstance(ones, tf.Tensor)
    assert ones.ndim == 3
    assert ones.shape[0] == 5
    assert ones.shape[1] == 4
    assert ones.shape[2] == 3
    assert np.all(ones.numpy() == 1.0)
Example #2
0
    def __init__(
        self,
        k: int = 2,
        shape: Union[int, List[int]] = [],
        posterior=Categorical,
        prior=None,
        transform=None,
        initializer={"probs": xavier},
        var_transform={"probs": O.additive_logistic_transform},
        name="CategoricalParameter",
    ):

        # Check type of k
        if not isinstance(k, int):
            raise TypeError("k must be an integer")
        if k < 2:
            raise ValueError("k must be >1")

        # Make shape a list
        if isinstance(shape, int):
            shape = [shape]

        # Use a uniform prior
        if prior is None:
            prior = Categorical(O.ones(shape) / float(k))

        # Create shape of underlying variable array
        shape = shape + [k - 1]

        # Initialize the parameter
        super().__init__(
            shape=shape,
            posterior=posterior,
            prior=prior,
            transform=transform,
            initializer=initializer,
            var_transform=var_transform,
            name=name,
        )

        # shape should correspond to the sample shape
        self.shape = shape
Example #3
0
    def __init__(
        self,
        k: int = 2,
        shape: Union[int, List[int]] = [],
        posterior=Dirichlet,
        prior=None,
        transform=None,
        initializer={"concentration": pos_xavier},
        var_transform={"concentration": O.softplus},
        name="DirichletParameter",
    ):

        # Check type of k
        if not isinstance(k, int):
            raise TypeError("k must be an integer")
        if k < 2:
            raise ValueError("k must be >1")

        # Make shape a list
        if isinstance(shape, int):
            shape = [shape]

        # Create shape of underlying variable array
        shape = shape + [k]

        # Use a uniform prior
        if prior is None:
            prior = Dirichlet(O.ones(shape))

        # Initialize the parameter
        super().__init__(
            shape=shape,
            posterior=posterior,
            prior=prior,
            transform=transform,
            initializer=initializer,
            var_transform=var_transform,
            name=name,
        )
Example #4
0
 def init(shape):
     return val * O.ones(shape)