potorch入门(一)

来源:互联网 发布:中学生书单知乎 编辑:程序博客网 时间:2024/06/05 09:25

1.在gpu上运行程序

if torch.cuda.is_available():    x = x.cuda()    y = y.cuda()    x + y

2、定义一个矩阵

x = torch.rand(5, 3)print(x)

或者

x = torch.Tensor(5,3)

得到尺寸

x.size()

3、将torch Tensor转变成numpy array

a = torch.ones(5)# 将b转换成numpy数组类型b = a.numpy()print(b)

4、将numpy array 转换成 torch tensor

import numpy as npa = np.ones(5)b = torch.from_numpy(a)
原创粉丝点击