def test_shape_basic(): s = shape(np.array(1)) assert np.array_equal(eval_outputs([s]), []) s = shape(np.ones((5, 3))) assert np.array_equal(eval_outputs([s]), [5, 3]) s = shape(np.ones(2)) assert np.array_equal(eval_outputs([s]), [2]) s = shape(np.ones((5, 3, 10))) assert np.array_equal(eval_outputs([s]), [5, 3, 10])
def test_shape_basic(): s = shape([]) assert s.type.broadcastable == (True, ) s = shape([10]) assert s.type.broadcastable == (True, ) s = shape(lscalar()) assert s.type.broadcastable == (False, ) class MyType(Type): def filter(self, *args, **kwargs): raise NotImplementedError() def __eq__(self, other): return isinstance(other, MyType) and other.thingy == self.thingy s = shape(Variable(MyType())) assert s.type.broadcastable == (False, ) s = shape(np.array(1)) assert np.array_equal(eval_outputs([s]), []) s = shape(np.ones((5, 3))) assert np.array_equal(eval_outputs([s]), [5, 3]) s = shape(np.ones(2)) assert np.array_equal(eval_outputs([s]), [2]) s = shape(np.ones((5, 3, 10))) assert np.array_equal(eval_outputs([s]), [5, 3, 10])
def grad(self, inputs, cost_grad): """ In defining the gradient, the Finite Fourier Transform is viewed as a complex-differentiable function of a complex variable """ a = inputs[0] n = inputs[1] axis = inputs[2] grad = cost_grad[0] if not isinstance(axis, TensorConstant): raise NotImplementedError( f"{self.__class__.__name__}: gradient is currently implemented" " only for axis being an Aesara constant") axis = int(axis.data) # notice that the number of actual elements in wrto is independent of # possible padding or truncation: elem = arange(0, shape(a)[axis], 1) # accounts for padding: freq = arange(0, n, 1) outer_res = outer(freq, elem) pow_outer = exp(((-2 * math.pi * 1j) * outer_res) / (1.0 * n)) res = tensordot(grad, pow_outer, (axis, 0)) # This would be simpler but not implemented by aesara: # res = switch(lt(n, shape(a)[axis]), # set_subtensor(res[...,n::], 0, False, False), res) # Instead we resort to that to account for truncation: flip_shape = list(np.arange(0, a.ndim)[::-1]) res = res.dimshuffle(flip_shape) res = switch( lt(n, shape(a)[axis]), set_subtensor( res[n::, ], 0, False, False, ), res, ) res = res.dimshuffle(flip_shape) # insures that gradient shape conforms to input shape: out_shape = (list(np.arange(0, axis)) + [a.ndim - 1] + list(np.arange(axis, a.ndim - 1))) res = res.dimshuffle(*out_shape) return [res, None, None]
def L_op(self, inputs, outputs, out_grads): x, k = inputs k_grad = grad_undefined(self, 1, k, "topk: k is not differentiable") if not (self.return_indices or self.return_values): x_grad = grad_undefined( self, 0, x, "topk: cannot get gradient" " without both indices and values", ) else: x_shp = shape(x) z_grad = out_grads[0] ndim = x.ndim axis = self.axis % ndim grad_indices = [ arange(x_shp[i]).dimshuffle([0] + ["x"] * (ndim - i - 1)) if i != axis else outputs[-1] for i in range(ndim) ] x_grad = x.zeros_like(dtype=z_grad.dtype) x_grad = set_subtensor(x_grad[tuple(grad_indices)], z_grad) return [x_grad, k_grad]
def test_nonstandard_shapes(): a = tensor3(config.floatX) a.tag.test_value = np.random.random((2, 3, 4)).astype(config.floatX) b = tensor3(config.floatX) b.tag.test_value = np.random.random((2, 3, 4)).astype(config.floatX) tl = make_list([a, b]) tl_shape = shape(tl) assert np.array_equal(tl_shape.get_test_value(), (2, 2, 3, 4)) # There's no `FunctionGraph`, so it should return a `Subtensor` tl_shape_i = shape_i(tl, 0) assert isinstance(tl_shape_i.owner.op, Subtensor) assert tl_shape_i.get_test_value() == 2 tl_fg = FunctionGraph([a, b], [tl], features=[ShapeFeature()]) tl_shape_i = shape_i(tl, 0, fgraph=tl_fg) assert not isinstance(tl_shape_i.owner.op, Subtensor) assert tl_shape_i.get_test_value() == 2 none_shape = shape(NoneConst) assert np.array_equal(none_shape.get_test_value(), [])
def predict_mean_i(i, x_star, s_star, X, beta, h): n, D = shape(X) # rescale every dimension by the corresponding inverse lengthscale iL = at.diag(h[i, :D]) inp = (X - x_star).dot(iL) # compute the mean B = iL.dot(s_star).dot(iL) t = inp.dot(B) lb = (inp * t).sum() + beta.sum() Mi = at_sum(lb) * h[i, D] return Mi