拼接

  • numpy

    np.concatenate(vecs, axis=1)
    
  • torch

    t.cat(vecs, axis=1)
    

增加维度

  • numpy

    np.expand_dims(vec, 1)
    
  • torch

    t.unsqueeze(vec, 1)
    

切换维度

  • torch

各维度重排

a=torch.rand(4,3,28,32)
a.permute(0,2,3,1).shape

维度对换

a=torch.rand(4,3,28,32)
a.transpose(1,3).shape

索引

  • numpy

    c = vecs_array[:,[1,2]]
    
  • torch

    torch.index_select(vecs_array, 1, torch.tensor(indexs)) # 1为搜索维度
    

    排序

  • numpy
    conc[conc[:,0].argsort()[::-1]] # 按照第一列排序整个矩阵的行数
    

位置矩阵变换onehot矩阵

  • 输入
# 0无意义,仅占位符
tensor([[1, 1, 1, 2, 1, 1, 1, 1, 1],
        [2, 3, 2, 1, 0, 2, 2, 0, 2],
        [0, 0, 3, 0, 0, 0, 0, 0, 0]])
  • 输出
tensor([[1., 1., 0.],
        [1., 0., 1.],
        [1., 1., 1.],
        [1., 1., 0.],
        [1., 0., 0.],
        [1., 1., 0.],
        [1., 1., 0.],
        [1., 0., 0.],
        [1., 1., 0.]])
  • torch
zeros = torch.zeros(inputs.shape[1],inputs.shape[0],dtype=torch.float)
ones = torch.ones(inputs.shape[1],inputs.shape[0],dtype=torch.float)
zeros.scatter_(1,inputs.T,ones)[:,1:]

矩阵条件选择

原probas矩阵,大于0.5的赋值1,小于0.5的赋值0

  • torch
predict_metrics = torch.where(
                probas > 0.5,
                torch.ones(probas.shape[0], probas.shape[1],
                torch.zeros(
                    probas.shape[0], probas.shape[1]),
            )

维度数据复制

  • torch
input_ids.repeat(x, y)

在第y个维度上,复制x份

results matching ""

    No results matching ""