def test_fail(self): # Test that conv2d fails for dimensions other than 2 or 3. with pytest.raises(Exception): conv.conv2d(dtensor4(), dtensor3()) with pytest.raises(Exception): conv.conv2d(dtensor3(), dvector())
def test_norm(self, axis): x = dtensor3() a = np.random.random((3, 2, 4)).astype(aesara.config.floatX) mode = Mode(optimizer="fast_compile", linker="py") f = function( [x], [ x.norm(L=1, axis=axis, keepdims=True), self.makeKeepDims_local( x, x.norm(L=1, axis=axis, keepdims=False), axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape g = function( [x], [ x.norm(L=2, axis=axis, keepdims=True), self.makeKeepDims_local( x, x.norm(L=2, axis=axis, keepdims=False), axis), ], mode=mode, ) ans1, ans2 = g(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape
def test_max_pool_3d_3D_deprecated_interface(self): rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1, 1), (3, 2, 1)) imval = rng.rand(4, 5, 6) images = dtensor3() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"], ): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) output = pool_3d( input=images, ds=maxpoolshp, ignore_border=ignore_border, st=maxpoolshp, padding=(0, 0, 0), mode=mode, ) output_val = function([images], output)(imval) utt.assert_allclose(output_val, numpy_output_val) def mp(input): return pool_3d(input, maxpoolshp, ignore_border, mode=mode)
def test_wrong_input(self): # Make sure errors are raised when image and kernel are not 4D tensors with pytest.raises(Exception): self.validate((3, 2, 8, 8), (4, 2, 5, 5), "valid", input=dmatrix()) with pytest.raises(Exception): self.validate((3, 2, 8, 8), (4, 2, 5, 5), "valid", filters=dvector()) with pytest.raises(Exception): self.validate((3, 2, 8, 8), (4, 2, 5, 5), "valid", input=dtensor3())
def test_norm(self): x = dtensor3() a = np.random.rand(3, 2, 4).astype(aesara.config.floatX) mode = Mode(optimizer="fast_compile", linker="py") for axis in [ 0, 1, 2, [0], [1], [2], None, [0, 1], [1, 2], [0, 1, 2], [-1], [-2], [-3], [-1, -2], [-1, -2, -3], [0, -2, 2], ]: f = function( [x], [ x.norm(L=1, axis=axis, keepdims=True), self.makeKeepDims_local( x, x.norm(L=1, axis=axis, keepdims=False), axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape g = function( [x], [ x.norm(L=2, axis=axis, keepdims=True), self.makeKeepDims_local( x, x.norm(L=2, axis=axis, keepdims=False), axis), ], mode=mode, ) ans1, ans2 = g(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape
def test_infer_shape(self): z = dtensor3() x = dmatrix() y = dscalar() self._compile_and_check( [x, y], [self.op(x, y)], [np.random.random((8, 5)), np.random.random()], self.op_class, ) self._compile_and_check( [z, y], [self.op(z, y)], # must be square when nd>2 [np.random.random((8, 8, 8)), np.random.random()], self.op_class, warn=False, )
def test_max_pool_2d_3D(self): rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = [(1, 2)] imval = rng.rand(2, 3, 4) images = dtensor3() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"], ): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) output_val = function([images], output)(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_single_or_any_axis(self, axis, op): # the following ops can be specified with either a single axis or every # axis: x = dtensor3() a = np.random.random((3, 2, 4)) # We don't need to test all opt and C code, as this is tested # by the ops tests. mode = Mode(optimizer="fast_compile", linker="py") f = function( [x], [ op(x, axis=axis, keepdims=True), self.makeKeepDims_local(x, op(x, axis=axis, keepdims=False), axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape
def test_max_and_argmax(self, axis, op): x = dtensor3() a = np.random.random((3, 2, 4)) # We don't need to test all opt and C code, as this is tested # by the ops tests. mode = Mode(optimizer="fast_compile", linker="py") # 'max_and_argmax' has two outputs and can be specified with either # a single or every axis: f = function( [x], [ op(x, axis=axis, keepdims=True)[0], self.makeKeepDims_local(x, op(x, axis=axis, keepdims=False)[0], axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape f = function( [x], [ op(x, axis=axis, keepdims=True)[1], self.makeKeepDims_local(x, op(x, axis=axis, keepdims=False)[1], axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape
def test_keepdims(self): x = dtensor3() a = np.random.rand(3, 2, 4) # We don't need to test all opt and C code, as this is tested # by the ops tests. mode = Mode(optimizer="fast_compile", linker="py") # 'max_and_argmax' has two outputs and can be specified with either # a single or every axis: for axis in [ 0, 1, 2, [0], [1], [2], None, [0, 1, 2], [-1], [-2], [-3], [-1, -2, -3], [0, -1, -2], [-2, -3, 2], ]: op = max_and_argmax f = function( [x], [ op(x, axis=axis, keepdims=True)[0], self.makeKeepDims_local( x, op(x, axis=axis, keepdims=False)[0], axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape f = function( [x], [ op(x, axis=axis, keepdims=True)[1], self.makeKeepDims_local( x, op(x, axis=axis, keepdims=False)[1], axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape # the following ops can be specified with either a single axis or every # axis: for op in [argmax, argmin]: for axis in [ 0, 1, 2, [0], [1], [2], None, [0, 1, 2], [-1], [-2], [-3], [-1, -2, -3], [0, -2, 2], ]: f = function( [x], [ op(x, axis=axis, keepdims=True), self.makeKeepDims_local( x, op(x, axis=axis, keepdims=False), axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape # the following ops can be specified with a freely specified axis # parameter for op in [ tt_sum, prod, mean, var, std, tt_all, tt_any, tt_max, tt_min, ]: for axis in [ 0, 1, 2, [0], [1], [2], None, [0, 1], [1, 2], [0, 1, 2], [-1], [-2], [-3], [-1, -2], [-1, -2, -3], [0, -2, 2], ]: f = function( [x], [ op(x, axis=axis, keepdims=True), self.makeKeepDims_local( x, op(x, axis=axis, keepdims=False), axis), ], mode=mode, ) ans1, ans2 = f(a) assert np.allclose(ans1, ans2) assert ans1.shape == ans2.shape