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


Python torch.sub方法代码示例

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


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

示例1: test_torch_sub

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def test_torch_sub():
    x = torch.tensor([0.5, 0.8, 1.3]).fix_prec()
    y = torch.tensor([0.1, 0.2, 0.3]).fix_prec()

    # ADD Private Tensor at wrapper stack
    x = x.private_tensor(allowed_users=["User"])
    y = y.private_tensor(allowed_users=["User"])

    z = torch.sub(x, y)

    # Test if it preserves the parent user credentials.
    assert z.allow("User")
    assert not z.allow("NonRegisteredUser")

    assert (z.child.child.child == torch.LongTensor([400, 600, 1000])).all()
    z_fp = z.float_prec()

    assert (z_fp == torch.tensor([0.4, 0.6, 1.0])).all() 
开发者ID:OpenMined,项目名称:PySyft,代码行数:20,代码来源:test_private.py

示例2: give_edges

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def give_edges(self, pred, block_id):
        r''' Returns the edges for given block.

        Arguments:
            pred (tensor): vertex predictions of dim
                            (batch_size, n_vertices, 3)
            block_id (int): deformation block id (1,2 or 3)
        '''
        batch_size = pred.shape[0]  # (batch_size, n_vertices, 3)
        num_edges = self.edges[block_id-1].shape[0]
        edges = self.edges[block_id-1]
        nod1 = torch.index_select(pred, 1, edges[:, 0].long())
        nod2 = torch.index_select(pred, 1, edges[:, 1].long())
        assert(
            nod1.shape == (batch_size, num_edges, 3) and
            nod2.shape == (batch_size, num_edges, 3))
        final_edges = torch.sub(nod1, nod2)
        assert(final_edges.shape == (batch_size, num_edges, 3))
        return final_edges 
开发者ID:autonomousvision,项目名称:occupancy_networks,代码行数:21,代码来源:training.py

示例3: laplacian_loss

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def laplacian_loss(self, pred1, pred2, block_id):
        r''' Returns the Laplacian loss and move loss for given block.

        Arguments:
            pred (tensor): vertex predictions from previous block
            pred (tensor): vertex predictions from current block
            block_id (int): deformation block id (1,2 or 3)
        '''
        lap1 = self.give_laplacian_coordinates(pred1, block_id)
        lap2 = self.give_laplacian_coordinates(pred2, block_id)
        l_l = torch.sub(lap1, lap2).pow(2).sum(dim=2).mean()

        # move loss from the authors' implementation
        move_loss = 0
        if block_id != 1:
            move_loss = torch.sub(pred1, pred2).pow(2).sum(dim=2).mean()
        return l_l, move_loss 
开发者ID:autonomousvision,项目名称:occupancy_networks,代码行数:19,代码来源:training.py

示例4: margin_loss

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def margin_loss(
    embeddings: torch.Tensor,
    labels: torch.Tensor,
    alpha: float = 0.2,
    beta: float = 1.0,
    skip_labels: Union[int, List[int]] = -1,
) -> torch.Tensor:
    """@TODO: Docs. Contribution is welcome."""
    embeddings = F.normalize(embeddings, p=2.0, dim=1)
    distances = euclidean_distance(embeddings, embeddings)

    margin_mask = _create_margin_mask(labels)
    skip_mask = _skip_labels_mask(labels, skip_labels).float()
    loss = torch.mul(
        skip_mask,
        F.relu(alpha + torch.mul(margin_mask, torch.sub(distances, beta))),
    )
    return loss.sum() / (skip_mask.sum() + _EPS) 
开发者ID:catalyst-team,项目名称:catalyst,代码行数:20,代码来源:functional.py

示例5: forward

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def forward(self, feat_index, feat_value):
        # Step1: 先计算得到线性的那一部分
        feat_value = torch.unsqueeze(feat_value, dim=2)  # None * F * 1
        first_weights = self.first_weights(feat_index)   # None * F * 1
        first_weight_value = torch.mul(first_weights, feat_value)  # None * F * 1
        first_weight_value = torch.squeeze(first_weight_value, dim=2)  # None * F
        y_first_order = torch.sum(first_weight_value, dim=1)  # None

        # Step2: 再计算二阶部分
        secd_feat_emb = self.feat_embeddings(feat_index)                      # None * F * K
        feat_emd_value = torch.mul(secd_feat_emb, feat_value)                 # None * F * K(广播)

        # sum_square part
        summed_feat_emb = torch.sum(feat_emd_value, 1)                        # None * K
        interaction_part1 = torch.pow(summed_feat_emb, 2)                     # None * K

        # squared_sum part
        squared_feat_emd_value = torch.pow(feat_emd_value, 2)                 # None * K
        interaction_part2 = torch.sum(squared_feat_emd_value, dim=1)          # None * K

        y_secd_order = 0.5 * torch.sub(interaction_part1, interaction_part2)
        y_secd_order = torch.sum(y_secd_order, dim=1)

        output = self.bias + y_first_order + y_secd_order
        output = torch.unsqueeze(output, dim=1)
        return output 
开发者ID:JianzhouZhan,项目名称:Awesome-RecSystem-Models,代码行数:28,代码来源:FM_PyTorch.py

示例6: forward

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def forward(self, feat_index, feat_value, use_dropout=True):
        feat_value = torch.unsqueeze(feat_value, dim=2)                       # None * F * 1

        # Step1: 先计算一阶线性的部分 sum_square part
        first_weights = self.first_weights(feat_index)                        # None * F * 1
        first_weight_value = torch.mul(first_weights, feat_value)
        y_first_order = torch.sum(first_weight_value, dim=2)                  # None * F
        if use_dropout:
            y_first_order = nn.Dropout(self.dropout_fm[0])(y_first_order)         # None * F

        # Step2: 再计算二阶部分
        secd_feat_emb = self.feat_embeddings(feat_index)                      # None * F * K
        feat_emd_value = secd_feat_emb * feat_value                           # None * F * K(广播)

        # sum_square part
        summed_feat_emb = torch.sum(feat_emd_value, 1)                        # None * K
        interaction_part1 = torch.pow(summed_feat_emb, 2)                     # None * K

        # squared_sum part
        squared_feat_emd_value = torch.pow(feat_emd_value, 2)                 # None * K
        interaction_part2 = torch.sum(squared_feat_emd_value, dim=1)          # None * K

        y_secd_order = 0.5 * torch.sub(interaction_part1, interaction_part2)
        if use_dropout:
            y_secd_order = nn.Dropout(self.dropout_fm[1])(y_secd_order)

        # Step3: Deep部分
        y_deep = feat_emd_value.reshape(-1, self.num_field * self.embedding_size)  # None * (F * K)
        if use_dropout:
            y_deep = nn.Dropout(self.dropout_deep[0])(y_deep)

        for i in range(1, len(self.layer_sizes) + 1):
            y_deep = getattr(self, 'linear_' + str(i))(y_deep)
            y_deep = getattr(self, 'batchNorm_' + str(i))(y_deep)
            y_deep = F.relu(y_deep)
            if use_dropout:
                y_deep = getattr(self, 'dropout_' + str(i))(y_deep)

        concat_input = torch.cat((y_first_order, y_secd_order, y_deep), dim=1)
        output = self.fc(concat_input)
        return output 
开发者ID:JianzhouZhan,项目名称:Awesome-RecSystem-Models,代码行数:43,代码来源:DeepFM_PyTorch.py

示例7: forward

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def forward(self, predictions, actuals):
        cond = torch.zeros_like(predictions).to(self.device)
        loss = torch.sub(actuals, predictions).to(self.device)

        less_than = torch.mul(loss, torch.mul(torch.gt(loss, cond).type(torch.FloatTensor).to(self.device),
                                              self.training_tau))

        greater_than = torch.mul(loss, torch.mul(torch.lt(loss, cond).type(torch.FloatTensor).to(self.device),
                                                 (self.training_tau - 1)))

        final_loss = torch.add(less_than, greater_than)
        # losses = []
        # for i in range(self.output_size):
        #     prediction = predictions[i]
        #     actual = actuals[i]
        #     if actual > prediction:
        #         losses.append((actual - prediction) * self.training_tau)
        #     else:
        #         losses.append((actual - prediction) * (self.training_tau - 1))
        # loss = torch.Tensor(losses)
        return torch.sum(final_loss) / self.output_size * 2


# test1 = torch.rand(100)
# test2 = torch.rand(100)
# pb = PinballLoss(0.5, 100)
# pb(test1, test2)


### sMAPE

# float sMAPE(vector<float>& out_vect, vector<float>& actuals_vect) {
#   float sumf = 0;
#   for (unsigned int indx = 0; indx<OUTPUT_SIZE; indx++) {
#     auto forec = out_vect[indx];
#     auto actual = actuals_vect[indx];
#     sumf+=abs(forec-actual)/(abs(forec)+abs(actual));
#   }
#   return sumf / OUTPUT_SIZE * 200;
# } 
开发者ID:damitkwr,项目名称:ESRNN-GPU,代码行数:42,代码来源:loss_modules.py

示例8: sub

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def sub(t1, t2):
    """
    Element-wise subtraction of values of operand t2 from values of operands t1 (i.e t1 - t2), not commutative.
    Takes the two operands (scalar or tensor) whose elements are to be subtracted (operand 2 from operand 1)
    as argument.

    Parameters
    ----------
    t1: tensor or scalar
        The first operand from which values are subtracted
    t2: tensor or scalar
        The second operand whose values are subtracted

    Returns
    -------
    result: ht.DNDarray
        A tensor containing the results of element-wise subtraction of t1 and t2.

    Examples:
    ---------
    >>> import heat as ht
    >>> ht.sub(4.0, 1.0)
    tensor([3.])

    >>> T1 = ht.float32([[1, 2], [3, 4]])
    >>> T2 = ht.float32([[2, 2], [2, 2]])
    >>> ht.sub(T1, T2)
    tensor([[-1., 0.],
            [1., 2.]])

    >>> s = 2.0
    >>> ht.sub(s, T1)
    tensor([[ 1.,  0.],
            [-1., -2.]])
    """
    return operations.__binary_op(torch.sub, t1, t2)


# Alias in compliance with numpy API 
开发者ID:helmholtz-analytics,项目名称:heat,代码行数:41,代码来源:arithmetics.py

示例9: give_laplacian_coordinates

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def give_laplacian_coordinates(self, pred, block_id):
        r''' Returns the laplacian coordinates for the predictions and given block.

            The helper matrices are used to detect neighbouring vertices and
            the number of neighbours which are relevant for the weight matrix.
            The maximal number of neighbours is 8, and if a vertex has less,
            the index -1 is used which points to the added zero vertex.

        Arguments:
            pred (tensor): vertex predictions
            block_id (int): deformation block id (1,2 or 3)
        '''
        batch_size = pred.shape[0]
        num_vert = pred.shape[1]
        # Add "zero vertex" for vertices with less than 8 neighbours
        vertex = torch.cat(
            [pred, torch.zeros(batch_size, 1, 3).to(self.device)], 1)
        assert(vertex.shape == (batch_size, num_vert+1, 3))
        # Get 8 neighbours for each vertex; if a vertex has less, the
        # remaining indices are -1
        indices = torch.from_numpy(
            self.lape_idx[block_id-1][:, :8]).to(self.device)
        assert(indices.shape == (num_vert, 8))
        weights = torch.from_numpy(
            self.lape_idx[block_id-1][:, -1]).float().to(self.device)
        weights = torch.reciprocal(weights)
        weights = weights.view(-1, 1).expand(-1, 3)
        vertex_select = vertex[:, indices.long(), :]
        assert(vertex_select.shape == (batch_size, num_vert, 8, 3))
        laplace = vertex_select.sum(dim=2)  # Add neighbours
        laplace = torch.mul(laplace, weights)  # Multiply by weights
        laplace = torch.sub(pred, laplace)  # Subtract from prediction
        assert(laplace.shape == (batch_size, num_vert, 3))
        return laplace 
开发者ID:autonomousvision,项目名称:occupancy_networks,代码行数:36,代码来源:training.py

示例10: cosine_distance

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def cosine_distance(
    x: torch.Tensor, z: Optional[torch.Tensor] = None,
) -> torch.Tensor:
    """Calculate cosine distance between x and z.
    @TODO: Docs. Contribution is welcome.
    """
    x = F.normalize(x)

    if z is not None:
        z = F.normalize(z)
    else:
        z = x.clone()

    return torch.sub(1, torch.mm(x, z.transpose(0, 1))) 
开发者ID:catalyst-team,项目名称:catalyst,代码行数:16,代码来源:functional.py

示例11: skrnn_loss

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def skrnn_loss(gmm_params, kl_params, data, mask=[], device =None):
       
    def get_2d_normal(x1,x2,mu1,mu2,s1,s2,rho):
      ##### implementing Eqn. 24 and 25 of the paper ###########
        norm1 = torch.sub(x1,mu1)
        norm2 = torch.sub(x2,mu2)
        s1s2 = torch.mul(s1,s2)
        z = torch.div(norm1**2,s1**2) + torch.div(norm2**2,s2**2) - 2*torch.div(torch.mul(rho, torch.mul(norm1,norm2)),s1s2)
        deno = 2*np.pi*s1s2*torch.sqrt(1-rho**2)
        numer = torch.exp(torch.div(-z,2*(1-rho**2)))
      ##########################################################
        return numer / deno
    
    eos = torch.stack([torch.Tensor([0,0,0,0,1])]*data.size()[0], device = device).unsqueeze(1)
    data = torch.cat([data, eos], 1) 
    
    target = data.view(-1, 5)
    x1, x2, eos = target[:,0].unsqueeze(1), target[:,1].unsqueeze(1), target[:,2:]

    q_t, pi_t = gmm_params[0], gmm_params[1]
    res = get_2d_normal(x1,x2,gmm_params[2],gmm_params[3],gmm_params[4],gmm_params[5],gmm_params[6])
    epsilon = torch.tensor(1e-5, dtype=torch.float)  # to prevent overflow

    Ls = torch.sum(torch.mul(pi_t,res),dim=1).unsqueeze(1)
    Ls = -torch.log(Ls + epsilon)
    mask_zero_out = 1-eos[:,2]
    
    Ls = torch.mul(Ls, mask_zero_out.view(-1,1))
    Lp = -torch.sum(eos*torch.log(q_t), -1).view(-1,1)
    Lr = Ls + Lp
    mu, sigma = kl_params[0], kl_params[1]

    L_kl = -(0.5)*torch.mean(1 + sigma - mu**2 - torch.exp(sigma))

    return Lr.mean(), L_kl 
开发者ID:GauravBh1010tt,项目名称:DL-Seq2Seq,代码行数:37,代码来源:model.py

示例12: mdn_loss

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def mdn_loss(mdn_params, data, mask=[]):

    def get_2d_normal(x1,x2,mu1,mu2,s1,s2,rho):
        
      ##### implementing Eqn. 24 and 25 of the paper ###########
      norm1 = torch.sub(x1.view(-1,1),mu1)
      norm2 = torch.sub(x2.view(-1,1),mu2)
      s1s2 = torch.mul(s1,s2)
      z = torch.div(norm1**2,s1**2) + torch.div(norm2**2,s2**2) - 2*torch.div(torch.mul(rho, torch.mul(norm1,norm2)),s1s2)
      deno = 2*np.pi*s1s2*torch.sqrt(1-rho**2)
      numer = torch.exp(torch.div(-z,2*(1-rho**2)))
      ##########################################################
      return numer / deno

    eos, x1, x2 = data[:,0], data[:,1], data[:,2]
    e_t, pi_t = mdn_params[0], mdn_params[1]
    res = get_2d_normal(x1,x2,mdn_params[2],mdn_params[3],mdn_params[4],mdn_params[5],mdn_params[6])
    
    epsilon = torch.tensor(1e-20, dtype=torch.float, device=device)  # to prevent overflow

    res1 = torch.sum(torch.mul(pi_t,res),dim=1)
    res1 = -torch.log(torch.max(res1,epsilon))
    res2 = torch.mul(eos, e_t.t()) + torch.mul(1-eos,1-e_t.t())
    res2 = -torch.log(res2)
    
    if len(mask)!=0:        # using masking in case of padding
        res1 = torch.mul(res1,mask)
        res2 = torch.mul(res2,mask)
    return torch.sum(res1+res2) 
开发者ID:GauravBh1010tt,项目名称:DL-Seq2Seq,代码行数:31,代码来源:model.py

示例13: test_torch_sub

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def test_torch_sub(workers):
    bob, alice, james = (workers["bob"], workers["alice"], workers["james"])

    x = torch.tensor([0.5, 0.8, 1.3]).fix_prec()
    y = torch.tensor([0.1, 0.2, 0.3]).fix_prec()

    z = torch.sub(x, y)

    assert (z.child.child == torch.LongTensor([400, 600, 1000])).all()
    z = z.float_prec()

    assert (z == torch.tensor([0.4, 0.6, 1.0])).all()

    # with AdditiveSharingTensor
    tx = torch.tensor([1.0, -2.0, 3.0])
    ty = torch.tensor([0.1, 0.2, 0.3])
    x = tx.fix_prec()
    y = ty.fix_prec().share(bob, alice, crypto_provider=james)

    z1 = torch.sub(y, x).get().float_prec()
    z2 = torch.sub(x, y).get().float_prec()

    assert (z1 == torch.sub(ty, tx)).all()
    assert (z2 == torch.sub(tx, ty)).all()

    # with constant integer
    t = torch.tensor([1.0, -2.0, 3.0])
    x = t.fix_prec()
    c = 4

    z = (x - c).float_prec()
    assert (z == (t - c)).all()

    z = (c - x).float_prec()
    assert (z == (c - t)).all()

    # with constant float
    t = torch.tensor([1.0, -2.0, 3.0])
    x = t.fix_prec()
    c = 4.2

    z = (x - c).float_prec()
    assert ((z - (t - c)) < 10e-3).all()

    z = (c - x).float_prec()
    assert ((z - (c - t)) < 10e-3).all()

    # with dtype int
    x = torch.tensor([1.0, 2.0, 3.0]).fix_prec(dtype="int")
    y = torch.tensor([0.1, 0.2, 0.3]).fix_prec(dtype="int")

    z = x - y
    assert (z.float_prec() == torch.tensor([0.9, 1.8, 2.7])).all() 
开发者ID:OpenMined,项目名称:PySyft,代码行数:55,代码来源:test_precision.py

示例14: load_velodyne_points_torch

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def load_velodyne_points_torch(points, rg, N):

    cloud = pcl.PointCloud()
    cloud.from_array(points)
    clipper = cloud.make_cropbox()
    clipper.set_MinMax(*rg)
    out_cloud = clipper.filter()

    if(out_cloud.size > 15000):
        leaf_size = 0.05
        vox = out_cloud.make_voxel_grid_filter()
        vox.set_leaf_size(leaf_size, leaf_size, leaf_size)
        out_cloud = vox.filter()

    if(out_cloud.size > 0):
        cloud = torch.from_numpy(np.asarray(out_cloud)).float().cuda()

        points_count = cloud.shape[0]
        # pdb.set_trace()
        # print("indices", len(ind))
        if(points_count > 1):
            prob = torch.randperm(points_count)
            if(points_count > N):
                idx = prob[:N]
                crop = cloud[idx]
                # print(len(crop))
            else:
                r = int(N/points_count)
                cloud = cloud.repeat(r+1,1)
                crop = cloud[:N]
                # print(len(crop))

            x_shift = (rg[0] + rg[4])/2.0
            y_shift = (rg[1] + rg[5])/2.0
            z_shift = -1.8
            shift = torch.tensor([x_shift, y_shift, z_shift]).cuda()
            crop = torch.sub(crop, shift)

        else:
            crop = torch.ones(N,3).cuda()
            # print("points count zero")

    else:
        crop = torch.ones(N,3).cuda()
        # print("points count zero")


    return crop 
开发者ID:anshulpaigwar,项目名称:Attentional-PointNet,代码行数:50,代码来源:kitti_evaluation.py

示例15: forward

# 需要导入模块: import torch [as 别名]
# 或者: from torch import sub [as 别名]
def forward(self, inp, char_vec, old_k, old_w, text_len, hidden1, hidden2, bias = 0):
        
        if len(inp.size()) == 2:
            inp=inp.unsqueeze(1)
            
        embed = torch.cat([inp, old_w], dim=-1)   # adding attention window to the input of rnn
        
        output1, hidden1 = self.rnn1(embed, hidden1)
        if self.bi_mode == 1:
            output1 = output1[:,:,0:self.hidden_size] + output1[:,:,self.hidden_size:]            
        
        ##### implementing Eqn. 48 - 51 of the paper ###########
        abk_t = self.window(output1.squeeze(1)).exp()
        a_t, b_t, k_t = abk_t.split(self.num_attn_gaussian, dim=1)
        k_t = old_k + k_t        
        #######################################################
        
        
        ##### implementing Eqn. 46 and 47 of the paper ###########
        u = torch.linspace(1, char_vec.shape[1], char_vec.shape[1], device=device)
        phi_bku = torch.exp(torch.mul(torch.sub(k_t.unsqueeze(2).repeat((1,1,len(u))),u)**2,
                                      -b_t.unsqueeze(2)))
        phi = torch.sum(torch.mul(a_t.unsqueeze(2),phi_bku),dim=1)* (char_vec.shape[1]/text_len)
        win_t = torch.sum(torch.mul(phi.unsqueeze(2), char_vec),dim=1)
        ##########################################################
        
        
        inp_skip = torch.cat([output1, inp, win_t.unsqueeze(1)], dim=-1)  # implementing skip connection
        output2, hidden2 = self.rnn2(inp_skip, hidden2)        
        if self.bi_mode == 1:
            output2 = output2[:,:,0:self.hidden_size] + output2[:,:,self.hidden_size:]          
        output = torch.cat([output1,output2], dim=-1)

        ##### implementing Eqn. 17 to 22 of the paper ###########
        y_t = self.mdn(output.squeeze(1))  

        e_t = y_t[:,0:1]
        pi_t, mu1_t, mu2_t, s1_t, s2_t, rho_t = torch.split(y_t[:,1:], self.num_gaussian, dim=1)
        e_t = F.sigmoid(e_t)
        pi_t = F.softmax(pi_t*(1+bias))  # bias would be used during inference
        s1_t, s2_t = torch.exp(s1_t), torch.exp(s2_t)
        rho_t = torch.tanh(rho_t)
        ##########################################################
        
        mdn_params = [e_t, pi_t, mu1_t, mu2_t, s1_t, s2_t, rho_t, phi, win_t, k_t]       
        return mdn_params, hidden1, hidden2 
开发者ID:GauravBh1010tt,项目名称:DL-Seq2Seq,代码行数:48,代码来源:model.py


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