pytorch中numpy到tensor使用转化

程序展示:

import numpy as np
import torch
from torch.autograd import Variable


n_out = np.random.randint(0,3,(2,2,3))
print(n_out.dtype)
print(n_out)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)

t_out=torch.from_numpy(n_out)
t_out = transforms.ToTensor()(n_out)
# t_out.to(device=device, dtype=torch.float32)
t_out.cuda()
print(t_out.type())
print(t_out.shape)
print(t_out)
data=t_out.cpu()
print(data)
# proint(t_out.numpy())
print(t_out.cpu().numpy())

参考:https://blog.csdn.net/moshiyaofei/article/details/90519430

你可能感兴趣的:(pytorch)