当前位置: 首页>>代码示例>>Python>>正文


Python tensor.dtensor4函数代码示例

本文整理汇总了Python中theano.tensor.dtensor4函数的典型用法代码示例。如果您正苦于以下问题:Python dtensor4函数的具体用法?Python dtensor4怎么用?Python dtensor4使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了dtensor4函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_conv_no_bias

    def test_conv_no_bias(self):
        images = T.dtensor4('input_conv')
        weights = T.dtensor4('weights')

        images_internal = U2IConv(imshp=(12, 3, 256, 256), kshp=(12, 3, 3, 3))(images)

        convOut = Conv2D(imshp=(12, 3, 256, 256), kshp=(12, 3, 3, 3), filter_flip=False)(images_internal, weights)
        convOut_user = I2U()(convOut)
        convOutLoss = T.mean(convOut_user)
        conv_op_di = T.grad(convOutLoss, images)
        conv_op_dk = T.grad(convOutLoss, weights)
        convOutBack = [conv_op_di, conv_op_dk]

        ival = numpy.random.rand(12, 3, 256, 256).astype(numpy.float64)
        wval = numpy.random.rand(12, 3, 3, 3).astype(numpy.float64)

        fopt = theano.function(inputs=[images, weights], outputs=convOutBack, mode=mode_with_mkl)
        new_out = fopt(ival, wval)

        convOut = conv2d(images, weights, input_shape=(12, 3, 256, 256), filter_shape=(12, 3, 3, 3), filter_flip=False)
        convOutLoss = T.mean(convOut)
        conv_op_di = T.grad(convOutLoss, images)
        conv_op_dk = T.grad(convOutLoss, weights)
        convOutBack = [conv_op_di, conv_op_dk]

        fori = theano.function(inputs=[images, weights], outputs=convOutBack, mode=mode_without_mkl)
        old_out = fori(ival, wval)

        assert len(fopt.maker.fgraph.toposort()) != len(fori.maker.fgraph.toposort())
        assert numpy.allclose(old_out[0], new_out[0])
        assert new_out[0].dtype == 'float64'
开发者ID:pcs-theano,项目名称:Theano,代码行数:31,代码来源:test_conv.py

示例2: test_infer_shape

    def test_infer_shape(self):
        image = tensor.dtensor4()
        maxout = tensor.dtensor4()
        gz = tensor.dtensor4()
        rng = numpy.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]]]

        for i, maxpoolshp in enumerate(maxpoolshps):
            for j, ignore_border in enumerate([True, False]):

                # checking shapes generated by DownsampleFactorMax
                self._compile_and_check([image],
                        [DownsampleFactorMax(maxpoolshp,
                        ignore_border=ignore_border)(image)],
                        [image_val], DownsampleFactorMax)

                # checking shapes generated by DownsampleFactorMaxGrad
                maxout_val = rng.rand(*out_shapes[i][j])
                gz_val = rng.rand(*out_shapes[i][j])
                self._compile_and_check([image, maxout, gz],
                        [DownsampleFactorMaxGrad(maxpoolshp,
                        ignore_border=ignore_border)(image, maxout, gz)],
                        [image_val, maxout_val, gz_val],
                                        DownsampleFactorMaxGrad)
开发者ID:errord,项目名称:Theano,代码行数:31,代码来源:test_downsample.py

示例3: test_relu_grad

    def test_relu_grad(self):
        seed = utt.fetch_seed()
        rng = numpy.random.RandomState(seed)

        imgsize_list = ((5, 5), (6, 6), (6, 6), (8, 8))
        n, c = 4, 2

        axis = 1

        image = T.dtensor4('image')
        image1 = T.dtensor4('image1')
        for imgsize in imgsize_list:
            imval = rng.rand(n, c, imgsize[0], imgsize[1])

            out = T.concatenate([image, image1], axis)
            sum_ref = T.sum(out)
            gx_ref = T.grad(sum_ref, [image, image1])
            f_ref = theano.function([image, image1], outputs=gx_ref, mode=mode_without_mkl)
            output_ref = f_ref(imval, imval)

            out_mkl = self.mkl_concatenate_func(axis, image, image1)
            sum_mkl = T.sum(out_mkl)
            gx_mkl = T.grad(sum_mkl, [image, image1])
            f_mkl = theano.function([image, image1], outputs=gx_mkl)
            output_mkl = f_mkl(imval, imval)

            utt.assert_allclose(output_mkl, output_ref)
开发者ID:pcs-theano,项目名称:Theano,代码行数:27,代码来源:test_concatenate.py

示例4: test_infer_shape_gradI

    def test_infer_shape_gradI(self):

        def rand(*shape):
            r = numpy.asarray(numpy.random.rand(*shape), dtype='float64')
            return r * 2 - 1
        corrMM = corr.CorrMM
        gradI = corr.CorrMM_gradInputs

        adtens = T.dtensor4()
        bdtens = T.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 = theano.function([adtens, bdtens], cdtens)
                    cdtens_val = f(adtens_val, bdtens_val)
                    # CorrMM_gradInputs
                    shape = (theano.shared(aivec_val[2]), theano.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)
开发者ID:5730279821-TA,项目名称:Theano,代码行数:34,代码来源:test_corr.py

示例5: test_conv_with_bias

    def test_conv_with_bias(self):
        images = T.dtensor4('inputs')
        weights = T.dtensor4('weights')
        bias = T.dvector('bias')

        ishape = [(8, 3, 256, 256), (16, 3, 256, 256), (32, 3, 256, 256), (64, 3, 256, 256)]
        wshape = [(8, 3, 3, 3), (16, 3, 3, 3), (32, 3, 3, 3), (64, 3, 3, 3)]

        for i, ish in enumerate(ishape):
            wsh = wshape[i]
            images_internal = U2IConv(imshp=ish, kshp=wsh)(images)
            convOutBias_internal = Conv2D(imshp=ish, kshp=wsh, filter_flip=False)(images_internal, weights, bias)
            convOutBias_user = I2U()(convOutBias_internal)

            ival = numpy.random.rand(*ish).astype(numpy.float64)
            wval = numpy.random.rand(*wsh).astype(numpy.float64)
            bval = numpy.random.rand(wsh[0]).astype(numpy.float64)

            fopt = theano.function(inputs=[images, weights, bias], outputs=convOutBias_user, mode=mode_with_mkl)
            new_old = fopt(ival, wval, bval)

            convOut = conv2d(images, weights, input_shape=ish, filter_shape=wsh, filter_flip=False)
            convOutBias = convOut + bias.dimshuffle('x', 0, 'x', 'x')
            fori = theano.function(inputs=[images, weights, bias], outputs=convOutBias, mode=mode_without_mkl)
            old_out = fori(ival, wval, bval)

            assert str(fopt.maker.fgraph.toposort()) != str(fori.maker.fgraph.toposort())
            assert numpy.allclose(old_out, new_old)
开发者ID:pcs-theano,项目名称:Theano,代码行数:28,代码来源:test_conv.py

示例6: test_infer_shape_gradW

    def test_infer_shape_gradW(self):
        if theano.config.mode == "FAST_COMPILE":
            raise SkipTest("CorrMM don't work in FAST_COMPILE")

        def rand(*shape):
            r = numpy.asarray(numpy.random.rand(*shape), dtype="float64")
            return r * 2 - 1

        corrMM = corr.CorrMM
        gradW = corr.CorrMM_gradWeights

        adtens = T.dtensor4()
        bdtens = T.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], [11, 1, 3, 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)
                    f = theano.function([adtens, bdtens], cdtens)
                    cdtens_val = f(adtens_val, bdtens_val)
                    # CorrMM_gradWeights
                    shape = (theano.shared(bivec_val[2]), theano.shared(bivec_val[3]))
                    bdtens_g = gradW(border_mode=mode, subsample=subsample)(adtens, cdtens, shape=shape)
                    self._compile_and_check([adtens, cdtens], [bdtens_g], [adtens_val, cdtens_val], gradW, warn=False)
开发者ID:ChinaQuants,项目名称:Theano,代码行数:31,代码来源:test_corr.py

示例7: test_concatenate

    def test_concatenate(self):
        def ref(*inputs):
            axis = inputs[0]
            tensors = inputs[1:]

            return numpy.concatenate(tensors, axis)

        seed = utt.fetch_seed()
        rng = numpy.random.RandomState(seed)

        imgsize_list = ((5, 5), (6, 6), (6, 6), (8, 8))
        n, c = 4, 2

        axis = 1

        image = T.dtensor4('image')
        image1 = T.dtensor4('image1')
        for imgsize in imgsize_list:
            imval = rng.rand(n, c, imgsize[0], imgsize[1])

            output_ref = ref(axis, imval, imval)

            Opout = self.mkl_concatenate_func(axis, image, image1)
            f = function([image, image1], [Opout, ])
            output_mkl = f(imval, imval)

            utt.assert_allclose(output_mkl, output_ref)
开发者ID:pcs-theano,项目名称:Theano,代码行数:27,代码来源:test_concatenate.py

示例8: test_infer_shape_forward

    def test_infer_shape_forward(self):
        if theano.config.mode == "FAST_COMPILE":
            raise SkipTest("CorrMM don't work in FAST_COMPILE")

        def rand(*shape):
            r = numpy.asarray(numpy.random.rand(*shape), dtype='float64')
            return r * 2 - 1
        corrMM = corr.CorrMM

        adtens = T.dtensor4()
        bdtens = T.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)
开发者ID:HHiroki,项目名称:Theano,代码行数:29,代码来源:test_corr.py

示例9: setUp

 def setUp(self):
     super (TestConv2D, self).setUp()
     self.input = T.dtensor4('input')
     self.input.name = 'default_V'
     self.filters = T.dtensor4('filters')
     self.filters.name = 'default_filters'
     if not conv.imported_scipy_signal and theano.config.cxx == "":
         raise SkipTest("conv2d tests need SciPy or a c++ compiler")
开发者ID:Donghuan,项目名称:Theano,代码行数:8,代码来源:test_conv.py

示例10: test_infer_shape

    def test_infer_shape(self):
        image = tensor.dtensor4()
        maxout = tensor.dtensor4()
        gz = tensor.dtensor4()
        rng = numpy.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, padding in enumerate([(0, 0), (1, 1), (1, 2)]):
                    if out_shapes[k][i][j] is None:
                        continue
                    # checking shapes generated by DownsampleFactorMax
                    self._compile_and_check([image],
                                            [DownsampleFactorMax(maxpoolshp,
                                                                 ignore_border=ignore_border,
                                                                 padding=padding)(image)],
                                            [image_val], DownsampleFactorMax)

                    # 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(maxpoolshp,
                                                         ignore_border=ignore_border,
                                                         padding=padding)
                                            (image, maxout, gz)],
                                            [image_val, maxout_val, gz_val],
                                            MaxPoolGrad,
                                            warn=False)
        # checking with broadcastable input
        image = tensor.tensor(dtype='float64',
                              broadcastable=(False, False, True, True))
        image_val = rng.rand(4, 6, 1, 1)
        self._compile_and_check(
            [image],
            [DownsampleFactorMax((2, 2),
                                 ignore_border=True,
                                 padding=(0, 0))(image)],
            [image_val], DownsampleFactorMax)
开发者ID:hhoareau,项目名称:Theano,代码行数:57,代码来源:test_downsample.py

示例11: test_no_shape

    def test_no_shape(self):
        images = T.dtensor4('inputs')
        weights = T.dtensor4('weights')

        convOut = conv2d(images, weights, filter_shape=(12, 3, 3, 3), filter_flip=False)

        fopt = theano.function(inputs=[images, weights], outputs=convOut, mode=mode_with_mkl)

        fori = theano.function(inputs=[images, weights], outputs=convOut, mode=mode_without_mkl)

        # No optimization for the case image shape is None
        assert all([not isinstance(n, (Conv2D, U2IConv, I2U)) for n in fopt.maker.fgraph.toposort()])
        assert str(fopt.maker.fgraph.toposort()) == str(fori.maker.fgraph.toposort())
开发者ID:pcs-theano,项目名称:Theano,代码行数:13,代码来源:test_conv.py

示例12: test_pool_stride_padding

    def test_pool_stride_padding(self):
        rng = numpy.random.RandomState(utt.fetch_seed())
        # generate random images
        ds_list = ((3, 3), (4, 4), (3, 4), (5, 5))
        st_list = ((1, 1), (2, 2), (3, 3), (1, 2))
        pad_list = ((1, 1), (0, 0), (1, 1), (1, 1))
        imgsize_list = ((5, 5), (6, 6), (6, 6), (8, 8))
        n = 4
        c = 2

        images = T.dtensor4()

        for idx, ignore_border, mode in product(numpy.arange(len(ds_list)),
                                                [False],
                                                ['max',
                                                 'average_exc_pad']):
            imgsize = imgsize_list[idx]
            imval = rng.rand(n, c, imgsize[0], imgsize[1])
            ds = ds_list[idx]
            st = st_list[idx]
            pad = pad_list[idx]

            # Pure Numpy computation
            numpy_output_val = self.numpy_pool_2d_stride_padding(imval, ds,
                                                                 ignore_border, st,
                                                                 pad, mode)

            # MKL Ops
            output = self.mkl_pool_func(images, ignore_border, mode, ds, st, pad)

            f = function([images, ], [output, ])
            output_val = f(imval)
            utt.assert_allclose(output_val, numpy_output_val)
开发者ID:intel,项目名称:theano,代码行数:33,代码来源:test_pool.py

示例13: __init__

 def __init__(self, minibatch_size=128, epochs=1, learn_rate=1e-3,
         bottleneck_width=10, **kwargs):
     '''Initialize a ready-to-train convolutional autoencoder.'''
     super(IBDPairConvAe, self).__init__(self)
     # Shapes are given as (batch, depth, height, width)
     nchannels = kwargs.get('nchannels', 4)
     weighted = kwargs.get('weighted_cost', False)
     self.minibatch_shape = (minibatch_size, nchannels, 8, 24)
     self.minibatch_size = minibatch_size
     self.image_shape = self.minibatch_shape[1:-1]
     self.num_features = reduce(mul, self.image_shape)
     self.epochs = epochs
     self.learn_rate = learn_rate
     self.bottleneck_width = bottleneck_width
     self.input_var = T.dtensor4('input')
     self.network = self._setup_network()
     self.train_prediction = self._setup_prediction(deterministic=False)
     self.test_prediction = self._setup_prediction(deterministic=True)
     self.train_cost = self._setup_cost(deterministic=False,
             weighted=weighted)
     self.test_cost = self._setup_cost(deterministic=True, array=True)
     self.optimizer = self._setup_optimizer()
     self.train_once = theano.function([self.input_var],
         [self.train_cost], updates=self.optimizer)
     self.predict_fn = theano.function([self.input_var],
         [self.test_cost, self.test_prediction])
开发者ID:NERSC,项目名称:dayabay-learn,代码行数:26,代码来源:LasagneConv.py

示例14: test_gauntlet

    def test_gauntlet(self):
        maxpoolshps = ((1, 1), (3, 3), (5, 3),)
        stridesizes = ((1, 1), (3, 3), (5, 7),)
        # generate random images
        imval = self.rng.rand(4, 10, 16, 16)

        images = T.dtensor4()
        for index_type, index_scope, maxpoolshp, stride, ignore_border in product(['flattened',
                                                        'array'],
                                                        ['local',
                                                         'global'],
                                                       maxpoolshps,
                                                       stridesizes,
                                                       [True, False]):
                # Pool op
                max_pool_op = Pool(ds=maxpoolshp, 
                                   ignore_border=ignore_border, 
                                   st=stride, mode='max')(images)
                max_pool_f = theano.function([images], max_pool_op)
                maxpool_output_val = max_pool_f(imval)
                
                maxpoolswitch_op = MaxPoolSwitch(ds = maxpoolshp,
                                                 ignore_border=ignore_border,
                                                 st=stride, index_type=index_type, 
                                                 index_scope=index_scope)(images)
                f = theano.function([images], maxpoolswitch_op, mode='DebugMode')
                output_val = f(imval)
                self.check_max_and_switches(imval, output_val, maxpool_output_val, 
                                            maxpoolshp, ignore_border, stride, None, 
                                            index_type, index_scope)
开发者ID:bokorn,项目名称:Keras-and-Theano-layers-for-Switched-Pooling,代码行数:30,代码来源:test_switched_pooling.py

示例15: _make_graph

  def _make_graph(self):
    if not self.twod_inputs:
      self._inputs = TT.dtensor4("inputs")
      layer_outputs = self._inputs
    else:
      self._inputs = TT.matrix("inputs")
      layer_outputs = self._inputs.reshape(self.img_shape)
    
    for i in range(0, len(self.conv_weights)):
      # Perform the convolution.
      conv_out = conv.conv2d(layer_outputs, self.conv_weights[i],
          filter_shape = self.filter_shapes[i],
          image_shape = self.layer_shapes[i])
      
      # Downsample the feature maps.
      pooled_out = downsample.max_pool_2d(conv_out, self.pool_sizes[i],
          ignore_border = True)

      # Account for the bias. Since it is a vector, we first need to reshape it
      # to (1, n_filters, 1, 1).
      layer_outputs = self.activation_func(pooled_out + \
          self.conv_biases[i].dimshuffle("x", 0, "x", "x"))

    # Concatenate output maps into one big matrix where each row is the
    # concatenation of all the feature maps from one item in the batch.
    next_shape = self.layer_shapes[i + 1]
    new_shape = (next_shape[0], reduce(mul, next_shape[1:], 1))
    print "New Shape: " + str(new_shape)
    self.x = layer_outputs.reshape(new_shape)
开发者ID:djpetti,项目名称:dec-meg-2014,代码行数:29,代码来源:weight_sharing.py


注:本文中的theano.tensor.dtensor4函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。