tensor
是类似于array
和matrix
的结构,可以在GPU或CPU上运行,在PyTorch中,模型的输入、输出和参数都是tensor
格式的。
导入相关的库
import torch
import numpy as np
一、初始化一个tensor
1.1 直接从数据转换
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
1.2 从numpy.array转换
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
从tensor
转向一个numpy
数组
t_array = torch.ones(5)
n_array = t.numpy()
将GPU上的tensor
转到CPU上的numpy
cpu_numpy = gpu_tensor.cpu().detatch().numpy()
将tensor
数组转至CPU的方法
cpu_tensor = gpu_tensor.cpu()
cpu_tensor = gpu_tensor.to('cpu')
1.3 其他数组传递数组大小
import numpy as np
import torch
list_data = [[1, 2], [3, 4], [5, 6]]
x_data = torch.tensor(list_data)
x_ones = torch.ones_like(x_data)
x_rand = torch.rand_like(x_data, dtype=torch.float)
print(x_ones, x_rand)
1.4 指定大小创建数组
shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
二、输出tensor的属性
import numpy as np
import torch
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}")
三、tensor转移到GPU上
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to('cuda')
四、tensor的索引
import numpy as np
import torch
data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
tensor = torch.tensor(data)
print('First row: ',tensor[0])
print('First column: ', tensor[:, 0])
print('Last column:', tensor[..., -1])
tensor[:,1] = 0 # 第二列全为0
tensor[1] = 0 # 第二行全为0
print(tensor)
五、tensor的拼接
import numpy as np
import torch
# data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
data = [[1, 2], [3, 4]]
tensor = torch.tensor(data)
t1 = torch.cat([tensor, tensor, tensor], dim=0) # 竖着连接3个tensor
t2 = torch.cat([tensor, tensor, tensor], dim=1) # 横着连接3个tensor
另外有一个函数为torch.stack
,但是它和torch.cat
有区别,它会增加每次拼接的数组的维度,如下所示:
import numpy as np
import torch
# data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
data = [[1, 2], [3, 4]]
tensor = torch.tensor(data)
t1 = torch.stack([tensor, tensor, tensor], dim=0) # 竖着连接3个tensor
t2 = torch.cat([tensor, tensor, tensor], dim=0) # 横着连接3个tensor
print(t1)
print(t2)
tensor([[[1, 2],
[3, 4]], [[1, 2],
[3, 4]], [[1, 2],
[3, 4]]]) tensor([[1, 2],
[3, 4],
[1, 2],
[3, 4],
[1, 2],
[3, 4]])
六、计算tensor的相乘和点积
import numpy as np
import torch
# data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
data = [[1, 2], [3, 4]]
tensor = torch.tensor(data, dtype=torch.float)
# This computes the matrix multiplication between two tensors. y1, y2, y3 will have the same value
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(tensor)
torch.matmul(tensor, tensor.T, out=y3)
print(y1)
# This computes the element-wise product. z1, z2, z3 will have the same value
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)
print(z1)
七、tensor的item属性
import numpy as np
import torch
# data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
data = [[1, 2], [3, 4]]
tensor = torch.tensor(data,dtype=torch.float)
agg = tensor.sum()
agg_item = agg.item()
print(agg_item, type(agg_item))
八、tensor的in place操作
这个操作我不知道怎么翻译,大致意思就是可以直接对这个tensor数组进行一些操作,
8.1 tensor添加一个数
如这个tensor数组整体加上一个数字:
import numpy as np
import torch
# data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
data = [[1, 2], [3, 4]]
tensor = torch.tensor(data,dtype=torch.float)
print(tensor, "\n")
tensor.add_(5) # 数组中的每个数都加上5
print(tensor)
8.2 tensor的复制
import numpy as np
import torch
# data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
data = [[1, 2], [3, 4]]
tensor = torch.tensor(data,dtype=torch.float)
print(tensor, "\n")
tensor_copy = tensor.copy_(tensor) # 复制这个tensor
tensor_copy.add_(5)
print(tensor_copy)
8.3 tensor的转置
import numpy as np
import torch
# data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
data = [[1, 2], [3, 4]]
tensor = torch.tensor(data,dtype=torch.float)
print(tensor, "\n")
tensor_t = tensor.t_() # 转置数组
print(tensor_t)