torch.where,torch.cat

import torch

if __name__ == '__main__':
    x = torch.randn(2,3,dtype=torch.double)
    print(x)
    x = torch.where(x>0,x,0.)
    print(x)
    y = torch.randn(1,3)
    print(y)
    print(torch.cat((x,y),dim=0))

torch.where (condition,x,y)三个参数 ,x,y是形状一样或者可以广播的,condition是满足的条件,当某个位置满足条件的时候,取x相应的位置上的值,否则取y相应位置上的值。

cat拼接操作,二维的情况按行拼接,dim = 0, 沿着列拼接,dim = 1.

tensor([[-0.2799, -0.6307, -0.7366],
        [ 0.4416, -0.4934, -0.2466]], dtype=torch.float64)
tensor([[0.0000, 0.0000, 0.0000],
        [0.4416, 0.0000, 0.0000]], dtype=torch.float64)
tensor([[-0.1325, -0.6699, -0.1472]])
tensor([[ 0.0000,  0.0000,  0.0000],
        [ 0.4416,  0.0000,  0.0000],
        [-0.1325, -0.6699, -0.1472]], dtype=torch.float64)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值