import torch
x=torch.arange(12) #arange=array+range
x
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
大小
x.shape
torch.Size([12])
x.size()
torch.Size([12])
x.numel() # number of element
12
变形
我们可以通过-1来调⽤此⾃动计算出维度的功能。即我们可以⽤x.reshape(-1,4)或x.reshape(3,-1)来取代x.reshape(3,4)。
x.reshape((3,4)) # not inplace
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
y = x.view(12)
z = x.view(-1, 6) # -1所指的维度可以根据其他维度的值推出来
print(x.size(), y.size(), z.size())
torch.Size([12]) torch.Size([12]) torch.Size([2, 6])
创建
torch.zeros((2,3,4))
tensor([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
torch.zeros(2,3,4) #torch.zeros(2,3,4)=torch.zeros((2,3,4))
tensor([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
torch.ones((2,3,4))
tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
torch.randn((3,4)) #random normal
tensor([[ 0.3262, -0.0639, -1.2047, 0.4276],
[ 0.7353, -0.0858, -0.3234, -0.5404],
[ 0.2908, -0.3504, -0.0557, -0.1088]])
torch.tensor([[1,2,3],[4,5,6]])
tensor([[1, 2, 3],
[4, 5, 6]])
运算符
x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
print(x*y)
print(x+y)
print(x-y)
print(x/y)
print(x**y)
print(torch.exp(x))
tensor([ 2., 4., 8., 16.])
tensor([ 3., 4., 6., 10.])
tensor([-1., 0., 2., 6.])
tensor([0.5000, 1.0000, 2.0000, 4.0000])
tensor([ 1., 4., 16., 64.])
tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])
拼接
X = torch.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(torch.cat((X, Y), dim=0))
print(torch.cat((X, Y), dim=1))
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[ 2., 1., 4., 3.],
[ 1., 2., 3., 4.],
[ 4., 3., 2., 1.]])
tensor([[ 0., 1., 2., 3., 2., 1., 4., 3.],
[ 4., 5., 6., 7., 1., 2., 3., 4.],
[ 8., 9., 10., 11., 4., 3., 2., 1.]])
X == Y
tensor([[False, True, False, True],
[False, False, False, False],
[False, False, False, False]])
print(X.sum())
print(X.sum(dim=0))
#dim属性的全称是dimension,表示维度。dim=0为第0个维度,代表行
print(X.sum(dim=1))
tensor(66.)
tensor([12., 15., 18., 21.])
tensor([ 6., 22., 38.])
广播
x = torch.arange(1, 3).view(1, 2)
print(x)
y = torch.arange(1, 4).view(3, 1)
print(y)
print(x + y)
tensor([[1, 2]])
tensor([[1],
[2],
[3]])
tensor([[2, 3],
[3, 4],
[4, 5]])
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a, b)
a += 1
print(a, b)
b += 1
print(a, b)
[1. 1. 1. 1. 1.] tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
[2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
[3. 3. 3. 3. 3.] tensor([3., 3., 3., 3., 3.], dtype=torch.float64)
if torch.cuda.is_available():
device = torch.device("cuda") # GPU
y = torch.ones_like(x, device=device) # 直接创建一个在GPU上的Tensor
x = x.to(device) # 等价于 .to("cuda")
z = x + y
print(z)
print(z.to("cpu", torch.double)) # to()还可以同时更改数据类型
tensor([[2, 3]], device='cuda:0')
tensor([[2., 3.]], dtype=torch.float64)