本文最后更新于:2020年10月19日 下午

基础操作
import torch
x = torch.randn(5,3)
x
tensor([[-0.7203, 0.2899, -1.0918], [ 1.0827, -0.2606, 1.2002], [-1.4074, -0.6593, 0.5724], [-2.3323, -0.3888, 1.1932], [-0.9810, 0.3074, 1.8057]])
torch.empty(5,3)
tensor([[ 0.0000e+00, -3.6893e+19, 0.0000e+00], [-3.6893e+19, 1.1033e-15, 1.4013e-45], [-3.7719e-01, 4.5911e-41, -3.7701e-01], [ 4.5911e-41, 0.0000e+00, 0.0000e+00], [-3.7711e-01, 4.5911e-41, 0.0000e+00]])
x = torch.rand(5,3)
x
tensor([[0.9503, 0.3975, 0.5659], [0.7228, 0.2224, 0.8126], [0.8509, 0.7857, 0.6712], [0.4691, 0.0178, 0.1114], [0.3368, 0.6302, 0.7395]])
x = torch.zeros(3,4)
x.dtype
torch.float32
x = torch.zeros(3,4,dtype=torch.long)
x
x.dtype
torch.int64
x = torch.tensor([4.4,3])
x
从一个已有的tensor构建一个tensor,重用原来tensor的特征,例如数据类型,除非提供新数据
x = x.new_ones(5,3,dtype=torch.double)
print(x);
x.dtype
tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64) torch.float64 随机产生一个与x形状相同的tensor
x = torch.randn_like(x, dtype=torch.float)
x
x.shape
torch.Size([5, 3]) 加法运算
y = torch.rand(5,3)
y
tensor([[0.3626, 0.0456, 0.2931], [0.6978, 0.5613, 0.7820], [0.4712, 0.1948, 0.7791], [0.4992, 0.7192, 0.9465], [0.9000, 0.2099, 0.3872]])
x+y
tensor([[-1.5833, 1.2630, -1.2127], [ 0.9719, 0.7344, -0.0690], [ 1.1334, -0.1762, -0.5193], [ 0.2159, -0.3773, 0.6665], [ 1.7880, 0.4180, -0.9072]])
torch.add(x,y)
tensor([[-1.5833, 1.2630, -1.2127], [ 0.9719, 0.7344, -0.0690], [ 1.1334, -0.1762, -0.5193], [ 0.2159, -0.3773, 0.6665], [ 1.7880, 0.4180, -0.9072]])
result = torch.empty(5,3)
torch.add(x,y,out=result)
result
tensor([[-1.5833, 1.2630, -1.2127], [ 0.9719, 0.7344, -0.0690], [ 1.1334, -0.1762, -0.5193], [ 0.2159, -0.3773, 0.6665], [ 1.7880, 0.4180, -0.9072]]) 任何以_结尾的操作都会替换原变量
y.add_(x)
y
tensor([[-1.5833, 1.2630, -1.2127], [ 0.9719, 0.7344, -0.0690], [ 1.1334, -0.1762, -0.5193], [ 0.2159, -0.3773, 0.6665], [ 1.7880, 0.4180, -0.9072]])
y[2:,]
tensor([[ 1.1334, -0.1762, -0.5193], [ 0.2159, -0.3773, 0.6665], [ 1.7880, 0.4180, -0.9072]])
y[2:4,1:]
tensor([[-0.1762, -0.5193], [-0.3773, 0.6665]])
x.shape
z=x.view(-1,5)
z
tensor([[-1.9458, 1.2174, -1.5058, 0.2741, 0.1731], [-0.8509, 0.6623, -0.3710, -1.2984, -0.2833], [-1.0965, -0.2801, 0.8880, 0.2081, -1.2944]]) 只有一个元素的tensor,可用.item()方法把里面的value变成python数值
x = torch.randn(1)
x.item()
0.704008936882019
z.shape
torch.Size([3, 5]) 转置:
z
tensor([[-1.9458, 1.2174, -1.5058, 0.2741, 0.1731], [-0.8509, 0.6623, -0.3710, -1.2984, -0.2833], [-1.0965, -0.2801, 0.8880, 0.2081, -1.2944]])
t = z.transpose(0,1)
t.shape
torch.Size([5, 3]) 把Torch Tensor转换成Numpy Array
a = torch.ones(5)
a
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
b
array([1., 1., 1., 1., 1.], dtype=float32) 两者共享内存空间
b[2]=2
b
array([1., 1., 2., 1., 1.], dtype=float32)
a
tensor([1., 1., 2., 1., 1.]) 把numpy ndarray转换成Torch Tensor
import numapy as np
a = np.ones(5)
a
array([1., 1., 1., 1., 1.])
b = torch.from_numpy(a)
b
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
np.add(a,1,out=a)
print(a,b)#a和b共享内存
[3. 3. 3. 3. 3.] tensor([3., 3., 3., 3., 3.], dtype=torch.float64)
a = a + 1  #此时为a重新分配一个新的空间
print(a,b)
[4. 4. 4. 4. 4.] tensor([3., 3., 3., 3., 3.], dtype=torch.float64) # CUDA Tensors 使用.to方法,Tensor可以被移动到别的device上
if torch.cuda.is_available():
    device = torch.device("cuda")
    y = torch.ones_like(x, device=device)
    x = x.to(device)
    z=x+y
    z.to("cpu", torch.double)
False
z.to("cpu").data.numpy()
z.cpu().data.numpy() #numpy都是在CPU上操作,需要先将GPU中的变量转换到CPU
model = model.cuda()