How to use the lab.B.shape function in lab

To help you get started, we’ve selected a few lab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wesselb / stheno / tests / test_util.py View on Github external
def test_uprank():
    allclose(uprank(0), [[0]])
    allclose(uprank(np.array([0])), [[0]])
    allclose(uprank(np.array([[0]])), [[0]])
    assert type(uprank(Component('test')(0))) == Component('test')

    k = OneKernel()

    assert B.shape(k(0, 0)) == (1, 1)
    assert B.shape(k(0, np.ones(5))) == (1, 5)
    assert B.shape(k(0, np.ones((5, 2)))) == (1, 5)

    assert B.shape(k(np.ones(5), 0)) == (5, 1)
    assert B.shape(k(np.ones(5), np.ones(5))) == (5, 5)
    assert B.shape(k(np.ones(5), np.ones((5, 2)))) == (5, 5)

    assert B.shape(k(np.ones((5, 2)), 0)) == (5, 1)
    assert B.shape(k(np.ones((5, 2)), np.ones(5))) == (5, 5)
    assert B.shape(k(np.ones((5, 2)), np.ones((5, 2)))) == (5, 5)

    with pytest.raises(ValueError):
        k(0, np.ones((5, 2, 1)))
    with pytest.raises(ValueError):
        k(np.ones((5, 2, 1)))

    m = OneMean()

    assert B.shape(m(0)) == (1, 1)
    assert B.shape(m(np.ones(5))) == (5, 1)
    assert B.shape(m(np.ones((5, 2)))) == (5, 1)
github wesselb / stheno / stheno / matrix.py View on Github external
def transpose(a): return Diagonal(B.diag(a), *reversed(B.shape(a)))
github wesselb / stheno / stheno / kernel.py View on Github external
def _dky(x, y):
        import tensorflow as tf

        with tf.GradientTape() as t:
            # Get the numbers of inputs.
            nx = B.shape(x)[0]
            ny = B.shape(y)[0]

            # Copy the input `nx` times to efficiently compute many derivatives.
            yis = tf.identity_n([y[:, i:i + 1]] * nx)
            t.watch(yis)

            # Tile inputs for batched computation.
            x = B.reshape(B.tile(x, 1, ny), nx * ny, -1)
            y = B.tile(y, nx, 1)

            # Insert tracked dimension, which is different for every tile.
            yi = B.concat(*yis, axis=0)
            y = B.concat(y[:, :i], yi, y[:, i + 1:], axis=1)

            # Perform the derivative computation.
            out = B.dense(k_elwise(x, y))
            grads = t.gradient(out, yis, unconnected_gradients='zero')
github wesselb / stheno / stheno / random.py View on Github external
def logpdf(self, x):
        """Compute the log-pdf.

        Args:
            x (input): Values to compute the log-pdf of.
            
        Returns:
            list[tensor]: Log-pdf for every input in `x`. If it can be
                determined that the list contains only a single log-pdf,
                then the list is flattened to a scalar.
        """
        logpdfs = -(B.logdet(self.var) +
                    B.cast(self.dtype, self.dim) *
                    B.cast(self.dtype, B.log_2_pi) +
                    B.iqf_diag(self.var, uprank(x) - self.mean)) / 2
        return logpdfs[0] if B.shape(logpdfs) == (1,) else logpdfs
github wesselb / stheno / stheno / kernel.py View on Github external
def elwise(self, x, y):
        if x is y and B.shape(B.uprank(x))[0] == B.shape(self.noises)[0]:
            return B.uprank(self.noises)
        else:
            x = B.uprank(x)
            return Zero(B.dtype(x), B.shape(x)[0], 1)
github wesselb / stheno / stheno / matrix.py View on Github external
def eye(a): return B.eye(B.dtype(a), *B.shape(a))
github wesselb / stheno / stheno / graph.py View on Github external
Returns:
            tuple: Tuple of samples.
        """
        sample = GP(MOK(*self.ps),
                    MOM(*self.ps),
                    graph=Graph())(MultiInput(*xs)).sample(n)

        # To unpack `x`, just keep `.get()`ing.
        def unpack(x):
            while isinstance(x, Input):
                x = x.get()
            return x

        # Unpack sample.
        lengths = [B.shape(uprank(unpack(x)))[0] for x in xs]
        i, samples = 0, []
        for length in lengths:
            samples.append(sample[i:i + length, :])
            i += length
        return samples[0] if len(samples) == 1 else samples
github wesselb / stheno / stheno / kernel.py View on Github external
def __call__(self, x, y):
        if x is y and B.shape(uprank(x))[0] == B.shape(self.noises)[0]:
            return Diagonal(self.noises)
        else:
            x, y = uprank(x), uprank(y)
            return Zero(B.dtype(x), B.shape(x)[0], B.shape(y)[0])
github wesselb / stheno / stheno / matrix.py View on Github external
@B.shape.extend_multi((Diagonal,), (Constant,))
def shape(a): return a.rows, a.cols