def test_infer_shape_gradI(self): rng = np.random.default_rng(280284) def rand(*shape): r = np.asarray(rng.random(shape), dtype="float64") return r * 2 - 1 corrMM = corr.CorrMM gradI = corr.CorrMM_gradInputs adtens = dtensor4() bdtens = dtensor4() aivec_vals = [ [1, 5, 6, 3], [8, 2, 7, 3], [1, 6, 9, 4], [9, 6, 8, 5], [9, 1, 6, 8], ] bivec_vals = [ [7, 5, 3, 1], [4, 2, 5, 3], [12, 6, 3, 2], [5, 6, 1, 3], [7, 1, 3, 4], ] modes = ["valid", "full", "half", (1, 1), (2, 1), (1, 2), 1] subsamples = [(1, 1), (2, 1), (1, 2)] for aivec_val, bivec_val in zip(aivec_vals, bivec_vals): adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) for mode in modes: for subsample in subsamples: # CorrMM cdtens = corrMM(border_mode=mode, subsample=subsample)( adtens, bdtens ) f = aesara.function([adtens, bdtens], cdtens) cdtens_val = f(adtens_val, bdtens_val) # CorrMM_gradInputs shape = (aesara.shared(aivec_val[2]), aesara.shared(aivec_val[3])) adtens_g = gradI(border_mode=mode, subsample=subsample)( bdtens, cdtens, shape=shape ) self._compile_and_check( [bdtens, cdtens], [adtens_g], [bdtens_val, cdtens_val], gradI, warn=False, )
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_more_shapes(self): # TODO: generalize infer_shape to account for tensor variable # (non-constant) input shape admat = dmatrix() ndim = 1 admat_val = random(3, 4) self._compile_and_check([admat], [Reshape(ndim)(admat, [12])], [admat_val], Reshape) self._compile_and_check([admat], [Reshape(ndim)(admat, [-1])], [admat_val], Reshape) ndim = 2 self._compile_and_check([admat], [Reshape(ndim)(admat, [4, 3])], [admat_val], Reshape) self._compile_and_check([admat], [Reshape(ndim)(admat, [4, -1])], [admat_val], Reshape) self._compile_and_check([admat], [Reshape(ndim)(admat, [3, -1])], [admat_val], Reshape) self._compile_and_check([admat], [Reshape(ndim)(admat, [-1, 3])], [admat_val], Reshape) self._compile_and_check([admat], [Reshape(ndim)(admat, [-1, 4])], [admat_val], Reshape) aivec = ivector() self._compile_and_check([admat, aivec], [Reshape(ndim)(admat, aivec)], [admat_val, [4, 3]], Reshape) self._compile_and_check([admat, aivec], [Reshape(ndim)(admat, aivec)], [admat_val, [4, -1]], Reshape) adtens4 = dtensor4() ndim = 4 adtens4_val = random(2, 4, 3, 5) self._compile_and_check([adtens4], [Reshape(ndim)(adtens4, [1, -1, 10, 4])], [adtens4_val], Reshape) self._compile_and_check([adtens4], [Reshape(ndim)(adtens4, [1, 3, 10, 4])], [adtens4_val], Reshape) self._compile_and_check( [adtens4, aivec], [Reshape(ndim)(adtens4, aivec)], [adtens4_val, [1, -1, 10, 4]], Reshape, ) self._compile_and_check( [adtens4, aivec], [Reshape(ndim)(adtens4, aivec)], [adtens4_val, [1, 3, 10, 4]], Reshape, )
def test_infer_shape_forward(self): rng = np.random.default_rng(280284) def rand(*shape): r = np.asarray(rng.random(shape), dtype="float64") return r * 2 - 1 corrMM = corr.CorrMM adtens = dtensor4() bdtens = dtensor4() aivec_vals = [ [4, 5, 6, 3], [6, 2, 8, 3], [3, 6, 7, 5], [3, 6, 7, 5], [5, 2, 4, 3], ] bivec_vals = [ [7, 5, 3, 2], [4, 2, 5, 3], [5, 6, 3, 2], [5, 6, 2, 3], [6, 2, 4, 3], ] modes = ["valid", "full", "half", (1, 1), (2, 1), (1, 2), 1] subsamples = [(1, 1), (2, 1), (1, 2)] for aivec_val, bivec_val in zip(aivec_vals, bivec_vals): adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) for mode in modes: for subsample in subsamples: # CorrMM cdtens = corrMM(border_mode=mode, subsample=subsample)( adtens, bdtens ) self._compile_and_check( [adtens, bdtens], [cdtens], [adtens_val, bdtens_val], corrMM, warn=False, )
def test_local_dimshuffle_subtensor(): dimshuffle_subtensor = out2in(local_dimshuffle_subtensor) x = dtensor4("x") x = aet.patternbroadcast(x, (False, True, False, False)) i = iscalar("i") out = x[:, :, 10:30, ::i].dimshuffle(0, 2, 3) g = FunctionGraph([x, i], [out]) dimshuffle_subtensor(g) topo = g.toposort() assert any([not isinstance(x, DimShuffle) for x in topo]) # Test dimshuffle remove dimensions the subtensor don't "see". x = tensor(broadcastable=(False, True, False), dtype="float64") out = x[i].dimshuffle(1) g = FunctionGraph([x, i], [out]) dimshuffle_subtensor(g) topo = g.toposort() assert any([not isinstance(x, DimShuffle) for x in topo]) # Test dimshuffle remove dimensions the subtensor don't "see" but # have in between dimensions. x = tensor(broadcastable=(False, True, False, True), dtype="float64") out = x[i].dimshuffle(1) f = aesara.function([x, i], out) topo = f.maker.fgraph.toposort() assert any([not isinstance(x, DimShuffle) for x in topo]) assert f(np.random.rand(5, 1, 4, 1), 2).shape == (4, ) # Test a corner case that had Aesara return a bug. x = dtensor4("x") x = aet.patternbroadcast(x, (False, True, False, False)) assert x[:, :, 0:3, ::-1].dimshuffle(0, 2, 3).eval({ x: np.ones((5, 1, 6, 7)) }).shape == (5, 3, 7)
def test_neibs_full_with_inconsistent_borders(self): shape = (2, 3, 5, 5) images = dtensor4() images_val = np.arange(np.prod(shape), dtype="float32").reshape(shape) f = aesara.function([images], aet.sqr(images2neibs(images, (2, 2), mode="full")), mode=self.mode) with pytest.raises(TypeError): f(images_val)
def test_wrong_input(self): # Make sure errors are raised when image and kernel are not 5D tensors with pytest.raises(Exception): self.validate((3, 2, 8, 8, 8), (4, 2, 5, 5, 5), "valid", input=dmatrix()) with pytest.raises(Exception): self.validate((3, 2, 8, 8, 8), (4, 2, 5, 5, 5), "valid", input=vector()) with pytest.raises(Exception): self.validate((3, 2, 8, 8, 8), (4, 2, 5, 5, 5), "valid", input=dtensor3()) with pytest.raises(Exception): self.validate((3, 2, 8, 8, 8), (4, 2, 5, 5, 5), "valid", input=dtensor4())
def test_infer_shape(self): rng = np.random.RandomState(3453) adtens4 = dtensor4() aivec = ivector() aivec_val = [3, 4, 2, 5] adtens4_val = rng.rand(*aivec_val) self._compile_and_check( [adtens4, aivec], [SpecifyShape()(adtens4, aivec)], [adtens4_val, aivec_val], SpecifyShape, )
def test_infer_shape(self): rng = np.random.default_rng(3453) adtens4 = dtensor4() aivec = TensorVariable(TensorType("int64", (4, ))) aivec_val = [3, 4, 2, 5] adtens4_val = rng.random(aivec_val) self._compile_and_check( [adtens4, aivec], [specify_shape(adtens4, aivec)], [adtens4_val, aivec_val], SpecifyShape, )
def test_infer_shape_partial(self): rng = np.random.default_rng(3453) adtens4 = dtensor4() aivec = [iscalar(), iscalar(), None, iscalar()] aivec_val = [3, 4, 5] adtens4_val = rng.random((3, 4, 2, 5)) self._compile_and_check( [adtens4, *(ivec for ivec in aivec if ivec is not None)], [specify_shape(adtens4, aivec)], [adtens4_val, *aivec_val], SpecifyShape, )
def test_DownsampleFactorMaxStrideExtra(self): rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) outputshps = ( (4, 10, 4, 7), (4, 10, 5, 8), (4, 10, 2, 3), (4, 10, 3, 4), (4, 10, 2, 3), (4, 10, 2, 3), (4, 10, 4, 1), (4, 10, 4, 1), (4, 10, 3, 2), (4, 10, 4, 2), (4, 10, 1, 0), (4, 10, 1, 1), (4, 10, 0, 0), (4, 10, 1, 1), ) images = dtensor4() for indx in np.arange(len(maxpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(4, 10, imvsize[0], imvsize[1]) stride = stridesizes[indx] maxpoolshp = maxpoolshps[indx] for ignore_border, mode in product( [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"]): indx_out = indx * 2 if not ignore_border: indx_out += 1 outputshp = outputshps[indx_out] # Pool op numpy_output_val = self.numpy_max_pool_2d_stride( imval, maxpoolshp, ignore_border, stride, mode) assert (numpy_output_val.shape == outputshp ), "outshape is {}, calculated shape is {}".format( outputshp, numpy_output_val.shape, ) maxpool_op = Pool(ignore_border=ignore_border, ndim=len(maxpoolshp), mode=mode)(images, maxpoolshp, stride) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_rebroadcast(self): rng = np.random.RandomState(3453) # Rebroadcast adtens4 = dtensor4() adict = [(0, False), (1, True), (2, False), (3, True)] adtens4_val = rng.rand(2, 1, 3, 1).astype(config.floatX) self._compile_and_check( [adtens4], [Rebroadcast(*adict)(adtens4)], [adtens4_val], Rebroadcast, warn=False, ) adtens4_bro = TensorType("float64", (True, True, True, False))() bdict = [(0, True), (1, False), (2, False), (3, False)] adtens4_bro_val = rng.rand(1, 1, 1, 3).astype(config.floatX) self._compile_and_check( [adtens4_bro], [Rebroadcast(*bdict)(adtens4_bro)], [adtens4_bro_val], Rebroadcast, )
def test_infer_shape(self): # Note: infer_shape is incomplete and thus input and filter shapes # must be provided explicitly def rand(*shape): r = np.asarray(np.random.rand(*shape), dtype="float64") return r * 2 - 1 adtens = dtensor4() bdtens = dtensor4() aivec_val = [4, 5, 6, 3] bivec_val = [7, 5, 3, 2] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [6, 2, 8, 3] bivec_val = [4, 2, 5, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [3, 6, 7, 5] bivec_val = [5, 6, 3, 2] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [3, 6, 7, 5] bivec_val = [5, 6, 2, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) aivec_val = [5, 2, 4, 3] bivec_val = [6, 2, 4, 3] adtens_val = rand(*aivec_val) bdtens_val = rand(*bivec_val) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="valid") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], ) self._compile_and_check( [adtens, bdtens], [ self.conv2d( adtens, bdtens, aivec_val, bivec_val, border_mode="full") ], [adtens_val, bdtens_val], conv.ConvOp, excluding=["conv_gemm"], )
def test_infer_shape(self): image = dtensor4() maxout = dtensor4() gz = dtensor4() rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3), (3, 2)) image_val = rng.rand(4, 6, 7, 9) out_shapes = [ [ [[4, 6, 7, 9], [4, 6, 7, 9]], [[4, 6, 3, 4], [4, 6, 4, 5]], [[4, 6, 2, 3], [4, 6, 3, 3]], [[4, 6, 3, 3], [4, 6, 4, 3]], [[4, 6, 2, 4], [4, 6, 3, 5]], ], [ [None, None], [[4, 6, 4, 5], None], [[4, 6, 3, 3], None], [[4, 6, 4, 3], None], [[4, 6, 3, 5], None], ], [ [None, None], [None, None], [[4, 6, 3, 4], None], [[4, 6, 4, 4], None], [None, None], ], ] for i, maxpoolshp in enumerate(maxpoolshps): for j, ignore_border in enumerate([True, False]): for k, pad in enumerate([(0, 0), (1, 1), (1, 2)]): if out_shapes[k][i][j] is None: continue # checking shapes generated by Pool self._compile_and_check( [image], [ Pool(ignore_border=ignore_border)( image, maxpoolshp, pad=pad) ], [image_val], Pool, ) # checking shapes generated by MaxPoolGrad maxout_val = rng.rand(*out_shapes[k][i][j]) gz_val = rng.rand(*out_shapes[k][i][j]) self._compile_and_check( [image, maxout, gz], [ MaxPoolGrad(ignore_border=ignore_border)( image, maxout, gz, maxpoolshp, pad=pad) ], [image_val, maxout_val, gz_val], MaxPoolGrad, warn=False, ) # checking with broadcastable input image = tensor(dtype="float64", broadcastable=(False, False, True, True)) image_val = rng.rand(4, 6, 1, 1) self._compile_and_check( [image], [Pool(ignore_border=True)(image, (2, 2), pad=(0, 0))], [image_val], Pool, )