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


Python tensor.sum方法代码示例

本文整理汇总了Python中theano.tensor.sum方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.sum方法的具体用法?Python tensor.sum怎么用?Python tensor.sum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在theano.tensor的用法示例。


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

示例1: reduce_log_sum

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def reduce_log_sum(tensor, axis=None, guaranteed_finite=False):
    """
    Sum probabilities in the log domain, i.e return
        log(e^vec[0] + e^vec[1] + ...)
        = log(e^x e^(vec[0]-x) + e^x e^(vec[1]-x) + ...)
        = log(e^x [e^(vec[0]-x) + e^(vec[1]-x) + ...])
        = log(e^x) + log(e^(vec[0]-x) + e^(vec[1]-x) + ...)
        = x + log(e^(vec[0]-x) + e^(vec[1]-x) + ...)
    For numerical stability, we choose x = max(vec)
    Note that if x is -inf, that means all values are -inf,
    so the answer should be -inf. In this case, choose x = 0
    """
    maxval = T.max(tensor, axis)
    maxval_full = T.max(tensor, axis, keepdims=True)
    if not guaranteed_finite:
        maxval = T.switch(T.isfinite(maxval), maxval, T.zeros_like(maxval))
        maxval_full = T.switch(T.isfinite(maxval_full), maxval_full, T.zeros_like(maxval_full))
    reduced_sum = T.sum(T.exp(tensor - maxval_full), axis)
    logsum = maxval + T.log(reduced_sum)
    return logsum 
开发者ID:hexahedria,项目名称:gated-graph-transformer-network,代码行数:22,代码来源:util.py

示例2: define_cost

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def define_cost(self, pred, y0, m0):
        bsize = self.bsize
        npix = int(np.prod(test_shape(y0)[1:]))
        y0_target = y0.reshape((self.bsize, npix))
        y0_mask = m0.reshape((self.bsize, npix))
        pred = pred.reshape((self.bsize, npix))

        p = pred * y0_mask
        t = y0_target * y0_mask

        d = (p - t)

        nvalid_pix = T.sum(y0_mask, axis=1)
        depth_cost = (T.sum(nvalid_pix * T.sum(d**2, axis=1))
                         - 0.5*T.sum(T.sum(d, axis=1)**2)) \
                     / T.maximum(T.sum(nvalid_pix**2), 1)

        return depth_cost 
开发者ID:hjimce,项目名称:Depth-Map-Prediction,代码行数:20,代码来源:depth.py

示例3: Linear

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def Linear(inp, inp_dim, outp_dim, vs, name="linear_layer", use_bias=True, initializer=None, bias_initializer=None):
    if isinstance(inp, tuple):
        assert isinstance(inp_dim, tuple)
        # Build initializers which are aware of the real shape of the overall
        # (unsplit) matrix.
        real_inp_dim = sum(inp_dim)
        initializer = partial(initializer or vs.default_initializer,
                              real_shape=(real_inp_dim, outp_dim))

        try:
            Ws = [vs.add_param("%s_W%i" % (name, i), (dim_i, outp_dim),
                               initializer=initializer)
                  for i, dim_i in enumerate(inp_dim)]
        except TypeError, e:
            raise RuntimeError(
                "TypeError in vs initialization for split Gemm. Does the "
                "initializer you provided (%s) support real_shape?"
                % initializer, e)

        outp = T.dot(inp[0], Ws[0])
        for inp_i, W_i in zip(inp[1:], Ws[1:]):
            # TODO inplace add?
            outp += T.dot(inp_i, W_i) 
开发者ID:stanfordnlp,项目名称:spinn,代码行数:25,代码来源:blocks.py

示例4: __init__

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def __init__(self, data, n_sample, rng=None, item_key='item_id', sample_alpha=0.75, sample_store=10000000):
        self.sample_alpha = sample_alpha
        self.sample_store = sample_store
        self.n_sample = n_sample
        if rng is None:
            self.rng = np.random.RandomState(1234)
        else:
            self.rng = rng

        self.pop = data[item_key].value_counts() ** sample_alpha
        self.pop = self.pop.cumsum() / self.pop.sum()
        if self.sample_store:
            self.generate_length = self.sample_store // self.n_sample
            if self.generate_length <= 1:
                self.sample_store = 0
                logger.info('No example store was used')
            else:
                self.neg_samples = self._generate_neg_samples(self.pop, self.generate_length)
                self.sample_pointer = 0
                logger.info('Created sample store with {} batches of samples'.format(self.generate_length))
        else:
            logger.info('No example store was used') 
开发者ID:mquad,项目名称:hgru4rec,代码行数:24,代码来源:hgru4rec.py

示例5: set_output

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def set_output(self):
        padding = self._padding
        input_shape = self._input_shape
        if np.sum(self._padding) > 0:
            padded_input = tensor.alloc(0.0,  # Value to fill the tensor
                                        input_shape[0],
                                        input_shape[1] + 2 * padding[1],
                                        input_shape[2],
                                        input_shape[3] + 2 * padding[3],
                                        input_shape[4] + 2 * padding[4])

            padded_input = tensor.set_subtensor(
                padded_input[:, padding[1]:padding[1] + input_shape[1], :, padding[3]:padding[3] +
                             input_shape[3], padding[4]:padding[4] + input_shape[4]],
                self._prev_layer.output)
        else:
            padded_input = self._prev_layer.output

        self._output = conv3d2d.conv3d(padded_input, self.W.val) + \
            self.b.val.dimshuffle('x', 'x', 0, 'x', 'x') 
开发者ID:chrischoy,项目名称:3D-R2N2,代码行数:22,代码来源:layers.py

示例6: get_output_for

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def get_output_for(self, input, init=False, **kwargs):
        if input.ndim > 2:
            # if the input has more than two dimensions, flatten it into a
            # batch of feature vectors.
            input = input.flatten(2)
        
        activation = T.tensordot(input, self.W, [[1], [0]])
        abs_dif = (T.sum(abs(activation.dimshuffle(0,1,2,'x') - activation.dimshuffle('x',1,2,0)),axis=2)
                    + 1e6 * T.eye(input.shape[0]).dimshuffle(0,'x',1))

        if init:
            mean_min_abs_dif = 0.5 * T.mean(T.min(abs_dif, axis=2),axis=0)
            abs_dif /= mean_min_abs_dif.dimshuffle('x',0,'x')
            self.init_updates = [(self.log_weight_scale, self.log_weight_scale-T.log(mean_min_abs_dif).dimshuffle(0,'x'))]
        
        f = T.sum(T.exp(-abs_dif),axis=2)

        if init:
            mf = T.mean(f,axis=0)
            f -= mf.dimshuffle('x',0)
            self.init_updates.append((self.b, -mf))
        else:
            f += self.b.dimshuffle('x',0)

        return T.concatenate([input, f], axis=1) 
开发者ID:djsutherland,项目名称:opt-mmd,代码行数:27,代码来源:nn.py

示例7: __init__

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def __init__(self):
        def f(x, u, i, terminal):
            if terminal:
                ctrl_cost = T.zeros_like(x[..., 0])
            else:
                ctrl_cost = T.square(u).sum(axis=-1)

            # x: (batch_size, 8)
            # x[..., 0:4]: qpos
            # x[..., 4:8]: qvel, time derivatives of qpos, not used in the cost.
            theta = x[..., 0]  # qpos[0]: angle of joint 0
            phi = x[..., 1]  # qpos[1]: angle of joint 1
            target_xpos = x[..., 2:4]  # qpos[2:4], target x & y coordinate
            body1_xpos = 0.1 * T.stack([T.cos(theta), T.sin(theta)], axis=1)
            tip_xpos_incr = 0.11 * T.stack([T.cos(phi), T.sin(phi)], axis=1)
            tip_xpos = body1_xpos + tip_xpos_incr
            delta = tip_xpos - target_xpos

            state_cost = T.sqrt(T.sum(delta * delta, axis=-1))
            cost = state_cost + ctrl_cost

            return cost

        super().__init__(f, state_size=8, action_size=2) 
开发者ID:HumanCompatibleAI,项目名称:adversarial-policies,代码行数:26,代码来源:mujoco_costs.py

示例8: __init__

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def __init__(self):
        super(M, self).__init__()

        x = T.matrix('x') # input, target
        self.w = module.Member(T.matrix('w')) # weights
        self.a = module.Member(T.vector('a')) # hid bias
        self.b = module.Member(T.vector('b')) # output bias

        self.hid = T.tanh(T.dot(x, self.w) + self.a)
        hid = self.hid

        self.out = T.tanh(T.dot(hid, self.w.T) + self.b)
        out = self.out

        self.err = 0.5 * T.sum((out - x)**2)
        err = self.err

        params = [self.w, self.a, self.b]

        gparams = T.grad(err, params)

        updates = [(p, p - 0.01 * gp) for p, gp in zip(params, gparams)]

        self.step = module.Method([x], err, updates=dict(updates)) 
开发者ID:muhanzhang,项目名称:D-VAE,代码行数:26,代码来源:aa.py

示例9: test_local_csm_grad_c

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def test_local_csm_grad_c():
    raise SkipTest("Opt disabled as it don't support unsorted indices")
    if not theano.config.cxx:
        raise SkipTest("G++ not available, so we need to skip this test.")
    data = tensor.vector()
    indices, indptr, shape = (tensor.ivector(), tensor.ivector(),
                              tensor.ivector())
    mode = theano.compile.mode.get_default_mode()

    if theano.config.mode == 'FAST_COMPILE':
        mode = theano.compile.Mode(linker='c|py', optimizer='fast_compile')

    mode = mode.including("specialize", "local_csm_grad_c")
    for CS, cast in [(sparse.CSC, sp.csc_matrix), (sparse.CSR, sp.csr_matrix)]:
        cost = tensor.sum(sparse.DenseFromSparse()(CS(data, indices, indptr, shape)))
        f = theano.function(
            [data, indices, indptr, shape],
            tensor.grad(cost, data),
            mode=mode)
        assert not any(isinstance(node.op, sparse.CSMGrad) for node
                       in f.maker.fgraph.toposort())
        v = cast(random_lil((10, 40),
                            config.floatX, 3))
        f(v.data, v.indices, v.indptr, v.shape) 
开发者ID:muhanzhang,项目名称:D-VAE,代码行数:26,代码来源:test_opt.py

示例10: test_csm_grad

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def test_csm_grad(self):
        for sparsetype in ('csr', 'csc'):
            x = tensor.vector()
            y = tensor.ivector()
            z = tensor.ivector()
            s = tensor.ivector()
            call = getattr(sp, sparsetype + '_matrix')
            spm = call(random_lil((300, 400), config.floatX, 5))
            out = tensor.grad(dense_from_sparse(
                CSM(sparsetype)(x, y, z, s)
            ).sum(), x)
            self._compile_and_check([x, y, z, s],
                                    [out],
                                    [spm.data, spm.indices, spm.indptr,
                                     spm.shape],
                                    (CSMGrad, CSMGradC)
                                   ) 
开发者ID:muhanzhang,项目名称:D-VAE,代码行数:19,代码来源:test_basic.py

示例11: test_csm_unsorted

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def test_csm_unsorted(self):
        """
        Test support for gradients of unsorted inputs.
        """
        sp_types = {'csc': sp.csc_matrix,
                    'csr': sp.csr_matrix}

        for format in ['csr', 'csc', ]:
            for dtype in ['float32', 'float64']:
                x = tensor.tensor(dtype=dtype, broadcastable=(False,))
                y = tensor.ivector()
                z = tensor.ivector()
                s = tensor.ivector()
                # Sparse advanced indexing produces unsorted sparse matrices
                a = sparse_random_inputs(format, (4, 3), out_dtype=dtype,
                                         unsorted_indices=True)[1][0]
                # Make sure it's unsorted
                assert not a.has_sorted_indices
                def my_op(x):
                    y = tensor.constant(a.indices)
                    z = tensor.constant(a.indptr)
                    s = tensor.constant(a.shape)
                    return tensor.sum(
                        dense_from_sparse(CSM(format)(x, y, z, s) * a))
                verify_grad_sparse(my_op, [a.data]) 
开发者ID:muhanzhang,项目名称:D-VAE,代码行数:27,代码来源:test_basic.py

示例12: test_op

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def test_op(self):
        for format in sparse.sparse_formats:
            for axis in self.possible_axis:
                variable, data = sparse_random_inputs(format,
                                                      shape=(10, 10))

                z = theano.sparse.sp_sum(variable[0], axis=axis)
                if axis is None:
                    assert z.type.broadcastable == ()
                else:
                    assert z.type.broadcastable == (False, )

                f = theano.function(variable, self.op(variable[0], axis=axis))
                tested = f(*data)
                expected = data[0].todense().sum(axis).ravel()
                utt.assert_allclose(expected, tested) 
开发者ID:muhanzhang,项目名称:D-VAE,代码行数:18,代码来源:test_basic.py

示例13: sum

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def sum(x, axis=None, keepdims=False):
    '''Sum of the values in a tensor, alongside the specified axis.
    '''
    return T.sum(x, axis=axis, keepdims=keepdims) 
开发者ID:lingluodlut,项目名称:Att-ChemdNER,代码行数:6,代码来源:theano_backend.py

示例14: categorical_crossentropy

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def categorical_crossentropy(output, target, from_logits=False):
    if from_logits:
        output = T.nnet.softmax(output)
    else:
        # scale preds so that the class probas of each sample sum to 1
        output /= output.sum(axis=-1, keepdims=True)
    # avoid numerical instability with _EPSILON clipping
    output = T.clip(output, _EPSILON, 1.0 - _EPSILON)
    return T.nnet.categorical_crossentropy(output, target) 
开发者ID:lingluodlut,项目名称:Att-ChemdNER,代码行数:11,代码来源:theano_backend.py

示例15: l2_normalize

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import sum [as 别名]
def l2_normalize(x, axis):
    norm = T.sqrt(T.sum(T.square(x), axis=axis, keepdims=True))
    return x / norm 
开发者ID:lingluodlut,项目名称:Att-ChemdNER,代码行数:5,代码来源:theano_backend.py


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