• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python tensor.as_tensor_variable函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: test_downsample

def test_downsample():
    shps = [
        (1, 1, 1, 12),
        (1, 1, 2, 2),
        (1, 1, 1, 1),
        (1, 1, 4, 4),
        (1, 1, 10, 11),
        (1, 2, 2, 2),
        (3, 5, 4, 4),
        (25, 1, 7, 7),
        (1, 1, 12, 12),
        (1, 1, 2, 14),
        (1, 1, 12, 14),
        (1, 1, 14, 14),
        (1, 1, 16, 16),
        (1, 1, 18, 18),
        (1, 1, 24, 24),
        (1, 6, 24, 24),
        (10, 1, 24, 24),
        (10, 6, 24, 24),
        (30, 6, 12, 12),
        (30, 2, 24, 24),
        (30, 6, 24, 24),
        (10, 10, 10, 11),
        (1, 1, 10, 1025),
        (1, 1, 10, 1023),
        (1, 1, 1025, 10),
        (1, 1, 1023, 10),
    ]

    numpy.random.RandomState(unittest_tools.fetch_seed()).shuffle(shps)

    for shp in shps:
        for ds in (2, 2), (3, 2), (1, 1):
            if ds[0] > shp[2]:
                continue
            if ds[1] > shp[3]:
                continue
            # GpuDownsampleFactorMax doesn't like having more than 512 columns
            # in the output tensor.
            if float(shp[3]) / ds[1] > 512:
                continue
            for ignore_border in (True, False):
                print "test_downsample", shp, ds, ignore_border
                ds_op = DownsampleFactorMax(ds, ignore_border=ignore_border)

                a = tcn.shared_constructor(my_rand(*shp), "a")
                f = pfunc([], ds_op(tensor.as_tensor_variable(a)), mode=mode_with_gpu)
                f2 = pfunc([], ds_op(tensor.as_tensor_variable(a)), mode=mode_without_gpu)
                assert any([isinstance(node.op, tcn.blas.GpuDownsampleFactorMax) for node in f.maker.env.toposort()])
                assert any([isinstance(node.op, DownsampleFactorMax) for node in f2.maker.env.toposort()])
                assert numpy.allclose(f(), f2())

                g = pfunc([], tensor.grad(ds_op(tensor.as_tensor_variable(a)).sum(), a), mode=mode_with_gpu)
                g2 = pfunc([], tensor.grad(ds_op(tensor.as_tensor_variable(a)).sum(), a), mode=mode_without_gpu)
                assert any(
                    [isinstance(node.op, tcn.blas.GpuDownsampleFactorMaxGrad) for node in g.maker.env.toposort()]
                )
                assert any([isinstance(node.op, DownsampleFactorMaxGrad) for node in g2.maker.env.toposort()])
                assert numpy.allclose(g(), g2())
开发者ID:pascanur,项目名称:Theano,代码行数:60,代码来源:test_blas.py


示例2: make_node

 def make_node(self, a, n, axis):
     a = tensor.as_tensor_variable(a)
     if a.ndim < 1:
         raise TypeError('%s: input must be an array, not a scalar' %
                         self.__class__.__name__)
     if axis is None:
         axis = a.ndim - 1
         axis = tensor.as_tensor_variable(axis)
     else:
         axis = tensor.as_tensor_variable(axis)
         if (not axis.dtype.startswith('int')) and \
            (not axis.dtype.startswith('uint')):
             raise TypeError('%s: index of the transformed axis must be'
                             ' of type integer' % self.__class__.__name__)
         elif axis.ndim != 0 or (isinstance(axis, tensor.TensorConstant) and
                                 (axis.data < 0 or axis.data > a.ndim - 1)):
             raise TypeError('%s: index of the transformed axis must be'
                             ' a scalar not smaller than 0 and smaller than'
                             ' dimension of array' % self.__class__.__name__)
     if n is None:
         n = a.shape[axis]
         n = tensor.as_tensor_variable(n)
     else:
         n = tensor.as_tensor_variable(n)
         if (not n.dtype.startswith('int')) and \
            (not n.dtype.startswith('uint')):
             raise TypeError('%s: length of the transformed axis must be'
                             ' of type integer' % self.__class__.__name__)
         elif n.ndim != 0 or (isinstance(n, tensor.TensorConstant) and
                              n.data < 1):
             raise TypeError('%s: length of the transformed axis must be a'
                             ' strictly positive scalar'
                             % self.__class__.__name__)
     return gof.Apply(self, [a, n, axis], [tensor.TensorType('complex128',
                      a.type.broadcastable)()])
开发者ID:ALISCIFP,项目名称:Segmentation,代码行数:35,代码来源:fourier.py


示例3: __init__

    def __init__(self, distribution, lower, upper, transform="infer", *args, **kwargs):
        dtype = kwargs.get("dtype", theano.config.floatX)

        if lower is not None:
            lower = tt.as_tensor_variable(lower).astype(dtype)
        if upper is not None:
            upper = tt.as_tensor_variable(upper).astype(dtype)

        if transform == "infer":
            if lower is None and upper is None:
                transform = None
                default = None
            elif lower is not None and upper is not None:
                transform = transforms.interval(lower, upper)
                default = 0.5 * (lower + upper)
            elif upper is not None:
                transform = transforms.upperbound(upper)
                default = upper - 1
            else:
                transform = transforms.lowerbound(lower)
                default = lower + 1
        else:
            default = None

        super().__init__(
            distribution, lower, upper, default, *args, transform=transform, **kwargs
        )
开发者ID:pymc-devs,项目名称:pymc3,代码行数:27,代码来源:bound.py


示例4: quantized_lognormal_sampler

def quantized_lognormal_sampler(
    rstream, mu=0.0, sigma=1.0, step=1, draw_shape=None, ndim=None, dtype=theano.config.floatX
):
    """
    Sample from a quantized log-normal distribution centered on avg with
    the specified standard deviation (std).

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, the output shape will be determined by the shapes
    of avg and std.

    If dtype is not specified, it will be inferred from the dtype of
    avg and std, but will be at least as precise as floatX.
    """

    mu = tensor.as_tensor_variable(mu)
    sigma = tensor.as_tensor_variable(sigma)
    step = tensor.as_tensor_variable(step)

    if dtype == None:
        dtype = tensor.scal.upcast(theano.config.floatX, mu.dtype, sigma.dtype, step.dtype)
    rstate = rstream.new_shared_rstate()
    ndim, draw_shape, bcast = tensor.raw_random._infer_ndim_bcast(ndim, draw_shape, mu, sigma)
    op = QuantizedLognormal(otype=tensor.TensorType(dtype=dtype, broadcastable=bcast))
    new_rstate, out = op(rstate, draw_shape, mu, sigma, step)
    rstream.add_default_update(out, rstate, new_rstate)
    return out
开发者ID:yamins81,项目名称:MonteTheano,代码行数:29,代码来源:distributions.py


示例5: make_node

    def make_node(self, activations, labels, input_lengths):
        t_activations = T.as_tensor_variable(activations)
        # Ensure activations array is C-contiguous
        t_activations = cpu_contiguous(t_activations)

        t_labels = T.as_tensor_variable(labels)
        t_input_lengths = T.as_tensor_variable(input_lengths)

        if t_activations.type.dtype != 'float32':
            raise TypeError('activations must use the float32 type!')

        if t_activations.ndim != 3:
            raise ValueError('activations must have 3 dimensions.')

        if t_labels.type.dtype != 'int32':
            raise TypeError('labels must use the int32 type!')

        if t_labels.ndim != 2:
            raise ValueError('labels must have 2 dimensions.')

        if t_input_lengths.type.dtype != 'int32':
            raise TypeError('input_lengths must use the int32 type!')

        if t_input_lengths.ndim != 1:
            raise ValueError('input_lengths must have 1 dimension.')

        costs = T.fvector(name="ctc_cost")
        outputs = [costs]
        if self.compute_grad:
            gradients = T.ftensor3(name="ctc_grad")
            outputs += [gradients]

        return gof.Apply(self, inputs=[t_activations, t_labels, t_input_lengths],
                         outputs=outputs)
开发者ID:rezaprimasatya,项目名称:Theano,代码行数:34,代码来源:ctc.py


示例6: make_node

 def make_node(self,x,y):
     if x.type.ndim != y.type.ndim:
         raise TypeError()
     # TODO: consider restricting the dtype?
     x = tensor.as_tensor_variable(x)
     y = tensor.as_tensor_variable(y)
     return gof.Apply(self, [x,y], [])
开发者ID:RedHenLab,项目名称:Gesture,代码行数:7,代码来源:raw_theano.py


示例7: test_neibs_bad_shape_wrap_centered

    def test_neibs_bad_shape_wrap_centered(self):
        shape = (2, 3, 10, 10)

        for dtype in self.dtypes:
            images = shared(numpy.arange(
                numpy.prod(shape), dtype=dtype
                ).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)

                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
                images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
                neib_shape = T.as_tensor_variable((3, 3))
                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            # Test a valid shapes
            shape = (2, 3, 3, 3)
            images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
            neib_shape = T.as_tensor_variable((3, 3))

            f = function([],
                         images2neibs(images, neib_shape, mode="wrap_centered"),
                         mode=self.mode)
            f()
开发者ID:Faruk-Ahmed,项目名称:Theano,代码行数:33,代码来源:test_neighbours.py


示例8: make_node

 def make_node(self, x, scale, shift):
     if x.type.ndim != 4:
         raise TypeError()
     x = tensor.as_tensor_variable(x)
     scale = tensor.as_tensor_variable(scale)
     shift = tensor.as_tensor_variable(shift)
     return gof.Apply(self, [x, scale, shift], [x.type()])
开发者ID:intel,项目名称:theano,代码行数:7,代码来源:mkl_bn.py


示例9: __init__

    def __init__(self, distribution, lower, upper,
                 transform='infer', *args, **kwargs):
        dtype = kwargs.get('dtype', theano.config.floatX)

        if lower is not None:
            lower = tt.as_tensor_variable(lower).astype(dtype)
        if upper is not None:
            upper = tt.as_tensor_variable(upper).astype(dtype)

        if transform == 'infer':
            if lower is None and upper is None:
                transform = None
                default = None
            elif lower is not None and upper is not None:
                transform = transforms.interval(lower, upper)
                default = 0.5 * (lower + upper)
            elif upper is not None:
                transform = transforms.upperbound(upper)
                default = upper - 1
            else:
                transform = transforms.lowerbound(lower)
                default = lower + 1
        else:
            default = None

        super(_ContinuousBounded, self).__init__(
            distribution=distribution, lower=lower, upper=upper,
            transform=transform, default=default, *args, **kwargs)
开发者ID:alexander-belikov,项目名称:pymc3,代码行数:28,代码来源:bound.py


示例10: _traverse

    def _traverse(node):
        """ TODO: writeme """
        if node is None:
            return None
        else:
            op = node.op
            inputs = node.inputs

            # Compute the evaluation points corresponding to each of the
            # inputs of the node
            local_eval_points = []
            for inp in inputs:
                if inp in wrt:
                    local_eval_points.append(eval_points[wrt.index(inp)])
                elif inp.owner is None:
                    local_eval_points.append(inp.zeros_like())
                elif inp.owner in seen_nodes:

                    local_eval_points.append(seen_nodes[inp.owner][inp.owner.outputs.index(inp)])

                else:
                    # We actually need to compute the R_op for this node

                    _traverse(inp.owner)
                    local_eval_points.append(seen_nodes[inp.owner][inp.owner.outputs.index(inp)])
            for x, y in zip(inputs, local_eval_points):
                if y is not None:
                    assert as_tensor_variable(x).type == as_tensor_variable(y).type

            seen_nodes[node] = op.R_op(node.inputs, local_eval_points)
            return None
开发者ID:vlb,项目名称:Theano,代码行数:31,代码来源:gradient.py


示例11: make_node

    def make_node(self, kern, topgrad, shape=None):
        kern = as_tensor_variable(kern)
        topgrad = as_tensor_variable(topgrad)
        kern, topgrad = self.as_common_dtype(kern, topgrad)
        if kern.type.ndim != 5:
            raise TypeError('kern must be 5D tensor')
        if topgrad.type.ndim != 5:
            raise TypeError('topgrad must be 5D tensor')
        if shape is None:
            if self.subsample != (1, 1, 1):
                raise ValueError('shape must be given if subsample != (1, 1, 1)')
            height_width_depth = []
        else:
            height_width_depth = [as_tensor_variable(shape[0]).astype('int64'),
                                  as_tensor_variable(shape[1]).astype('int64'),
                                  as_tensor_variable(shape[2]).astype('int64')]

        if self.num_groups > 1:
            broadcastable = [topgrad.type.broadcastable[0], False,
                             False, False, False]
        else:
            broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1],
                             False, False, False]
        dtype = kern.type.dtype
        return Apply(self, [kern, topgrad] + height_width_depth,
                     [TensorType(dtype, broadcastable)()])
开发者ID:DEVESHTARASIA,项目名称:Theano,代码行数:26,代码来源:corr3d.py


示例12: choice

def choice(random_state, size=None, a=2, replace=True, p=None, ndim=None,
           dtype='int64'):
    """
    Choose values from `a` with or without replacement. `a` can be a 1-D array
    or a positive scalar. If `a` is a scalar, the samples are drawn from the
    range 0,...,a-1.

    If the size argument is ambiguous on the number of dimensions, ndim
    may be a plain integer to supplement the missing information.

    If size is None, a scalar will be returned.

    """
    # numpy.random.choice is only available for numpy versions >= 1.7
    major, minor, _ = numpy.version.short_version.split('.')
    if (int(major), int(minor)) < (1, 7):
        raise ImportError('choice requires at NumPy version >= 1.7 '
                          '(%s)' % numpy.__version__)
    a = tensor.as_tensor_variable(a)
    if isinstance(replace, bool):
        replace = tensor.constant(replace, dtype='int8')
    else:
        replace = tensor.as_tensor_variable(replace)
    # encode p=None as an empty vector
    p = tensor.as_tensor_variable(p or [])
    ndim, size, bcast = _infer_ndim_bcast(ndim, size)
    op = RandomFunction(choice_helper, tensor.TensorType(dtype=dtype,
                                                         broadcastable=bcast))
    return op(random_state, size, a, replace, p)
开发者ID:ballasn,项目名称:Theano,代码行数:29,代码来源:raw_random.py


示例13: make_node

 def make_node(self, A, b):
     A = as_tensor_variable(A)
     b = as_tensor_variable(b)
     otype = tensor.tensor(
             broadcastable=b.broadcastable,
             dtype = (A*b).dtype)
     return Apply(self, [A,b], [otype])
开发者ID:hamelphi,项目名称:Theano,代码行数:7,代码来源:ops.py


示例14: __init__

    def __init__(self, q, beta, *args, **kwargs):
        super().__init__(*args, defaults=('median',), **kwargs)

        self.q = q = tt.as_tensor_variable(q)
        self.beta = beta = tt.as_tensor_variable(beta)

        self.median = self._ppf(0.5)
开发者ID:brandonwillard,项目名称:pymc3,代码行数:7,代码来源:discrete.py


示例15: __init__

 def __init__(self, w, mu, *args, **kwargs):
     _, sd = get_tau_sd(tau=kwargs.pop('tau', None),
                        sd=kwargs.pop('sd', None))
     self.mu = mu = tt.as_tensor_variable(mu)
     self.sd = sd = tt.as_tensor_variable(sd)
     super(NormalMixture, self).__init__(w, Normal.dist(mu, sd=sd),
                                         *args, **kwargs)
开发者ID:aasensio,项目名称:pymc3,代码行数:7,代码来源:mixture.py


示例16: __init__

    def __init__(self, q, beta, *args, **kwargs):
        super(DiscreteWeibull, self).__init__(*args, defaults=['median'], **kwargs)

        self.q = q = tt.as_tensor_variable(q)
        self.beta = beta = tt.as_tensor_variable(beta)

        self.median = self._ppf(0.5)
开发者ID:bballamudi,项目名称:pymc3,代码行数:7,代码来源:discrete.py


示例17: make_node

    def make_node(self, softmaxes, y_idxes, y_lengths, y_startidxes, g_costs, **kwargs):
        softmaxes = T.as_tensor_variable(softmaxes)
        y_idxes = T.as_tensor_variable(y_idxes)
        y_lengths = T.as_tensor_variable(y_lengths)
        y_startidxes = T.as_tensor_variable(y_startidxes)
        g_costs = T.as_tensor_variable(g_costs)

        if (softmaxes.type.ndim != 3 or
            softmaxes.type.dtype not in T.float_dtypes):
            raise ValueError('dy must be 3-d tensor of floats', softmaxes.type)

        if (y_idxes.type.ndim != 2 or
            y_idxes.type.dtype not in T.discrete_dtypes):
            raise ValueError('y_idxes must be 2-d tensor of integers', y_idxes.type)

        if (y_lengths.type.ndim != 1 or
            y_lengths.type.dtype not in T.discrete_dtypes):
            raise ValueError('y_lengths must be 1-d tensor of integers', y_lengths.type)

        if (y_startidxes.type.ndim != 1 or
            y_startidxes.type.dtype not in T.discrete_dtypes):
            raise ValueError('y_startidxes must be 1-d tensor of integers', y_startidxes.type)

        if (g_costs.type.ndim != 1 or
            g_costs.type.dtype not in T.float_dtypes):
            raise ValueError('g_costs must be 1-d tensor of floats', g_costs.type)

        return Apply(self, [softmaxes, y_idxes, y_lengths, y_startidxes, g_costs],
                     [T.Tensor(dtype=softmaxes.dtype, broadcastable=softmaxes.type.broadcastable)()])
开发者ID:Beronx86,项目名称:theano_lstm,代码行数:29,代码来源:masked_loss.py


示例18: make_node

    def make_node(self, x, new_length, insert_at):
        """
        .. todo::

            WRITEME
        """
        x_ = tensor.as_tensor_variable(x)
        new_length_ = tensor.as_tensor_variable(new_length)
        insert_at_ = tensor.as_tensor_variable(insert_at)
        assert x_.ndim == self.ndim, (
            "%s instance expected x.ndim = %d, got %d" %
            (self.__class__.__name__, self.ndim, x.ndim)
        )
        assert new_length_.ndim == 0, "new_length must be a scalar"
        assert insert_at_.ndim == 1, "insert_at must be vector"
        assert (new_length_.dtype.startswith('int') or
                new_length.dtype.startswith('uint')), (
                    "new_length must be integer type"
                )
        assert (insert_at_.dtype.startswith('int') or
                insert_at_.dtype.startswith('uint')), (
                    "insert_at must be integer type"
                )
        return theano.Apply(self,
          inputs=[x_, new_length_, insert_at_],
          outputs=[x_.type()])
开发者ID:julius506,项目名称:pylearn2,代码行数:26,代码来源:insert_along_axis.py


示例19: make_node

 def make_node(self, n, p, shape):
     n = tensor.as_tensor_variable(n)
     p = tensor.as_tensor_variable(p)
     shape = tensor.as_tensor_variable(shape)
     return gof.Apply(self, [n, p, shape],
                      [SparseType(dtype=self.dtype,
                                  format=self.format).make_variable()])
开发者ID:Jackwangyang,项目名称:Theano,代码行数:7,代码来源:sp2.py


示例20: __init__

    def __init__(self, rho, sigma=None, tau=None,
                 constant=False, init=Flat.dist(),
                 sd=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if sd is not None:
            sigma = sd

        tau, sigma = get_tau_sigma(tau=tau, sigma=sigma)
        self.sigma = self.sd = tt.as_tensor_variable(sigma)
        self.tau = tt.as_tensor_variable(tau)

        self.mean = tt.as_tensor_variable(0.)

        if isinstance(rho, list):
            p = len(rho)
        else:
            try:
                shape_ = rho.shape.tag.test_value
            except AttributeError:
                shape_ = rho.shape

            if hasattr(shape_, "size") and shape_.size == 0:
                p = 1
            else:
                p = shape_[0]

        if constant:
            self.p = p - 1
        else:
            self.p = p

        self.constant = constant
        self.rho = rho = tt.as_tensor_variable(rho)
        self.init = init
开发者ID:aloctavodia,项目名称:pymc3,代码行数:34,代码来源:timeseries.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python tensor.batched_dot函数代码示例发布时间:2022-05-27
下一篇:
Python tensor.as_tensor函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap