import torch
import numpy as np### 1. 由函数创建
x = torch.zeros(5, 3, dtype=torch.int64) # 指定数据类型
print(x.dtype)
x = torch.zeros(5, 3) # 默认数据类型为torch.float32
print(x.dtype)x = torch.rand(5, 3)x = torch.torch.ones(10,2,3)
x = torch.empty(5, 3)# Returns a tensor filled with random numbers from a normal distribution
x = torch.randn(2, 3)
# help(torch.randn)
print(x)# Returns a tensor filled with random numbers from a uniform distribution
# on the interval [0,1)
x = torch.rand(2, 3)
print(x)x = torch.eye(5)
print(x)### 2. 从列表创建
x = torch.tensor([[1., -1.], [1., -1.]]) ## 从 numpy array创建
x = torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
print(x)# torch tensor 和 numpy array互换
a = torch.ones(5)
b = a.numpy()a = np.ones(5)
b = torch.from_numpy(a)x = torch.randn(1)
print(x) # 一维tensor
print(x.item()) # Python number
print(x.numpy()) # numpy 一维数组### 3. 通过现有的Tensor来创建
x = torch.ones(2,3)
print(x)
print(x.dtype)
x = x.new_ones(5, 3, dtype=torch.float64) # 返回的tensor默认具有相同的torch.dtype和torch.deviceprint(x)
x = torch.randn_like(x, dtype=torch.float) # 指定新的数据类型
print(x)### 4. 查看tensor大小和形状
x = torch.randn(5,3,2)
print(x)
print(x.size())
print(x.shape)### 5. 改变tensor形状
a = torch.randn(5,3,2)
print(a.size())
a = a.view(-1, 5)
print(a.size())#b = torch.Tensor([1,2,3]).reshape((3,1))
b = torch.tensor([1,2,3]).reshape((3,1))
print(b)### 6. 查看tensor id
x = torch.tensor([1, 2])
y = torch.tensor([3, 4])id_before = id(y)#y = y + x
#print(id(y) == id_before) # Falsey[:] = y + x
print(id(y) == id_before) # True### 7. tensor运算
x = torch.rand(5, 3)
y = torch.rand(5, 3)print(x + y)print(torch.add(x, y))result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)y.add_(x)
print(y)z = x * 3
print(z)# 对应元素相乘,矩阵形状一样
print(torch.mul(x, y))
print(x * y) # 矩阵形成,前一矩阵的列数等于后一矩阵的行数
a = torch.ones(3,4)
b = torch.ones(4,2)
print(torch.mm(a, b))# 批处理,最后两个维度进行torch.mm操作
# batched matrix x batched matrix
tensor1 = torch.randn(10, 3, 4)
tensor2 = torch.randn(10, 4, 5)
print(torch.matmul(tensor1, tensor2).size())### 8. tensor克隆
x = torch.randn(5,3,2)print(x)
x_cp = x.clone()
print(x_cp)print(id(x) == id(x_cp)) # Falseprint(id(x),id(x.view(-1,5))) # False### 9. 指定tensor在CPU或GPU上。device = torch.device('cuda' if torch.cuda.is_available() else
'cpu')
print(device)
x = torch.ones(5,3,device=device)
print(x)x = torch.ones(5,3)
x = x.to(device) # 转移
print(x)### 10. tensor梯度
x = torch.rand(2, 2, requires_grad=True)
print(x)
print(x.grad_fn)
print(x.grad)# 构建计算图,再求x的梯度
y = 3*x
print(y)
print(y.grad_fn)print(y.view(-1,4))z = y * y
z = z.sum()
print(z.grad_fn)print(x.is_leaf, y.is_leaf, z.is_leaf)z.backward() # z要求是标量
print(x.grad)
参考:
https://pytorch.org/docs/stable/tensors.html