Tensor基础汇总

307 阅读2分钟

文章目录

张量(Tensor)

张量是pytorch里面基础的运算单位,与numpy的ndarray相同,都表示的是一个多维的矩阵。tensor可以在GPU上运行,ndarray只能在CPU上运行,在GPU上运行大大加快了运算速度。

# 首先要引入相关的包
import torch
import numpy as np
# 打印一下版本
torch.__version__
'1.7.0+cu101'
# 生成一个简单的张量
x = torch.rand(2,3)
x
tensor([[0.7882, 0.8245, 0.2318],
        [0.2758, 0.6882, 0.3428]])

查看矩阵维数

print(x.shape)
print(x.size())
torch.Size([2, 3])
torch.Size([2, 3])
#  生成多维张量
y = torch.rand(2,3,4,5)
print(y.size())
y
torch.Size([2, 3, 4, 5])





tensor([[[[0.9700, 0.4313, 0.2245, 0.0716, 0.6543],
          [0.4271, 0.4484, 0.9203, 0.9887, 0.0784],
          [0.9048, 0.7636, 0.5515, 0.9687, 0.0586],
          [0.4500, 0.2719, 0.3963, 0.6749, 0.2238]],

         [[0.5442, 0.2652, 0.0143, 0.7669, 0.7882],
          [0.6451, 0.1053, 0.0589, 0.2349, 0.7190],
          [0.3845, 0.1299, 0.7128, 0.2318, 0.7761],
          [0.5834, 0.7134, 0.8084, 0.5529, 0.4356]],

         [[0.2872, 0.9532, 0.8493, 0.2843, 0.7483],
          [0.0706, 0.9270, 0.9473, 0.7191, 0.1599],
          [0.2973, 0.7191, 0.9092, 0.4041, 0.1310],
          [0.6195, 0.8198, 0.2825, 0.4573, 0.2223]]],


        [[[0.1679, 0.3918, 0.8279, 0.5033, 0.1678],
          [0.3332, 0.6504, 0.0449, 0.7577, 0.4634],
          [0.0478, 0.2579, 0.2435, 0.6364, 0.8093],
          [0.7320, 0.4967, 0.8393, 0.5364, 0.0899]],

         [[0.4825, 0.8331, 0.8621, 0.8421, 0.4593],
          [0.1867, 0.8447, 0.7958, 0.1103, 0.5337],
          [0.1519, 0.1805, 0.8782, 0.7402, 0.5493],
          [0.5710, 0.5385, 0.3131, 0.3468, 0.6529]],

         [[0.2199, 0.3373, 0.9917, 0.6991, 0.4593],
          [0.8036, 0.8177, 0.6209, 0.6273, 0.9472],
          [0.1661, 0.1940, 0.6002, 0.2977, 0.6475],
          [0.6194, 0.6915, 0.1525, 0.1648, 0.1456]]]])

在同构的意义下,第零阶张量 (r = 0) 为标量 (Scalar),第一阶张量 (r = 1) 为向量 (Vector), 第二阶张量 (r = 2) 则成为矩阵 (Matrix),第三阶以上的统称为多维张量。

# 生成一个标量
scalar = torch.tensor(3.1433223)
print(scalar)
scalar.size()
tensor(3.1433)





torch.Size([])
# 对于标量,我们可以直接使用.item()从中取出其对应的python对象的数值
scalar.item()
3.143322229385376
# 特别的,如果张量中只有一个元素的tensor也可以用.item方法
tensor = torch.tensor([3.14332223])
print(tensor)
print(tensor.size())
print(tensor.item())
tensor([3.1433])
torch.Size([1])
3.143322229385376

基本类型(5种)

  • 32位浮点型:torch.FloatTensor。 (默认)
  • 64位整型:torch.LongTensor。
  • 32位整型:torch.IntTensor。
  • 16位整型:torch.ShortTensor。
  • 64位浮点型:torch.DoubleTensor。
long = tensor.long() # 64位整形
long
tensor([3])
half = tensor.half() # 16位浮点
half
tensor([3.1426], dtype=torch.float16)
flo = tensor.float()
flo
tensor([3.1433])
short = tensor.short()
short
tensor([3], dtype=torch.int16)
ch = tensor.char()
ch
tensor([3], dtype=torch.int8)
bt = tensor.byte()
bt
tensor([3], dtype=torch.uint8)

Numpy转换

# 使用numpy方法将tensor转化为ndarray
a = torch.randn((3,2))
# tensor转换为numpy
nump_a = a.numpy()
print(nump_a)
[[-0.63136804  0.10507226]
 [-0.7398295  -1.5187927 ]
 [-0.84429693 -0.39006093]]
# numpy转化为tensor
torch_a = torch.from_numpy(nump_a)
torch_a
tensor([[-0.6314,  0.1051],
        [-0.7398, -1.5188],
        [-0.8443, -0.3901]])

Tensor和numpy对象共享内存,所以他们之间的转换很快,而且几乎不会消耗什么资源。但这也意味着,如果其中一个变了,另外一个也会随之改变.

设备间转换

一般情况下可以使用.cuda方法将tensor移动到GPU,这步操作需要cuda设备支持

cpu_a = torch.rand((4,3))
cpu_a
tensor([[0.0381, 0.6092, 0.4768],
        [0.3149, 0.9581, 0.1944],
        [0.7407, 0.5214, 0.4032],
        [0.5865, 0.9149, 0.6676]])
print(cpu_a.type())
# print(torch.cuda.is_available()) #colab里修改->笔记本设置选GPU
torch.FloatTensor
gpu_a = cpu_a.cuda()
gpu_a.type()
'torch.cuda.FloatTensor'
# 使用.cpu方法将tensor移动到cpu
cpu_b = gpu_a.cpu()
cpu_b.type()
'torch.FloatTensor'
# 如果我们有多GPU的情况,可以使用to方法来确定使用哪个设备



# 使用torch.cuda.is_available()来确定是否有cuda设备
device = torch.device('cuda' if torch.cuda.is_available else 'cpu')
print(device)
cuda
# 将张量传送到设备
gpu_b = cpu_b.to(device)
gpu_b.type()
'torch.cuda.FloatTensor'

初始化张量

# 使用[0,1]均匀分布随机初始化二维数组
rnd = torch.rand(5,3)
print(rnd)
tensor([[0.7145, 0.7597, 0.4477],
        [0.5116, 0.4398, 0.3444],
        [0.6151, 0.3329, 0.6626],
        [0.4892, 0.4639, 0.7785],
        [0.1333, 0.5660, 0.1274]])
one = torch.ones(2,3)
one
tensor([[1., 1., 1.],
        [1., 1., 1.]])
zero = torch.zeros(2,2)
zero
tensor([[0., 0.],
        [0., 0.]])
# 初始化一个单位矩阵,即对角线为1 其他为0
eye=torch.eye(2,2)
eye
tensor([[1., 0.],
        [0., 1.]])

常用方法

tensor = torch.randn(3,3)
tensor
tensor([[-0.3459,  0.2093, -1.1872],
        [-0.6068, -0.0489,  0.1501],
        [ 1.9289,  1.8885, -0.3188]])
# 沿着行取最大值
max_value, max_idx = torch.max(tensor,dim=1)
print(max_value,max_idx)
tensor([0.2093, 0.1501, 1.9289]) tensor([1, 2, 0])
# 每行x求和
sum_x = torch.sum(tensor,dim=1)
print(sum_x)
tensor([-1.3239, -0.5056,  3.4986])
y = torch.randn(3,3)
z = tensor + y
print(z)
tensor([[ 0.1602, -0.2848, -0.6249],
        [-0.6315, -1.8085, -0.8782],
        [ 1.2611,  2.2078, -0.3961]])
# 以_为结尾的,均会改变调用值
tensor.add_(y)
tensor
tensor([[ 0.1602, -0.2848, -0.6249],
        [-0.6315, -1.8085, -0.8782],
        [ 1.2611,  2.2078, -0.3961]])