pytorch tutorials-tensors
本篇文章来自pytorch官网,自己翻译加理解。
Tensors 是一种特殊的数据结构,和数组(array)和矩阵(matrices)类似,在pytorch中,使用tensors来组织输入和输出的数据以及模型的参数。它很像NumPy的ndarray,除了一点:tensors可以在GPU上并行计算。其他的和ndarray都很像。
import torch
import numpy as np
Tensor Initialization-Tensor的初始化
有很多方式,如:
从list中 | a=torch.tensor(data_list) |
---|---|
从NumPy array 中 | a=torch.from_numpy(np_array) |
从另一个tensor中 | a=torch.ones_like(x_data) 或者 a=torch.rand_like(x_data) |
例子:
从list中初始化tensor:
data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)
从Numy的ndarray中初始化tensor:
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
从另一个tensor初始化:
除非自己指定,否则新的tensor会保留原tensor的特性,如shape,datatype。
x_ones = torch.ones_like(x_data) # retains the properties of x_data,保留了x_data的特性
print(f"Ones Tensor: \n {x_ones} \n")
x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data,新指定了dtype
print(f"Random Tensor: \n {x_rand} \n")
输出为:
Ones Tensor:
tensor([[1, 1],
[1, 1]])
Random Tensor:
tensor([[0.3483, 0.9411],
[0.7310, 0.6100]])
tensor shape
shape是tensor维度的tuple,它决定了输出tensor的维度。如:
shape = (2,3,)
rand_tensor = torch.rand(shape)#全是随机数
ones_tensor = torch.ones(shape)#全是0
zeros_tensor = torch.zeros(shape)#全是1
Tensor Attributes
Tensor attributes describe their shape, datatype, and the device on which they are stored.
tensor attributes决定了tensor的shape,数据类型和tensor的存储位置(cpu或者GPU)
tensor = torch.rand(3,4)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
Out:
Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu
Tensor Operations-操作
Over 100 tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra,。
tensor有超过100种操作:如调换维度顺序(transposing),索引(indexing),切片(slicing),数学运算(mathematical operations),矩阵运算( linear algebra),随机采样( random sampling)
We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')
例子:
切片和索引
tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)
Out:
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
两个tensor合并:
torch.cat ,torch.stack
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)
Out:
tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])
torch.cat:拼接,但不会增加维度,只在指定维度上拼接,如dim=0,dim=1.
torch.stack:拼接,并且增加一个维度,且需要两个tensor维度相同,如两个(3,3)的tensor,用torch.cat会变成(6,3),用torch.stack会变成(2,3,3)
This computes the element-wise product
tensor中每一项对应的加减乘除
a+b或者torch.add(a,b),效果是一样的
同理:mul(*) 和div(/)和sub(-)
print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n")
print(f"tensor * tensor \n {tensor * tensor}")
This computes the matrix multiplication between two tensors
矩阵运算
print(f"tensor.matmul(tensor.T) \n {tensor.matmul(tensor.T)} \n")
print(f"tensor @ tensor.T \n {tensor @ tensor.T}")
Out:
tensor.matmul(tensor.T)
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
tensor @ tensor.T
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
In-place operations are Operations that have a _ suffix are in-place. For example: x.copy_(y), x.t_(), will change x.
in-place操作一般有个_作为后缀,就是不经过复制,直接在原来的数据上进行操作,会改变原来数据的值。
print(tensor, "\n")
tensor.add_(5)
print(tensor)
Out:
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
tensor([[6., 5., 6., 6.],
[6., 5., 6., 6.],
[6., 5., 6., 6.],
[6., 5., 6., 6.]])
NOTE
In-place operations save some memory, but can be problematic when computing derivatives because of an immediate loss of history. Hence, their use is discouraged.
in-place操作会节约一些内存,但是不鼓励用。
Bridge with NumPy
Tensors on the CPU and NumPy arrays can share their underlying memory locations, and changing one will change the other.
cpu上的tensors和Numy array可以共享底层内存地址,改变一个也会使另一个发生改变。
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
A change in the tensor reflects in the NumPy array.
改变tensor会使array发生变化(前提是使用in-place操作)
t.add_(1)
print(f"t: {t}")
print(f"n: {n}")
Out:
t: tensor([2., 2., 2., 2., 2.])
n: [2. 2. 2. 2. 2.]
NumPy array to Tensor
n = np.ones(5)
t = torch.from_numpy(n)
np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")
Changes in the NumPy array reflects in the tensor.
改变array会使tensor发生变化
Out:
t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
n: [2. 2. 2. 2. 2.]
有的时候应该挺好用,但是很多时候要注意,防止数据发生了改变自己还不知道。
一些修改
矩阵操作只有两类:一种是普通的加减乘除,就是对应位置的操作,但是矩阵乘法就是用@
或者a.matmul(b)
来。
其实如果tensor
是通过torch.from_numpy()
来创建的,修改一个,另一个就会跟着变化,相当于tensor
和np_array
是指向同一个对象一样。但是如果是通过torch.tensor()
创建的tensor
,这两个就没啥关系了。对比如下:
import torch
import numpy as np
a=np.ones((2))
注意下面这句,用的是torch.from_numpy
b=torch.from_numpy(a)
print(a)
print(b)
a[0]=2
print(a)
print(b)
b[1]=3
print(a)
print(b)
print(id(a)==id(b))
结果如下:
[1. 1.]
tensor([1., 1.], dtype=torch.float64)
[2. 1.]
tensor([2., 1.], dtype=torch.float64)
[2. 3.]
tensor([2., 3.], dtype=torch.float64)
False#说明不是一个内存地址,但是确实动一个另一个也会变
另一种把b=torch.from_numpy(a)
改成b=torch.tensor(a)
就会发现二者互不干扰。
还有就是tensor转换为np_array,也是类似的。
import torch
import numpy as np
t = torch.ones(5)
print(f"t: {t}")
注意下面这句,用的是n = t.numpy()
n = t.numpy()
print(f"n: {n}")
t[1]=2
print(t)
print(n)
n[2]=3
print(t)
print(n)
print(id(t)==id(n))
结果:
t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]
tensor([1., 2., 1., 1., 1.])
[1. 2. 1. 1. 1.]
tensor([1., 2., 3., 1., 1.])
[1. 2. 3. 1. 1.]
False
如果把n = t.numpy()
换成n=np.array(t)
就是:两个好像完全没关系,操作一个不会影响另一个。