CUDA中的Tensor

CUDA(Computer Unified Device Architecture),是NVIDIA推出的运算平台。CUDA是一种有NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。

torch.cuda这个模块增加了对CUDA tensor的支持,能够在cpu和gpu上使用相同的方法操作tensor.

通过.to方法能够把一个tensor转移到另外一个设备(比如从CPU转到GPU)

import torch

x = torch.rand(5,3)

if torch.cuda.is_available():
    device = torch.device("cuda")
    y = torch.ones_like(x,device=device)
    x = x.to(device)
    z = x+y
    print(z)
    print(z.to("cpu",torch.double))

输出的结果

tensor([[1.3356, 1.9082, 1.7105],
        [1.2802, 1.1533, 1.3852],
        [1.2288, 1.2393, 1.7443],
        [1.3479, 1.6767, 1.8700],
        [1.7983, 1.5379, 1.4596]], device='cuda:0')
tensor([[1.3356, 1.9082, 1.7105],
        [1.2802, 1.1533, 1.3852],
        [1.2288, 1.2393, 1.7443],
        [1.3479, 1.6767, 1.8700],
        [1.7983, 1.5379, 1.4596]], dtype=torch.float64)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值