def test_dtype_failure(self): arr = TensorType(FLOATX, [False] * 3)("arr") indices = TensorType(FLOATX, [False] * 3)("indices") arr.tag.test_value = np.zeros((1,) * arr.ndim, dtype=FLOATX) indices.tag.test_value = np.zeros((1,) * indices.ndim, dtype=FLOATX) with pytest.raises(IndexError): take_along_axis(arr, indices)
def test_ctors(self): if PYTHON_INT_BITWIDTH == 32: assert shared(7).type == iscalar, shared(7).type else: assert shared(7).type == lscalar, shared(7).type assert shared(7.0).type == dscalar assert shared(np.float32(7)).type == fscalar # test tensor constructor b = shared(np.zeros((5, 5), dtype="int32")) assert b.type == TensorType("int32", broadcastable=[False, False]) b = shared(np.random.rand(4, 5)) assert b.type == TensorType("float64", broadcastable=[False, False]) b = shared(np.random.rand(5, 1, 2)) assert b.type == TensorType("float64", broadcastable=[False, False, False]) assert shared([]).type == generic def badfunc(): shared(7, bad_kw=False) with pytest.raises(TypeError): badfunc()
def test_convert_variable(): test_type = TensorType(config.floatX, [False, False]) test_var = test_type() test_type2 = TensorType(config.floatX, [True, False]) test_var2 = test_type2() res = test_type.convert_variable(test_var) assert res is test_var res = test_type.convert_variable(test_var2) assert res is test_var2 res = test_type2.convert_variable(test_var) assert res.type == test_type2 test_type3 = TensorType(config.floatX, [True, False, True]) test_var3 = test_type3() res = test_type2.convert_variable(test_var3) assert res is None const_var = at.as_tensor([[1, 2], [3, 4]], dtype=config.floatX) res = test_type.convert_variable(const_var) assert res is const_var
def test_filter_variable(): test_type = TensorType(config.floatX, []) with pytest.raises(TypeError): test_type.filter(test_type()) test_type = TensorType(config.floatX, [True, False]) with pytest.raises(TypeError): test_type.filter(np.empty((0, 1), dtype=config.floatX)) with pytest.raises(TypeError, match=".*not aligned.*"): test_val = np.empty((1, 2), dtype=config.floatX) test_val.flags.aligned = False test_type.filter(test_val) with pytest.raises(ValueError, match="Non-finite"): test_type.filter_checks_isfinite = True test_type.filter(np.full((1, 2), np.inf, dtype=config.floatX)) test_type2 = TensorType(config.floatX, [False, False]) test_var = test_type() test_var2 = test_type2() res = test_type.filter_variable(test_var, allow_convert=True) assert res is test_var # Make sure it returns the more specific type res = test_type.filter_variable(test_var2, allow_convert=True) assert res.type == test_type
def test_fill_grad(self): # Fix bug reported at # https://groups.google.com/d/topic/theano-users/nQshB8gUA6k/discussion x = TensorType(config.floatX, (False, True, False))("x") y = TensorType(config.floatX, (False, True, False))("y") e = second(x, y) aesara.grad(e.sum(), y)
def test_fixed_shape_convert_variable(): # These are equivalent types t1 = TensorType("float64", (True, True)) t2 = TensorType("float64", (1, 1)) assert t1 == t2 assert t1.shape == t2.shape t2_var = t2() res = t2.convert_variable(t2_var) assert res is t2_var res = t1.convert_variable(t2_var) assert res is t2_var t1_var = t1() res = t2.convert_variable(t1_var) assert res is t1_var t3 = TensorType("float64", (False, True)) t3_var = t3() res = t2.convert_variable(t3_var) assert isinstance(res.owner.op, Rebroadcast) t3 = TensorType("float64", (False, False)) t4 = TensorType("float64", (3, 2)) t4_var = t4() assert t3.shape == (None, None) res = t3.convert_variable(t4_var) assert res.type == t4 assert res.type.shape == (3, 2)
def make_node(self, x): x = aet.as_tensor_variable(x) self_axis = self.axis if self_axis is None: broadcastable = [False] else: if self_axis < 0: self_axis += len(x.broadcastable) if self_axis < 0 or self_axis >= len(x.broadcastable): raise RuntimeError( "Unique axis `{}` is outside of input ndim = " "{}.".format(self.axis, len(x.broadcastable))) broadcastable = [ b if axis != self_axis else False for axis, b in enumerate(x.broadcastable) ] outputs = [TensorType(broadcastable=broadcastable, dtype=x.dtype)()] typ = TensorType(broadcastable=[False], dtype="int64") if self.return_index: outputs.append(typ()) if self.return_inverse: outputs.append(typ()) if self.return_counts: outputs.append(typ()) return Apply(self, [x], outputs)
def test_fixed_shape_basic(): t1 = TensorType("float64", (1, 1)) assert t1.shape == (1, 1) assert t1.broadcastable == (True, True) t1 = TensorType("float64", (0,)) assert t1.shape == (0,) assert t1.broadcastable == (False,) t1 = TensorType("float64", (False, False)) assert t1.shape == (None, None) assert t1.broadcastable == (False, False) t1 = TensorType("float64", (2, 3)) assert t1.shape == (2, 3) assert t1.broadcastable == (False, False) assert t1.value_zeros(t1.shape).shape == t1.shape assert str(t1) == "TensorType(float64, (2, 3))" t1 = TensorType("float64", (1,)) assert t1.shape == (1,) assert t1.broadcastable == (True,) t2 = t1.clone() assert t1 is not t2 assert t1 == t2 t2 = t1.clone(dtype="float32", shape=(2, 4)) assert t2.dtype == "float32" assert t2.shape == (2, 4)
def test__getitem__AdvancedSubtensor_bool(): x = matrix("x") i = TensorType("bool", (False, False))("i") z = x[i] op_types = [ type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z]) ] assert op_types[-1] == AdvancedSubtensor i = TensorType("bool", (False, ))("i") z = x[:, i] op_types = [ type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z]) ] assert op_types[-1] == AdvancedSubtensor i = TensorType("bool", (False, ))("i") z = x[..., i] op_types = [ type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z]) ] assert op_types[-1] == AdvancedSubtensor with pytest.raises(TypeError): z = x[[True, False], i] z = x[ivector("b"), i] op_types = [ type(node.op) for node in aesara.graph.basic.io_toposort([x, i], [z]) ] assert op_types[-1] == AdvancedSubtensor
def test_infer_shape(self): for s_left, s_right in [ ((5, 6), (5, 6)), ((5, 6), (5, 1)), ((5, 6), (1, 6)), ((5, 1), (5, 6)), ((1, 6), (5, 6)), ((2, 3, 4, 5), (2, 3, 4, 5)), ((2, 3, 4, 5), (2, 3, 1, 5)), ((2, 3, 4, 5), (1, 3, 4, 5)), ((2, 1, 4, 5), (2, 3, 4, 5)), ((2, 3, 4, 1), (2, 3, 4, 5)), ]: dtype = aesara.config.floatX t_left = TensorType(dtype, [(entry == 1) for entry in s_left])() t_right = TensorType(dtype, [(entry == 1) for entry in s_right])() t_left_val = np.zeros(s_left, dtype=dtype) t_right_val = np.zeros(s_right, dtype=dtype) self._compile_and_check( [t_left, t_right], [Elemwise(aes.add)(t_left, t_right)], [t_left_val, t_right_val], Elemwise, )
def _input_tensors(self, shape): ndim = len(shape) arr = TensorType(FLOATX, [False] * ndim)("arr") indices = TensorType(INTX, [False] * ndim)("indices") arr.tag.test_value = np.zeros(shape, dtype=FLOATX) indices.tag.test_value = np.zeros(shape, dtype=INTX) return arr, indices
def test_extend_inplace(self): mySymbolicMatricesList1 = TypedListType( TensorType(aesara.config.floatX, (False, False)))() mySymbolicMatricesList2 = TypedListType( TensorType(aesara.config.floatX, (False, False)))() z = Extend()(mySymbolicMatricesList1, mySymbolicMatricesList2) m = aesara.compile.mode.get_default_mode().including( "typed_list_inplace_opt") f = aesara.function( [ In(mySymbolicMatricesList1, borrow=True, mutable=True), mySymbolicMatricesList2, ], z, mode=m, ) assert f.maker.fgraph.toposort()[0].op.inplace x = random_ranged(-1000, 1000, [100, 101]) y = random_ranged(-1000, 1000, [100, 101]) assert np.array_equal(f([x], [y]), [x, y])
def test_fixed_partial_shapes(self): x = TensorType("floatX", (None, None))("x") y = specify_shape(x, (None, 5)) assert y.type.shape == (None, 5) x = TensorType("floatX", (3, None))("x") y = specify_shape(x, (None, 5)) assert y.type.shape == (3, 5)
def _input_tensors(self, shape, floatX): intX = str(_conversion_map[floatX]) ndim = len(shape) arr = TensorType(floatX, [False] * ndim)("arr") indices = TensorType(intX, [False] * ndim)("indices") arr.tag.test_value = np.zeros(shape, dtype=floatX) indices.tag.test_value = np.zeros(shape, dtype=intX) return arr, indices
def test_get_vector_length(): x = TensorVariable(TensorType("int64", (4, ))) res = get_vector_length(x) assert res == 4 x = TensorVariable(TensorType("int64", (None, ))) with pytest.raises(ValueError): get_vector_length(x)
def test_dtype_failure(self, floatX): with aesara.config.change_flags(floatX=floatX): arr = TensorType(floatX, [False] * 3)("arr") indices = TensorType(floatX, [False] * 3)("indices") arr.tag.test_value = np.zeros((1, ) * arr.ndim, dtype=floatX) indices.tag.test_value = np.zeros((1, ) * indices.ndim, dtype=floatX) with pytest.raises(IndexError): take_along_axis(arr, indices)
def test_repeatOp(self): for ndim in [1, 3]: x = TensorType(config.floatX, [False] * ndim)() a = np.random.random((10, ) * ndim).astype(config.floatX) for axis in self._possible_axis(ndim): for dtype in integer_dtypes: r_var = scalar(dtype=dtype) r = np.asarray(3, dtype=dtype) if dtype == "uint64" or (dtype in self.numpy_unsupported_dtypes and r_var.ndim == 1): with pytest.raises(TypeError): repeat(x, r_var, axis=axis) else: f = aesara.function([x, r_var], repeat(x, r_var, axis=axis)) assert np.allclose(np.repeat(a, r, axis=axis), f(a, r)) r_var = vector(dtype=dtype) if axis is None: r = np.random.randint(1, 6, size=a.size).astype(dtype) else: r = np.random.randint(1, 6, size=(10, )).astype(dtype) if dtype in self.numpy_unsupported_dtypes and r_var.ndim == 1: with pytest.raises(TypeError): repeat(x, r_var, axis=axis) else: f = aesara.function([x, r_var], repeat(x, r_var, axis=axis)) assert np.allclose(np.repeat(a, r, axis=axis), f(a, r)) # check when r is a list of single integer, e.g. [3]. r = np.random.randint(1, 11, size=()).astype(dtype) + 2 f = aesara.function([x], repeat(x, [r], axis=axis)) assert np.allclose(np.repeat(a, r, axis=axis), f(a)) assert not np.any([ isinstance(n.op, RepeatOp) for n in f.maker.fgraph.toposort() ]) # check when r is aesara tensortype that broadcastable is (True,) r_var = TensorType(broadcastable=(True, ), dtype=dtype)() r = np.random.randint(1, 6, size=(1, )).astype(dtype) f = aesara.function([x, r_var], repeat(x, r_var, axis=axis)) assert np.allclose(np.repeat(a, r[0], axis=axis), f(a, r)) assert not np.any([ isinstance(n.op, RepeatOp) for n in f.maker.fgraph.toposort() ])
def test_ndim_failure(self, floatX): with aesara.config.change_flags(floatX=floatX): intX = str(_conversion_map[floatX]) arr = TensorType(floatX, [False] * 3)("arr") indices = TensorType(intX, [False] * 2)("indices") arr.tag.test_value = np.zeros((1, ) * arr.ndim, dtype=floatX) indices.tag.test_value = np.zeros((1, ) * indices.ndim, dtype=intX) with pytest.raises(ValueError): take_along_axis(arr, indices)
def make_node(self, x, y): x = aet.as_tensor_variable(x) y = aet.as_tensor_variable(y) outdim = x.ndim output1 = TensorType(dtype=aesara.scalar.upcast(x.dtype, y.dtype), broadcastable=[False] * outdim)() output2 = TensorType(dtype=aesara.scalar.upcast(x.dtype, y.dtype), broadcastable=[False] * outdim)() return Apply(self, inputs=[x, y], outputs=[output1, output2])
def test_tensorvariable(self): # Get counter value autoname_id = next(Variable.__count__) Variable.__count__ = count(autoname_id) r1 = TensorType(dtype="int32", shape=())("myvar") r2 = TensorVariable(TensorType(dtype="int32", shape=())) r3 = shared(np.random.standard_normal((3, 4))) assert r1.auto_name == "auto_" + str(autoname_id) assert r2.auto_name == "auto_" + str(autoname_id + 1) assert r3.auto_name == "auto_" + str(autoname_id + 2)
def test_is_super(): test_type = TensorType(config.floatX, [False, False]) test_type2 = TensorType(config.floatX, [False, True]) assert test_type.is_super(test_type) assert test_type.is_super(test_type2) assert not test_type2.is_super(test_type) test_type3 = TensorType(config.floatX, [False, False, False]) assert not test_type3.is_super(test_type)
def test_conv(self): for conv_op in [conv.conv2d, conv2d]: for border_mode in ["valid", "full"]: image_shape = (2, 2, 4, 5) filter_shape = (2, 2, 2, 3) image_dim = len(image_shape) filter_dim = len(filter_shape) input = TensorType(aesara.config.floatX, [False] * image_dim)(name="input") filters = TensorType(aesara.config.floatX, [False] * filter_dim)(name="filter") ev_input = TensorType(aesara.config.floatX, [False] * image_dim)(name="ev_input") ev_filters = TensorType(aesara.config.floatX, [False] * filter_dim)(name="ev_filters") def sym_conv2d(input, filters): return conv_op(input, filters, border_mode=border_mode) output = sym_conv2d(input, filters).flatten() yv = Rop(output, [input, filters], [ev_input, ev_filters]) mode = None if aesara.config.mode == "FAST_COMPILE": mode = "FAST_RUN" rop_f = function( [input, filters, ev_input, ev_filters], yv, on_unused_input="ignore", mode=mode, ) sy, _ = aesara.scan( lambda i, y, x1, x2, v1, v2: (grad(y[i], x1) * v1).sum() + (grad(y[i], x2) * v2).sum(), sequences=aet.arange(output.shape[0]), non_sequences=[ output, input, filters, ev_input, ev_filters ], mode=mode, ) scan_f = function( [input, filters, ev_input, ev_filters], sy, on_unused_input="ignore", mode=mode, ) dtype = aesara.config.floatX image_data = np.random.random(image_shape).astype(dtype) filter_data = np.random.random(filter_shape).astype(dtype) ev_image_data = np.random.random(image_shape).astype(dtype) ev_filter_data = np.random.random(filter_shape).astype(dtype) v1 = rop_f(image_data, filter_data, ev_image_data, ev_filter_data) v2 = scan_f(image_data, filter_data, ev_image_data, ev_filter_data) assert np.allclose(v1, v2), f"Rop mismatch: {v1} {v2}"
def test_nested_list_arg(self): # test for the 'depth' optional argument myNestedType = TypedListType( TensorType(aesara.config.floatX, (False, False)), 3 ) myType = TypedListType(TensorType(aesara.config.floatX, (False, False))) myManualNestedType = TypedListType(TypedListType(TypedListType(myType))) assert myNestedType == myManualNestedType
def test_fixed_shape_variable_basic(): x = TensorVariable(TensorType("int64", (4, ))) assert isinstance(x.shape, Constant) assert np.array_equal(x.shape.data, (4, )) x = TensorConstant(TensorType("int64", (False, False)), np.array([[1, 2], [2, 3]])) assert x.type.shape == (2, 2) with pytest.raises(ValueError): TensorConstant(TensorType("int64", (True, False)), np.array([[1, 2], [2, 3]]))
def test_type_equality(self): # Typed list types should only be equal # when they contains the same aesara # variables # list of matrices myType1 = TypedListType(TensorType(aesara.config.floatX, (False, False))) # list of matrices myType2 = TypedListType(TensorType(aesara.config.floatX, (False, False))) # list of scalars myType3 = TypedListType(TensorType(aesara.config.floatX, ())) assert myType2 == myType1 assert myType3 != myType1
def test_axis(self): variable = TensorType(config.floatX, [False, True, False])() res = squeeze(variable, axis=1) assert res.broadcastable == (False, False) variable = TensorType(config.floatX, [False, True, False])() res = squeeze(variable, axis=(1, )) assert res.broadcastable == (False, False) variable = TensorType(config.floatX, [False, True, False, True])() res = squeeze(variable, axis=(1, 3)) assert res.broadcastable == (False, False)
def test_non_tensor_type(self): mySymbolicNestedMatricesList = TypedListType( TensorType(aesara.config.floatX, (False, False)), 1)() mySymbolicMatricesList = TypedListType( TensorType(aesara.config.floatX, (False, False)))() z = Count()(mySymbolicNestedMatricesList, mySymbolicMatricesList) f = aesara.function( [mySymbolicNestedMatricesList, mySymbolicMatricesList], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert f([[x, y], [x, y, y]], [x, y]) == 1
def test_interface(self): mySymbolicMatricesList1 = TypedListType( TensorType(aesara.config.floatX, (False, False)))() mySymbolicMatricesList2 = TypedListType( TensorType(aesara.config.floatX, (False, False)))() z = mySymbolicMatricesList1.extend(mySymbolicMatricesList2) f = aesara.function([mySymbolicMatricesList1, mySymbolicMatricesList2], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert np.array_equal(f([x], [y]), [x, y])
def test_wrong_input(self): mySymbolicMatricesList = TypedListType( TensorType(aesara.config.floatX, (False, False)))() mySymbolicMatrix = matrix() with pytest.raises(TypeError): GetItem()(mySymbolicMatricesList, mySymbolicMatrix)
def test_basic(self): for type1 in [ "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "float32", "float64", ]: x = TensorType(dtype=type1, broadcastable=(False, ))() for type2, converter in zip( ["int8", "int16", "int32", "int64", "float32", "float64"], [ _convert_to_int8, _convert_to_int16, _convert_to_int32, _convert_to_int64, _convert_to_float32, _convert_to_float64, ], ): y = converter(x) f = function([In(x, strict=True)], y) a = np.arange(10, dtype=type1) b = f(a) assert np.all(b == np.arange(10, dtype=type2))