PyTorch学习
1、基本操作
import numpy as np
import torch
obj = torch.Tensor(3,4)
#1.判断是否是Tensor
torch.is_tensor(obj) # isinstance(obj,torch.Tensor)
#2.全局设置Tensor类型
torch.set_default_tensor_type(torch.DoubleTensor) #torch.Tensor(2).dtype : torch.float64
#3.获取Tensor元素个数
torch.numel(obj)
#4.与numpy互相转换
def array2tensor(array):
return torch.from_numpy(array)
def tensor2array(tensor):
return tensor.numpy()
#5.创建tensor ,基本同numpy
torch.zeros(2,2)
torch.eye(3,3)#单位矩阵
torch.arange(1,12,3)#step=3 1,4,7,10
torch.linspace(1,9,5,requires_grad=True