def __call__(self, x, y): x, y = x.get(), y.get() if x is y: return B.fill_diag(B.one(x), B.shape(uprank(x))[0]) else: x, y = uprank(x), uprank(y) return Zero(B.dtype(x), B.shape(x)[0], B.shape(y)[0])
def __call__(self, x, y): w_x, w_y = x.w, y.w x, y = x.get(), y.get() if x is y: return Diagonal(1 / w_x) else: return Zero(B.dtype(x), num_elements(x), num_elements(y))
def __call__(self, x, y): w_x, w_y = x.w, y.w x, y = x.get(), y.get() if x is y: return Diagonal(1 / w_x) else: x, y = uprank(x), uprank(y) return Zero(B.dtype(x), B.shape(x)[0], B.shape(y)[0])
def _noise_as_matrix(noise: type(None), dtype: B.DType, n: int): """Efficiently represent noise as a matrix. Args: noise (None, scalar, vector, or matrix): Noise. `None` means no noise. dtype (dtype): Data type that the noise should be. n (int): Number of observations. Returns: matrix: Noise as a matrix. """ return Zero(dtype, n, n)
def test_normal_mean_is_zero(): # Check zero case. dist = Normal(B.eye(3)) assert dist.mean_is_zero approx(dist.mean, B.zeros(3, 1)) # Check another zero case. dist = Normal(Zero(np.float32, 3, 1), B.eye(3)) assert dist.mean_is_zero approx(dist.mean, B.zeros(3, 1)) # Check nonzero case. assert not Normal(B.randn(3, 1), B.eye(3)).mean_is_zero
def test_zero_attributes(): zero = Zero(int, 3, 4) assert zero.dtype == int assert zero.rows == 3 assert zero.cols == 4
def test_zero_formatting(): assert str(Zero(int, 3, 3)) == "<zero matrix: shape=3x3, dtype=int>" assert repr(Zero(int, 3, 3)) == "<zero matrix: shape=3x3, dtype=int>"
def generate(code): """Generate a random tensor of a particular type, specified with a code. Args: code (str): Code of the matrix. Returns: tensor: Random tensor. """ mat_code, shape_code = code.split(":") # Parse shape. if shape_code == "": shape = () else: shape = tuple(int(d) for d in shape_code.split(",")) if mat_code == "randn": return B.randn(*shape) elif mat_code == "randn_pd": mat = B.randn(*shape) # If it is a scalar or vector, just pointwise square it. if len(shape) in {0, 1}: return mat**2 + 1 else: return B.matmul(mat, mat, tr_b=True) + B.eye(shape[0]) elif mat_code == "zero": return Zero(B.default_dtype, *shape) elif mat_code == "const": return Constant(B.randn(), *shape) elif mat_code == "const_pd": return Constant(B.randn()**2 + 1, *shape) elif mat_code == "lt": mat = B.vec_to_tril(B.randn(int(0.5 * shape[0] * (shape[0] + 1)))) return LowerTriangular(mat) elif mat_code == "lt_pd": mat = generate(f"randn_pd:{shape[0]},{shape[0]}") return LowerTriangular(B.cholesky(B.reg(mat))) elif mat_code == "ut": mat = B.vec_to_tril(B.randn(int(0.5 * shape[0] * (shape[0] + 1)))) return UpperTriangular(B.transpose(mat)) elif mat_code == "ut_pd": mat = generate(f"randn_pd:{shape[0]},{shape[0]}") return UpperTriangular(B.transpose(B.cholesky(B.reg(mat)))) elif mat_code == "dense": return Dense(generate(f"randn:{shape_code}")) elif mat_code == "dense_pd": return Dense(generate(f"randn_pd:{shape_code}")) elif mat_code == "diag": return Diagonal(generate(f"randn:{shape_code}")) elif mat_code == "diag_pd": return Diagonal(generate(f"randn_pd:{shape_code}")) else: raise RuntimeError(f'Cannot parse generation code "{code}".')
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)
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])
def __call__(self, x, y): return Zero(B.dtype(x), B.shape(x)[0], B.shape(y)[0])
def elwise(self, x, y): if x is y and num_elements(x) == B.shape(self.noises)[0]: return uprank(self.noises) else: return Zero(B.dtype(x), num_elements(x), 1)
def __call__(self, x, y): if x is y and num_elements(x) == B.shape(self.noises)[0]: return Diagonal(self.noises) else: return Zero(B.dtype(x), num_elements(x), num_elements(y))
def __call__(self, x, y): y = y.get() return Zero(B.dtype(x), num_elements(x), num_elements(y))
def test_isabstract_zero(): check_isabstract(lambda: Zero(np.float64, 2, 2))
def __call__(self, x, y): x, y = uprank(x), uprank(y.get()) return Zero(B.dtype(x), B.shape(x)[0], B.shape(y)[0])
def __call__(self, x, y): x, y = x.get(), y.get() if x is y: return B.fill_diag(B.one(x), num_elements(x)) else: return Zero(B.dtype(x), num_elements(x), num_elements(y))